-1

I know it is a duplicate question but I tried almost all the solutions given in all the answers I'm not getting it worked

Below is my encoded JSON response

string(22672) "{"status":true,"result":{"0":{"keyword":"BMW","avg_cpc":0.405132,"min_clicks":42847.5859375,"avg_budget":17358.92818603125},"1":{"keyword":"BMW livrable","avg_cpc":0,"min_clicks":0,"avg_budget":0},"2":{"keyword":"BMW disponible","avg_cpc":0.595299,"min_clicks":27.446643829345703,"avg_budget":16.338959624965668},"3":{"keyword":"bonne affaires BMW","avg_cpc":0,"min_clicks":0,"avg_budget":0},"4":{"keyword":"BMW en stock","avg_cpc":0.370574,"min_clicks":12.511731147766113,"avg_budget":4.63652225835228},"max_cpc":1.0131090475410072,"min_clicks":42887.54431247711}} "

For the above json json_last_error_msg() returning Syntax error.

Tried everything given in this answer: json_decode returns JSON_ERROR_SYNTAX but online formatter says the JSON is OK and others also. Nothing working for me.

I have referred to all the answers and tried everything but I can't decode the JSON. Sitting for several hours and decided to ask questions in stack overflow kindly give me a solution.

Direct response :

{"status":true,"result":{"0":{"keyword":"BMW","avg_cpc":0.407065,"min_clicks":42202.34765625,"avg_budget":17179.098648691408},"1":{"keyword":"BMW livrable","avg_cpc":0,"min_clicks":0,"avg_budget":0},"2":{"keyword":"BMW disponible","avg_cpc":0.598459,"min_clicks":27.12619400024414,"avg_budget":16.233914935192107},"3":{"keyword":"bonne affaires BMW","avg_cpc":0,"min_clicks":0,"avg_budget":0},"4":{"keyword":"BMW en stock","avg_cpc":0.367595,"min_clicks":11.617576599121094,"avg_budget":4.270563069953918},"max_cpc":1.0179426327372374,"min_clicks":42241.091426849365}}
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
VinoCoder
  • 1,133
  • 5
  • 22
  • 45
  • It's working for me. Can you provide a [mcve] (to include the JSON string as a hard-coded value) which demonstrates the problem? – David Oct 27 '22 at 12:35
  • 7
    The odd thing here is that it says `string(22672)` for that string in your question. When I copy/paste that string and dump it using `var_dump()`, I get `string(566)`. Is that the full response? If so, could it be a ton of hidden characters (that's not being copied) that messes things up? – M. Eriksson Oct 27 '22 at 12:36
  • @M.Eriksson That's the var_dump of the full response. **ton of hidden characters ** - May be. I cannot even see those characters in var_dump – VinoCoder Oct 27 '22 at 12:41
  • @Anant-Alivetodie That's the var_dump of the direct response – VinoCoder Oct 27 '22 at 12:42
  • @David when i do copy paste of the response it seems to be fine. The issue occurs only with the direct response which has hidden characters. Still i added my direct response in the question – VinoCoder Oct 27 '22 at 12:51
  • 1
    Where does it come from? Is it an API? If so, try calling it from something like Postman and check how many bytes it actually returns. If it still is around 22k, I would contact the API owners. We can't really know why that happens, especially without any context. – M. Eriksson Oct 27 '22 at 12:53
  • Its my own api which is in different server. Is it a problem of json encoding ? @M.Eriksson – VinoCoder Oct 27 '22 at 12:55
  • 1
    @VinoCoder: Have you confirmed that calling the API in another tool (e.g. Postman) returns that exact JSON but still reports the response as being over 22K bytes? – David Oct 27 '22 at 12:56
  • 3
    We can't possibly know where it actually fails or why. We don't know _anything_ about your code or what you're doing. Do more debugging to narrow the issue down (if it's the client or the server, would be a good first step). Then check all the data step by step in the code to see where it goes off the rails. – M. Eriksson Oct 27 '22 at 12:58

1 Answers1

0

Found the issue while checking it in Postman. Reason for showing string(22672) from api response is i was getting some Html codes along with the JSON response which i couldn't see in browser.

I fixed issue in api side

VinoCoder
  • 1,133
  • 5
  • 22
  • 45