PHP Json Values from Keys not being parsed from External API Call - php
I am trying to make an external API call to fetch JSON data and return it within my web app.
Here is what the data looks like from the API call:
JSON returned from URL:
{
"results":[
{
"name":"Company1",
"ProviderName": "ProviderName1",
"ProviderLogo": "/images/some_image1.jpg",
"GetURL": "/some_url_path",
"Costs": 10000.50
},{
"name":"Company2",
"ProviderName": "ProviderName2",
"ProviderLogo": "/images/some_image2.jpg",
"applyURL": "/some_url_path",
"Costs": 12000.50
}]
}
Since I am using PHP, I am using the following code:
$api_data = file_get_contents('MY URL');
// Had to use 'encoded_json()' since 'json_decode()' was initially returning NULL
$encoded_json = json_encode($api_data);
$decoded_json = json_decode($encoded_json);
// Now I want to get the specific values from the JSON result but nothing is being returned
// I tried using something like this but I'm not getting anything
foreach($decoded_json->results as $mydata)
{
echo 'Decoded Name: ' . $mydata->name . "\n";
}
Any help is greatly appreciated!
EDIT:
So this what was actually happening and I'm going to apologize to everyone in advance right now because I didn't include what was actually throwing the error off in the first place!
I used JSON Lint and put all of the API Json data that I was receiving and it showed me where the error was occurring.
Error:
"SomeCostUpper": $some_variable.rawUpperCost
I tested out my PHP code using valid JSON and was able to return the results without a problem.
Everyone, I'm really sorry for the confusion but thanks for your efforts!
$api_data = file_get_contents('MY URL');
You now have a string containing the data in that URL.
// Had to use 'encoded_json()' since 'json_decode()' was initially returning NULL
If json_decode returns NULL, then the string you had does not contain valid JSON.
$encoded_json = json_encode($api_data);
You now have a string representation of a string containing the data from the URL.
$decoded_json = json_decode($encoded_json);
And now you have reversed that, so you have your original string back.
foreach($decoded_json->results as $mydata)
It is a string. You can't do that.
You need to fix the data you are getting from MY URL.
Use a tool like JSON Lint, which will give you an error like:
Error: Parse error on line 7:
..."Costs": 10000.50, }, { "name": "Comp
----------------------^
Expecting 'STRING', got '}'
You have a rogue comma after the last entry in your object.
This is likely caused by generating JSON by mashing together strings or by writing it by hand. Avoid that. Use tools and libraries designed to generate JSON.
I don't know from which website you get that JSON data, but it isn't valid JSON, that is why json_decode() initially returned NULL. There are 2 commas (each costs value has one) that shouldn't be there.
You "fixed" that by using first json_encode() and then json_decode() on the JSON data returned from the API call. You might think that that fixed your problem but no, if you take a look at what is inside $decoded_json you will see that there isn't an object or an array which you can use.
That means that when you try to do
foreach($decoded_json->results as $mydata)
{
echo 'Decoded Name: ' . $mydata->name . "\n";
}
It fails because it cannot iterate over the 'string' inside $decoded_json.
If you had turned on error reporting you would have seen the following errors:
E_NOTICE : type 8 -- Trying to get property of non-object -- at line n
E_WARNING : type 2 -- Invalid argument supplied for foreach() -- at line n
the json is ok, you don't need to encode and then decode again...
just do:
$api_data = file_get_contents('MY URL');
$decoded_json = json_decode($api_data );
foreach($decoded_json->results as $mydata)
{
echo 'Decoded Name: ' . $mydata->name . "\n";
}
Your JSON data is not valid. Here is the valid JSON structure.
Remove the ',' from the last elemp "Costs": "10000.50" and use double quote for the value as "10000.50".
Related
parsing JSON in php using angularJS object
I am trying to pass JSON object from angular to php like this : <?php $placeBean = "{{masterCtrl.getLastplace();}}"; //VAR DUMP IS -> string(30) "{"id":1841,"name":"Milano","areaCode":"MI","latitude":0,"longitude":0}" $json = '{"id":1841,"name":"Milano","areaCode":"MI","latitude":0,"longitude":0}'; //VAR DUMP IS -> string(70) "{"id":1841,"name":"Milano","areaCode":"MI","latitude":0,"longitude":0}" ?> Then I need to parse the json in $placeBean, because it is not yet a json in php, so I use <?php $manage = json_decode($placeBean); ?> And this is the problem... because it doesn't work, in fact the result is an empty value: instead, if I do the same thing with $json( instead of $placeBean), the code works perfectly. I Think the issue is some missing chars, which are placed in$json, and not in $placeBean. Those are the echo of the variables ECHO OF JSON ->{"id":1841,"name":"Milano","areaCode":"MI","latitude":0,"longitude":0} ECHO OF placeBean -> placeBean{"id":1841,"name":"Milano","areaCode":"MI","latitude":0,"longitude":0} I Also tried to use json_last_error, which gave me this : - Syntax error, malformed
Well, the first one is not a valid JSON object, for this PHP refuses to parse it. When you run it though AngularJS it will actually use its own mechanism and replaces the string with the data returned by the masterCtrl.getLastplace() call.
Get json string elements
I am using a astrology API which sends an json string as response to the query. But i have been trying for long to convert it into json and retrieve different items from the response string. Here is the example of the response: {"ashtakoota":{"status":true,"received_points":26},"manglik":{"status":true,"male_percentage":13.25,"female_percentage":13.75},"rajju_dosha":{"status":false},"vedha_dosha":{"status":false},"conclusion":{"match_report":"Marriage between the prospective bride and groom is highly recommended. The couple would have a long-lasting relationship, which would be filled with happiness and affluence."}} I am using php script so i tried the following code : $json = json_decode($res1, true); TRY 1 --> echo array_values($json[1]); TRY 2 --> echo $json.ashtakoota.status; TRY 3 --> echo $res1.ashtakoota.status; But the output is always blank. I doubt that $json is empty or the json response is not perfectly json.
PHP uses string keys for its arrays, which is what json_decode(...) returns. As such, you need to access them as: echo $json['ashtakoota']['status']; Which should then output true for your example JSON input.
The true parameter on json_decode will cause it to return an array, not an object. Your syntax for objects is also incorrect, it's not a dot, but rather -> that you need. $json = json_decode($res1); echo $json->ashtakoota->status;
php json_decode error quoted object property name expected
I try to create a script that decoding a simple JSON string in PHP and I get the following error: quoted object property name expected The string I try to decode is the following : {"values":[{"url":"http://www.google.com","matches":"http|www|google|com"},{"url":"http://www.yahoo.com","matches":"http|www|yahoo|com"}]} and the code I use to decoded is the following: json_decode( $json_string ); I also have try to validate my json string in some online json validators, and the string seems to be fine. Can someone please help me ? Do you think the problem exists because of the double quotes ? Update #1 Definetelly was a debuging issue. I place my experience here just to help other devs may come accross the same issue in the feature: The problem was that my variable that came with the json string was html encoded so instead of the following string : {"values":[{"url":"http://www.google.com","matches":"http|www|google|com"},{"url":"http://www.yahoo.com","matches":"http|www|yahoo|com"}]} my variable came with the following string inside : {"values":[{"url":"http://www.google.com","matches":"http|www|google|com"},{"url":"http://www.yahoo.com","matches":"http|www|yahoo|com"}]} The mistake by my side was that I used the print_r method instead of the var_dump . This had as a result to print out the " as " in my page .
The json string is valid, and it works. You can add true for the second parameter of json_decode, and you get back an array. Try the following: $json_string = '{"values":[{"url":"http://www.google.com","matches":"http|www|google|com"},{"url":"http://www.yahoo.com","matches":"http|www|yahoo|com"}]}'; var_dump(json_decode($json_string, true)); It works for me.
Extracting value from JSON data in PHP
I have extracted data from another website which is in JSON format - here is what I extracted: http://sub7legends.net/crawler.php I need to get the value of kills and deaths from this. I tried json_decode() in php of this data but when I var_dump() the result it just says "NULL". What can I do to extract the required data?
The link doesn't work for me. But if you have a normal json just like this one: {"kills": "5", "deaths": "3"} Then the json_decode() method should work. Here would be a sample code snippet: $json_you_got_from_the_server = '{"kills": "5", "deaths": "3"}'; $result = json_decode($json_you_got_from_the_server); $kills = $result->kills; $deaths = $result->deaths; echo "You have ".$kills." kills and ".$deaths." deaths."; I haven't tried it, but it should work. If it doesn't, then please comment and I will try something else.
the current json contents are not valid, try validating it first. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.
That json in the link you have pasted has no proper ending scroll to the end and you will see. If this is the same thing that you wanted to parse then it will always return null.
PHP says that this json is invalid because "object property name separator ':' expected", can anyone see why?
What's wrong with this json: { "items" : [ {"timestamp":"1372964400","value":"1","nid":"545500","platform":"-1"},{"timestamp":"1373666400","value":"2","nid":"593141","platform":"-1"},{"timestamp":"1371398400","value":"1","nid":"34872","platform":"-1"},{"timestamp":"1374238800","value":"2","nid":"59251","platform":"-1"},{"timestamp":"1371902400","value":"1","nid":"79534","platform":"-1"},{"timestamp":"1373425200","value":"1","nid":"583657","platform":"-1"},{"timestamp":"1376251200","value":"4","nid":"701275","platform":"-1"},{"timestamp":"1371330000","value":"1","nid":"59251","platform":"-1"},{"timestamp":"1372183200","value":"1","nid":"81447","platform":"-1"},{"timestamp":"1372348800","value":"2","nid":"83560","platform":"-1"},{"timestamp":"1373162400","value":"1","nid":"583657","platform":"-1"},{"timestamp":"1374174000","value":"2","nid":"607389","platform":"-1"},{"timestamp":"1371834000","value":"2","nid":"79534","platform":"-1"},{"timestamp":"1372723200","value":"1","nid":"190555","platform":"-1"},{"timestamp":"1373385600","value":"2","nid":"590502","platform":"-1"},{"timestamp":"1375855200","value":"1","nid":"697831","platform":"-1"},{"timestamp":"1372312800","value":"1","nid":"81447","platform":"-1"},{"timestamp":"1373047200","value":"2","nid":"545517","platform":"-1"},{"timestamp":"1373929200","value":"3","nid":"605563","platform":"-1"},{"timestamp":"1375462800","value":"1","nid":"647466","platform":"-1"},{"timestamp":"1376434800","value":"5","nid":"704771","platform":"-1"},{"timestamp":"1371722400","value":"1","nid":"79534","platform":"-1"},{"timestamp":"1372240800","value":"1","nid":"82257","platform":"-1"},{"timestamp":"1372687200","value":"1","nid":"190555","platform":"-1"},{"timestamp":"1373367600","value":"1","nid":"590502","platform":"-1"},{"timestamp":"1374634800","value":"1","nid":"610377","platform":"-1"},{"timestamp":"1375812000","value":"1","nid":"697482","platform":"-1"},{"timestamp":"1372129200","value":"1","nid":"81157","platform":"-1"},{"timestamp":"1372287600","value":"1","nid":"79568","platform":"-1"},{"timestamp":"1372989600","value":"1","nid":"40191","platform":"-1"},{"timestamp":"1373788800","value":"1","nid":"545500","platform":"-1"},{"timestamp":"1375376400","value":"1","nid":"646893","platform":"-1"},{"timestamp":"1376420400","value":"1","nid":"704624","platform":"-1"},{"timestamp":"1371423600","value":"1","nid":"59251","platform":"-1"},{"timestamp":"1372230000","value":"7","nid":"81447","platform":"-1"},{"timestamp":"1372413600","value":"1","nid":"118275","platform":"-1"},{"timestamp":"1373306400","value":"5","nid":"590502","platform":"-1"},{"timestamp":"1374264000","value":"1","nid":"59251","platform":"-1"},{"timestamp":"1371960000","value":"1","nid":"79568","platform":"-1"},{"timestamp":"1372924800","value":"1","nid":"82456","platform":"-1"},{"timestamp":"1373558400","value":"3","nid":"590502","platform":"-1"},{"timestamp":"1375092000","value":"1","nid":"545500","platform":"-1"},{"timestamp":"1376280000","value":"1","nid":"701529","platform":"-1"},{"timestamp":"1370044800","value":"1","nid":"68566","platform":"-1"},{"timestamp":"1371380400","value":"1","nid":"59251","platform":"-1"},{"timestamp":"1372183200","value":"3","nid":"81457","platform":"-1"},{"timestamp":"1372352400","value":"1","nid":"83613","platform":"-1"},{"timestamp":"1373194800","value":"1","nid":"59251","platform":"-1"},{"timestamp":"1375689600","value":"1","nid":"687547","platform":"-1"},{"timestamp":"1371902400","value":"2","nid":"59251","platform":"-1"},{"timestamp":"1372762800","value":"1","nid":"212931","platform":"-1"},{"timestamp":"1375977600","value":"1","nid":"545500","platform":"-1"},{"timestamp":"1371322800","value":"2","nid":"59251","platform":"-1"},{"timestamp":"1372179600","value":"1","nid":"81457","platform":"-1"},{"timestamp":"1372345200","value":"2","nid":"83560","platform":"-1"},{"timestamp":"1373068800","value":"1","nid":"545517","platform":"-1"},{"timestamp":"1374163200","value":"1","nid":"607389","platform":"-1"},{"timestamp":"1375470000","value":"1","nid":"647526","platform":"-1"},{"timestamp":"1376460000","value":"1","nid":"705149","platform":"-1"},{"timestamp":"1370278800","value":"1","nid":"57508","platform":"-1"},{"timestamp":"1371765600","value":"2","nid":"79568","platform":"-1"},{"timestamp":"1372690800","value":"1","nid":"212147","platform":"-1"},{"timestamp":"1373382000","value":"9","nid":"590502","platform":"-1"},{"timestamp":"1374674400","value":"1","nid":"545500","platform":"-1"},{"timestamp":"1375819200","value":"1","nid":"697482","platform":"-1"},{"timestamp":"1371132000","value":"1","nid":"76647","platform":"-1"},{"timestamp":"1372143600","value":"1","nid":"81209","platform":"-1"},{"timestamp":"1372302000","value":"1","nid":"59251","platform":"-1"},{"timestamp":"1373036400","value":"1","nid":"545500","platform":"-1"},{"timestamp":"1373886000","value":"1","nid":"59251","platform":"-1"},{"timestamp":"1375380000","value":"1","nid":"646893","platform":"-1"},{"timestamp":"1376427600","value":"1","nid":"704735","platform":"-1"},{"timestamp":"1370260800","value":"3","nid":"68984","platform":"-1"},{"timestamp":"1372240800","value":"1","nid":"82162","platform":"-1"},{"timestamp":"1372428000","value":"1","nid":"154915","platform":"-1"},{"timestamp":"1373364000","value":"6","nid":"590502","platform":"-1"},{"timestamp":"1374606000","value":"1","nid":"610377","platform":"-1"}] } when running json_decode on this string php returns null. i've checked with every online php json_decoder and they decode it succesfully. i've also tried with json_decode($json, TRUE); but the call also returns NULL i'm on php version PHP 5.5.1-2+debphp.org~precise+2 I've checked what json_last_error_msg() has to say and it returned: object property name separator ':' expected
This works fine for me under 5.4.3 <?php $json = '{ "items" : [ {"timestamp":"1372964400","value":"1","nid":"545500","platform":"-1"},{"timestamp":"1373666400","value":"2","nid":"593141","platform":"-1"},{"timestamp":"1371398400","value":"1","nid":"34872","platform":"-1"},{"timestamp":"1374238800","value":"2","nid":"59251","platform":"-1"},{"timestamp":"1371902400","value":"1","nid":"79534","platform":"-1"},{"timestamp":"1373425200","value":"1","nid":"583657","platform":"-1"},{"timestamp":"1376251200","value":"4","nid":"701275","platform":"-1"},{"timestamp":"1371330000","value":"1","nid":"59251","platform":"-1"},{"timestamp":"1372183200","value":"1","nid":"81447","platform":"-1"},{"timestamp":"1372348800","value":"2","nid":"83560","platform":"-1"},{"timestamp":"1373162400","value":"1","nid":"583657","platform":"-1"},{"timestamp":"1374174000","value":"2","nid":"607389","platform":"-1"},{"timestamp":"1371834000","value":"2","nid":"79534","platform":"-1"},{"timestamp":"1372723200","value":"1","nid":"190555","platform":"-1"},{"timestamp":"1373385600","value":"2","nid":"590502","platform":"-1"},{"timestamp":"1375855200","value":"1","nid":"697831","platform":"-1"},{"timestamp":"1372312800","value":"1","nid":"81447","platform":"-1"},{"timestamp":"1373047200","value":"2","nid":"545517","platform":"-1"},{"timestamp":"1373929200","value":"3","nid":"605563","platform":"-1"},{"timestamp":"1375462800","value":"1","nid":"647466","platform":"-1"},{"timestamp":"1376434800","value":"5","nid":"704771","platform":"-1"},{"timestamp":"1371722400","value":"1","nid":"79534","platform":"-1"},{"timestamp":"1372240800","value":"1","nid":"82257","platform":"-1"},{"timestamp":"1372687200","value":"1","nid":"190555","platform":"-1"},{"timestamp":"1373367600","value":"1","nid":"590502","platform":"-1"},{"timestamp":"1374634800","value":"1","nid":"610377","platform":"-1"},{"timestamp":"1375812000","value":"1","nid":"697482","platform":"-1"},{"timestamp":"1372129200","value":"1","nid":"81157","platform":"-1"},{"timestamp":"1372287600","value":"1","nid":"79568","platform":"-1"},{"timestamp":"1372989600","value":"1","nid":"40191","platform":"-1"},{"timestamp":"1373788800","value":"1","nid":"545500","platform":"-1"},{"timestamp":"1375376400","value":"1","nid":"646893","platform":"-1"},{"timestamp":"1376420400","value":"1","nid":"704624","platform":"-1"},{"timestamp":"1371423600","value":"1","nid":"59251","platform":"-1"},{"timestamp":"1372230000","value":"7","nid":"81447","platform":"-1"},{"timestamp":"1372413600","value":"1","nid":"118275","platform":"-1"},{"timestamp":"1373306400","value":"5","nid":"590502","platform":"-1"},{"timestamp":"1374264000","value":"1","nid":"59251","platform":"-1"},{"timestamp":"1371960000","value":"1","nid":"79568","platform":"-1"},{"timestamp":"1372924800","value":"1","nid":"82456","platform":"-1"},{"timestamp":"1373558400","value":"3","nid":"590502","platform":"-1"},{"timestamp":"1375092000","value":"1","nid":"545500","platform":"-1"},{"timestamp":"1376280000","value":"1","nid":"701529","platform":"-1"},{"timestamp":"1370044800","value":"1","nid":"68566","platform":"-1"},{"timestamp":"1371380400","value":"1","nid":"59251","platform":"-1"},{"timestamp":"1372183200","value":"3","nid":"81457","platform":"-1"},{"timestamp":"1372352400","value":"1","nid":"83613","platform":"-1"},{"timestamp":"1373194800","value":"1","nid":"59251","platform":"-1"},{"timestamp":"1375689600","value":"1","nid":"687547","platform":"-1"},{"timestamp":"1371902400","value":"2","nid":"59251","platform":"-1"},{"timestamp":"1372762800","value":"1","nid":"212931","platform":"-1"},{"timestamp":"1375977600","value":"1","nid":"545500","platform":"-1"},{"timestamp":"1371322800","value":"2","nid":"59251","platform":"-1"},{"timestamp":"1372179600","value":"1","nid":"81457","platform":"-1"},{"timestamp":"1372345200","value":"2","nid":"83560","platform":"-1"},{"timestamp":"1373068800","value":"1","nid":"545517","platform":"-1"},{"timestamp":"1374163200","value":"1","nid":"607389","platform":"-1"},{"timestamp":"1375470000","value":"1","nid":"647526","platform":"-1"},{"timestamp":"1376460000","value":"1","nid":"705149","platform":"-1"},{"timestamp":"1370278800","value":"1","nid":"57508","platform":"-1"},{"timestamp":"1371765600","value":"2","nid":"79568","platform":"-1"},{"timestamp":"1372690800","value":"1","nid":"212147","platform":"-1"},{"timestamp":"1373382000","value":"9","nid":"590502","platform":"-1"},{"timestamp":"1374674400","value":"1","nid":"545500","platform":"-1"},{"timestamp":"1375819200","value":"1","nid":"697482","platform":"-1"},{"timestamp":"1371132000","value":"1","nid":"76647","platform":"-1"},{"timestamp":"1372143600","value":"1","nid":"81209","platform":"-1"},{"timestamp":"1372302000","value":"1","nid":"59251","platform":"-1"},{"timestamp":"1373036400","value":"1","nid":"545500","platform":"-1"},{"timestamp":"1373886000","value":"1","nid":"59251","platform":"-1"},{"timestamp":"1375380000","value":"1","nid":"646893","platform":"-1"},{"timestamp":"1376427600","value":"1","nid":"704735","platform":"-1"},{"timestamp":"1370260800","value":"3","nid":"68984","platform":"-1"},{"timestamp":"1372240800","value":"1","nid":"82162","platform":"-1"},{"timestamp":"1372428000","value":"1","nid":"154915","platform":"-1"},{"timestamp":"1373364000","value":"6","nid":"590502","platform":"-1"},{"timestamp":"1374606000","value":"1","nid":"610377","platform":"-1"}] }'; $json_object = json_decode($json); var_dump($json_object); Maybe it's your json_decode that's causing the issue. You can pass an extra argument to json_decode that changes the returned value from an object (by default) to an array. $json_array = json_decode($json, true); See json_decode
Either there is a bug in your installation of PHP (you're using a release candidate, so that could be the issue) or there are some kind of illegal characters in the JSON file, which might not be posted here because stack overflow probably deletes weird characters. Try a different version of PHP, or try going over the JSON code with a Hex Editor (I like HedFiend personally, but it only works on a mac). Also try running json_decode() on partial snippets of the string instead of the entire thing.
You can go to this URL and have a nice look # your JSON :) http://jsonviewer.stack.hu/