Skip to content
Advertisement

Parsing JSON keys and values in PHP

These are the values in JSON

"_id": "5db81ae803f7410018f7c081",
            "hotness": 72793.81406699134,
            "activityHotness": 0.10295588022415446,
            "primaryCategory": "Exchanges",
            "words": 443,
            "similarArticles": [],
            "coins": [
                {
                    "_id": "59cb59e81c073f09e76f614b",
                    "name": "Bitcoin",
                    "slug": "bitcoin",
                    "tradingSymbol": "btc"
                },
                {
                    "_id": "59d21e9b83a0523906a45dc5",
                    "name": "EOS",
                    "slug": "eos",
                    "tradingSymbol": "eos"
                },
                {
                    "_id": "59d21e9b83a0523906a45dbe",
                    "name": "Tether",
                    "slug": "tether",
                    "tradingSymbol": "usdt"
                }
            ],
            "description": "The world’s 5th largest crypto exchange OKEx is planning to launch Tether futures trading, offering a linear futures contract with leverage of up to 100x.nThe world’s 5th largest crypto exchange OKEx is planning to launch Tether ( USDT ) futures trading, offering a linear futures contract with…",
            "publishedAt": "2019-10-29T10:16:00.000Z",
            "title": "OKEx to Launch USDT Futures Trading With Up to 100x Leverage",
            "url": "https://cryptocontrol.io/r/api/article/5db81ae803f7410018f7c081?ref=5d9f090e03f7410018f41938",
            "source": {
                "_id": "59d70be3ef8bf95cc2aa2b4f",
                "name": "CoinTelegraph",
                "url": "https://cointelegraph.com/",
                "favicon": "https://assets.cryptocontrol.io/favicons/59d70be3ef8bf95cc2aa2b4f.png"
            },
            "sourceDomain": "cointelegraph.com",
            "originalImageUrl": "https://images.cointelegraph.com/images/740_IGh0dHBzOi8vczMuY29pbnRlbGVncmFwaC5jb20vc3RvcmFnZS91cGxvYWRzL3ZpZXcvMWY5YTllNWViMGI1NTNhMWJkNWVlYjBhZWNkOTAxYzkuanBn.jpg"
        },

I want to truncate the values from Id to coins and display the values starting from description I am trying to get keys and values from the JSON file in Blockchain array. But I want to truncate some of them. Not getting idea how to do.

I have tried using foreach loop

<?php 
$getJsonData = file_get_contents("sample.json");
$jsonArray = json_decode($getJsonData);
$mainName = "blockchain";

$i = 1;
foreach($jsonArray->$mainName as $row){
    echo "<br>----------record $i start <br><br>";

    foreach($row as $key => $val){
        if(is_object($val)){
            foreach($val as $ky => $v1){
                echo $key.' => '.$ky.': '.$v1;
                echo '<br>';
            }
        }else{
            echo $key.': '.$val;
            echo '<br>';
        } 
    }
    echo "<br>----- record $i end <br><br>";
    $i++;
}
?>

Advertisement

Answer

Since you call json_decode() without the optional second argument, the JSON objects are decoded as PHP objects, not associative arrays. So you can’t use foreach() to loop over the object properties.

Use json_decode($getJsonData, true) and then all the objects will become associative arrays.

Then you can use array_keys() to get all the keys, and remove all the keys between _id and coins.

$keys = array_keys($jsonArray[$mainName][0]);
$id_index = array_search('_id', $keys);
$coins_index = array_search('coins', $keys);
array_splice($keys, $id_index, $coins_index - $id_index + 1); // remove keys from _id to coins
foreach ($jsonArray[$mainName] as $row) {
    foreach ($keys as $key) {
        $val = $row[$key];
        if (is_array($val)) {
            foreach ($val as $k => $v) {
                echo "$key => $k: $v<br>";
            }
        } else {
            echo "$key: $val<br>";
        }
    }
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement