Skip to content
Advertisement

Parsing nested json data to extract a certain value

so I have a nested JSON response form a JSON request I’m having trouble parsing the nested JSON data to get a certain value I have commented out on the data in need ( “link”: “http://i.imgur.com/DnjD527h.gif”) need to extract data in two places see the JSON data for comments. Since I don’t have a good understanding of array need some help figuring out the issue. thank you

Complete JSON data

{
    "data": {
        "name": "gaming",
        "display_name": "gaming",
        "followers": 4264452,
        "total_items": 288150,
        "following": false,
        "is_whitelisted": true,
        "background_hash": "nrFCOUB",
        "thumbnail_hash": null,
        "accent": "2B1A5A",
        "background_is_animated": false,
        "thumbnail_is_animated": false,
        "is_promoted": false,
        "description": "combat and adventure",
        "logo_hash": null,
        "logo_destination_url": null,
        "description_annotations": {},
        "items": [
            {
                "id": "DnjD527",
                "title": "Stuck between a rock and a hard place.",
                "description": "Sauce: https://old.reddit.com/user/Sumit316",
                "datetime": 1596212992,
                "type": "image/gif",
                "animated": true,
                "width": 728,
                "height": 408,
                "size": 53069724,
                "views": 50417,
                "bandwidth": 2675616274908,
                "vote": null,
                "favorite": false,
                "nsfw": false,
                "section": "",
                "account_url": "OctopussSevenTwo",
                "account_id": 23603129,
                "is_ad": false,
                "in_most_viral": false,
                "has_sound": false,
                "tags": [
                    {
                        "name": "funny",
                        "display_name": "funny",
                        "followers": 4554003,
                        "total_items": 2683220,
                        "following": false,
                        "is_whitelisted": false,
                        "background_hash": "9r1qCDq",
                        "thumbnail_hash": null,
                        "accent": "633875",
                        "background_is_animated": false,
                        "thumbnail_is_animated": false,
                        "is_promoted": false,
                        "description": "LOLs, ROFLs, LMAOs",
                        "logo_hash": null,
                        "logo_destination_url": null,
                        "description_annotations": {}
                    },
                    {
                        "name": "gaming",
                        "display_name": "gaming",
                        "followers": 4264392,
                        "total_items": 288149,
                        "following": false,
                        "is_whitelisted": false,
                        "background_hash": "nrFCOUB",
                        "thumbnail_hash": null,
                        "accent": "2B1A5A",
                        "background_is_animated": false,
                        "thumbnail_is_animated": false,
                        "is_promoted": false,
                        "description": "combat and adventure",
                        "logo_hash": null,
                        "logo_destination_url": null,
                        "description_annotations": {}
                    },
                    {
                        "name": "reaction",
                        "display_name": "reaction",
                        "followers": 55610,
                        "total_items": 220184,
                        "following": false,
                        "is_whitelisted": false,
                        "background_hash": "UQOCajR",
                        "thumbnail_hash": null,
                        "accent": "7B74BF",
                        "background_is_animated": false,
                        "thumbnail_is_animated": false,
                        "is_promoted": false,
                        "description": "",
                        "logo_hash": null,
                        "logo_destination_url": null,
                        "description_annotations": {}
                    },
                    {
                        "name": "he_ded",
                        "display_name": "he ded",
                        "followers": 2822,
                        "total_items": 4594,
                        "following": false,
                        "is_whitelisted": false,
                        "background_hash": "QL9pTeJ",
                        "thumbnail_hash": null,
                        "accent": "159559",
                        "background_is_animated": false,
                        "thumbnail_is_animated": false,
                        "is_promoted": false,
                        "description": "",
                        "logo_hash": null,
                        "logo_destination_url": null,
                        "description_annotations": {}
                    }
                ],
                "ad_type": 0,
                "ad_url": "",
                "edited": 0,
                "in_gallery": true,
                "topic": "No Topic",
                "topic_id": 29,
               "link": "http://i.imgur.com/DnjD527h.gif",/****************Need this data************/
                "mp4": "https://i.imgur.com/DnjD527.mp4",
                "gifv": "https://i.imgur.com/DnjD527.gifv",
                "hls": "https://i.imgur.com/DnjD527.m3u8",
                "mp4_size": 8025341,
                "looping": true,
                "processing": {
                    "status": "completed"
                },
                "ad_config": {
                    "safeFlags": [
                        "in_gallery",
                        "gallery"
                    ],
                    "highRiskFlags": [],
                    "unsafeFlags": [
                        "sixth_mod_unsafe"
                    ],
                    "wallUnsafeFlags": [],
                    "showsAds": false
                },
                "comment_count": 14,
                "favorite_count": 37,
                "ups": 125,
                "downs": 7,
                "points": 118,
                "score": 142,
                "is_album": false
            },
            {
                "id": "sHoHzEI",
                "title": "Almost a year in development. Here's some gameplay for you!",
                "description": null,
                "datetime": 1596208860,
                "cover": "RS1a4kB",
                "cover_width": 960,
                "cover_height": 540,
                "account_url": "CHILLNPLAY",
                "account_id": 62468833,
                "privacy": "hidden",
                "layout": "blog",
                "views": 2497,
                "link": "https://imgur.com/a/sHoHzEI",
                "ups": 105,
                "downs": 2,
                "points": 103,
                "score": 104,
                "is_album": true,
                "vote": null,
                "favorite": false,
                "nsfw": false,
                "section": "",
                "comment_count": 6,
                "favorite_count": 29,
                "topic": "No Topic",
                "topic_id": 29,
                "images_count": 1,
                "in_gallery": true,
                "is_ad": false,
                "tags": [
                    {
                        "name": "gaming",
                        "display_name": "gaming",
                        "followers": 4264431,
                        "total_items": 288150,
                        "following": false,
                        "is_whitelisted": false,
                        "background_hash": "nrFCOUB",
                        "thumbnail_hash": null,
                        "accent": "2B1A5A",
                        "background_is_animated": false,
                        "thumbnail_is_animated": false,
                        "is_promoted": false,
                        "description": "combat and adventure",
                        "logo_hash": null,
                        "logo_destination_url": null,
                        "description_annotations": {}
                    },
                    {
                        "name": "video_games",
                        "display_name": "video games",
                        "followers": 61840,
                        "total_items": 47453,
                        "following": false,
                        "is_whitelisted": false,
                        "background_hash": "XhtRLrV",
                        "thumbnail_hash": "XhtRLrV",
                        "accent": "0093BF",
                        "background_is_animated": false,
                        "thumbnail_is_animated": false,
                        "is_promoted": false,
                        "description": "",
                        "logo_hash": null,
                        "logo_destination_url": null,
                        "description_annotations": {}
                    },
                    {
                        "name": "games",
                        "display_name": "games",
                        "followers": 7735,
                        "total_items": 13466,
                        "following": false,
                        "is_whitelisted": false,
                        "background_hash": "yWpvDy6",
                        "thumbnail_hash": null,
                        "accent": null,
                        "background_is_animated": false,
                        "thumbnail_is_animated": false,
                        "is_promoted": false,
                        "description": "",
                        "logo_hash": null,
                        "logo_destination_url": null,
                        "description_annotations": {}
                    },
                    {
                        "name": "gamedev",
                        "display_name": "gamedev",
                        "followers": 3840,
                        "total_items": 10997,
                        "following": false,
                        "is_whitelisted": false,
                        "background_hash": "MEWR7Ga",
                        "thumbnail_hash": null,
                        "accent": null,
                        "background_is_animated": false,
                        "thumbnail_is_animated": false,
                        "is_promoted": false,
                        "description": "",
                        "logo_hash": null,
                        "logo_destination_url": null,
                        "description_annotations": {}
                    },
                    {
                        "name": "indiedev",
                        "display_name": "indiedev",
                        "followers": 1232,
                        "total_items": 4319,
                        "following": false,
                        "is_whitelisted": false,
                        "background_hash": "A3E5z2R",
                        "thumbnail_hash": null,
                        "accent": "24565E",
                        "background_is_animated": false,
                        "thumbnail_is_animated": false,
                        "is_promoted": false,
                        "description": "",
                        "logo_hash": null,
                        "logo_destination_url": null,
                        "description_annotations": {}
                    }
                ],
                "ad_type": 0,
                "ad_url": "",
                "in_most_viral": false,
                "include_album_ads": false,
                "images": [
                    {
                        "id": "RS1a4kB",
                        "title": null,
                        "description": "Join the beta test:nhttps://gamejolt.com/games/PAPER_TANKS/474788nhttps://chillnplay.itch.io/paper-tanksnnFollow dev: nhttps://twitter.com/PAPERTANKS2nhttps://store.steampowered.com/app/1213020/PAPER_TANKS",
                        "datetime": 1596207639,
                        "type": "video/mp4",
                        "animated": true,
                        "width": 960,
                        "height": 540,
                        "size": 16796636,
                        "views": 61678,
                        "bandwidth": 1035982915208,
                        "vote": null,
                        "favorite": false,
                        "nsfw": null,
                        "section": null,
                        "account_url": null,
                        "account_id": null,
                        "is_ad": false,
                        "in_most_viral": false,
                        "has_sound": false,
                        "tags": [],
                        "ad_type": 0,
                        "ad_url": "",
                        "edited": "0",
                        "in_gallery": false,
                        "link": "https://i.imgur.com/RS1a4kB.mp4",/****************Need this data************/
                        "mp4_size": 16796636,
                        "mp4": "https://i.imgur.com/RS1a4kB.mp4",
                        "gifv": "https://i.imgur.com/RS1a4kB.gifv",
                        "hls": "https://i.imgur.com/RS1a4kB.m3u8",
                        "processing": {
                            "status": "completed"
                        },
                        "comment_count": null,
                        "favorite_count": null,
                        "ups": null,
                        "downs": null,
                        "points": null,
                        "score": null
                    }
                ],
                "ad_config": {
                    "safeFlags": [
                        "in_gallery",
                        "sixth_mod_safe",
                        "album"
                    ],
                    "highRiskFlags": [],
                    "unsafeFlags": [],
                    "wallUnsafeFlags": [],
                    "showsAds": true
                }
            }
            
        ]
        }
    "success": true,
    "status": 200
    
    }

Currently this what im trying

$gifs=file_get_contents(__DIR__ ."/backend_web/Json/tags/test.txt");
$local=$gifs;

 $decoded=json_decode($gifs,true);


//print_r($decoded);

foreach ($decoded as $y) {
foreach ($y->images as $z) {
    $data = $z->link 
}
}

but getting this error . Notice: Trying to get property ‘link’ of non-object in C:xampphtdocstesttest.php on line 29 Any help would be much appreciated

Advertisement

Answer

Better work with objects than arrays:

 $decoded=json_decode($gifs);

Then use the following loop:

foreach ($decoded->data->items as $y) {
   if (isset($y->images)) {
      foreach ($y->images as $x) {
         $data = $x->link;
      }
   }
}

Some objects have no “images” property, that’s why you get the error. It’s eliminated by the isset function.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement