0

I have JSON which is formatted like example below which is correct way to loop whole file to catch "IWANTTHIS" values? All arrays are on same level in JSON ("some:products"). Just too complicated JSON for me in this afternoon...

{
  "type": "page",
  "pageType": "section",
  "_links": {},
  "_embedded": {
  "somkindof:blocks": [
    {
     "somevalue": 25,
     "_embedded": {
     "some:products": [
     {
      "system": {
      "availability": {},
      "IWANTTHIS": "20284",
      "hhhh": []
      },
....

I tried something like this but I cant catch values formatted like in my example.

$jfo = json_decode($vcurl);
$channel = $jfo->_embedded->[somkindof:blocks];

var_dump $channel;
1
  • Try $channel = $jfo->_embedded->{'somkindof:blocks'} (You may find using it as an array easier, look at json_decode parameter 2 in the PHP docs) Commented Feb 14, 2017 at 15:37

4 Answers 4

2

Things get easier if you decode the JSON into an associative array.

(Even objects will become associative arrays and you can parse the whole object tree with the same syntax).

Then you parse the array with the square bracket syntax.

Note that both "somkindof:blocks" and "some:products" are regular arrays (ordered list of items) so you have to parse each item of them (with a simple foreach)

Said that you may have many "I want this".

<?php

$jfo = json_decode($vcurl, true ); // <-- decode into associative array

foreach( $jfo[ "embedded" ][ "somkindof:blocks" ] as $level_1_item )
{
    foreach( $level_1_item[ "_embedded" ][ "some:products" ] as $level_2_item )
    {
        $iWantThis = $level_2_item[ "IWANTTHIS" ];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Thx this tool: http://jsonselector.com

$channel = ['_embedded']['somkindof:blocks'][0]['_embedded']['some:products'][0]['system']['IWANTTHIS']

Comments

0

Add true as second parameter to json_decode to decode it into an array and not object.

<?php
$json = '{
  "type": "page",
  "pageType": "section",
  "_links": {},
  "_embedded": {
    "somkindof:blocks": [
      {
        "somevalue": 25,
        "_embedded": {
          "some:products": [
            {
              "system": {
                "availability": {},
                "IWANTTHIS": "20284",
                "hhhh": []
              }
            }
          ]
        }
      }
    ]
  }
}';

$jfo = json_decode($json, true);

$channel = $jfo['_embedded']['somkindof:blocks'];
print_r($channel);

Comments

0

All comments have suggested the Array option by providing true to json_decode() function.

However, if you wish to maintain stdClass use curly brackets.

Eg:

$jfo = json_decode($json);

foreach($jfo->_embedded->{'somkindof:blocks'} as $block) {
    echo $block->{'somSomething is wrongue'};
}

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.