-2

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!

Jeff P.
  • 2,214
  • 3
  • 15
  • 38
  • They build the json incorrect ... you can contact them? they need to remove the , from the end of costs value ... "Costs": 10000.50, – shushu304 Dec 01 '17 at 16:28
  • @shushu304 That was actually a typo on my part. I edited the JSON data in this post. – Jeff P. Dec 01 '17 at 16:31
  • @JeffP. If that isn't the problem you don't need to encode the JSON before decoding it. If you remove that line and just decode the JSON your script works. – Tom Udding Dec 01 '17 at 16:33
  • so... the json is ok, you don't needt the first $encoded_json = json_encode($api_data); line in your code... just do the $decoded_json = json_decode($api_data); – shushu304 Dec 01 '17 at 16:33

4 Answers4

2
$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.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
1

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

Community
  • 1
  • 1
Tom Udding
  • 2,154
  • 3
  • 19
  • 26
0

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";
}
shushu304
  • 1,403
  • 1
  • 5
  • 15
-2

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".