0

I can´t figure out what is wrong with my code, I have a JSON which is structured this way

{
   "data":[
      {
         "B00PM7UJMA":[
            {
               "Verkaufer":"Je Sens le Bonheur",
               "Stock":71.0,
               "Stock: Sold":7.0,
               "Stock: Sold 30 Days":7.0,
               "FBA":"no"
            },
            {
               "Verkaufer":"Parfumea",
               "Stock":2.0,
               "Stock: Sold":"NaN",
               "Stock: Sold 30 Days":"NaN",
               "FBA":"no"
            }
         ],
         "Insgesamt":[
            73.0
         ]
      }
   ]
}

I am trying to access the "Data" but it won´t work at all. I can´t figure out what the problem is, maybe there is something going on with my JSON?

This is my PHP

    $str = file_get_contents($row["URL"]);
    $json = json_encode($str); // decode the JSON into an associative array
    $jsondecode = json_decode($json, true);

    print_r($jsondecode['data']); // doesn´t work at all
    print_r($jsondecode->data); // doesn´t work either

Does someone know what the problem could be? I worked a lot with JSON via PHP. I bet the solution is really easy, but I just need someone others opinion on this one.

Thank you

4
  • $json = json_encode($str); // decode the JSON into an associative array It isn't decoding bro. You are basically double encoding the string. Commented Apr 25, 2021 at 10:59
  • You are encoding before decoding, which doesn't make sense. Just drop $json = json_encode($str);, your string is already JSON. Commented Apr 25, 2021 at 10:59
  • @nice_dev I tried as well, thank you, but somehow it just turns out in a blank response when I try it this way Commented Apr 25, 2021 at 11:12
  • 1
    @derd1199 Closing as your issue is not reproducible. See sandbox.onlinephpfunctions.com/code/… Commented Apr 25, 2021 at 11:25

2 Answers 2

4

I arranged your code a bit. You are encoding first and after that decoding again. I think it is redundant.

If you give $str directly to decode, it will work.

$jsondecode = json_decode($str);
print_r($jsondecode->data);

Also if you want to take the output as an array, you should add true to json_decode and print with square brackets like that

$jsondecode = json_decode($str, true);
print_r($jsondecode['data']);
Sign up to request clarification or add additional context in comments.

4 Comments

I tried it as well, somehow it just gives me a blank response
Thank you @Gunobaba I tried to add the boolean true and access the data with square brackets but it somehow still just return an empty response
Then I think json may not be taken clearly from the URL. because I tried with assigning the above json code to $str variable and give it to decode function.
When I print the whole json print_r($jsondecode) it will return the full json. As soon as I am trying to parse it by accessing 'data' it will return a blank one. I am getting the same json from a .json link I store on my server. Idk what is going on here
0
$str = '{"data":[{"B00PM7UJMA":[{"Verkaufer":"Je Sens le Bonheur","Stock":71.0,"Stock: Sold":7.0,"Stock: Sold 30 Days":7.0,"FBA":"no"},{"Verkaufer":"Parfumea","Stock":2.0,"Stock: Sold":"NaN","Stock: Sold 30 Days":"NaN","FBA":"no"}],"Insgesamt":[73.0]}]}';

$json = json_decode($str, false);

print_r($json->data);
print_r($json->data[0]);
print_r($json->data[0]->B00PM7UJMA);
print_r($json->data[0]->B00PM7UJMA[0]);
print_r($json->data[0]->B00PM7UJMA[0]->Verkaufer);

The final line prints: "Je Sens le Bonheur"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.