1

I am trying to read a json file, which has header, body and footer part and subsequently want to insert in mysql db using PHP code.

I tried to read json file, however the file has few header objects and data object. Data object has many records. I want to read header and data seperately. I tried to read data object, however even print statement didn't work. Please help in parsing the header and data object seperately.

php code

$json = file_get_contents('./f.json');

$json_data = json_decode($json,true);

print_r($json_data);


foreach ($json_data['data'] as $item => $value) {
    print $item['symbol'];
    print ' - ';
    print $item['ltp'];
    print ' - ';
    print '<br>';
}

json file

{

    "noChg":0,
    "adv":10,
    "dec":3,
    "data":[
        {
            "symbol":"TECHM",
            "open":"749.90",
            "high":"749.90",
            "low":"732.05",
            "ltP":"737.00",
            "ptsC":"-6.30",
            "per":"-0.85"
        },
        {
            "symbol":"HAVELLS",
            "open":"752.85",
            "high":"754.90",
            "low":"739.10",
            "ltP":"745.00",
            "ptsC":"-6.50",
            "per":"-0.86"
        },
        {
            "symbol":"NESTLEIND",
            "open":"10,813.00",
            "high":"10,849.90",
            "low":"10,592.30",
            "ltP":"10,682.40",
            "ptsC":"-114.65",
            "per":"-1.06"
        },
        {
            "symbol":"HEXAWARE",
            "open":"356.30",
            "high":"357.45",
            "low":"349.45",
            "ltP":"352.00",
            "ptsC":"-4.15",
            "per":"-1.17"
        },
        {
            "symbol":"ADANIENT",
            "open":"162.35",
            "high":"163.75",
            "low":"151.85",
            "ltP":"159.15",
            "ptsC":"-1.90",
            "per":"-1.18"
        },
        {
            "symbol":"TORNTPHARM",
            "open":"1,554.80",
            "high":"1,566.90",
            "low":"1,511.20",
            "ltP":"1,530.00",
            "ptsC":"-22.20",
            "per":"-1.43"
        },
        {
            "symbol":"BATAINDIA",
            "open":"1,374.90",
            "high":"1,374.90",
            "low":"1,329.00",
            "ltP":"1,345.00",
            "ptsC":"-20.50",
            "per":"-1.50"
        },
        {
            "symbol":"KAJARIACER",
            "open":"616.05",
            "high":"619.00",
            "low":"595.20",
            "ltP":"603.00",
            "ptsC":"-12.80",
            "per":"-2.08"
        },
        {
            "symbol":"RELINFRA",
            "open":"116.40",
            "high":"116.90",
            "low":"111.05",
            "ltP":"111.85",
            "ptsC":"-2.60",
            "per":"-2.27"
        },
        {
            "symbol":"PVR",
            "open":"1,783.80",
            "high":"1,783.80",
            "low":"1,698.85",
            "ltP":"1,716.00",
            "ptsC":"-50.85",
            "per":"-2.88"
        },
        {
            "symbol":"SUNTV",
            "open":"553.00",
            "high":"561.30",
            "low":"512.00",
            "ltP":"531.70",
            "ptsC":"-16.15",
            "per":"-2.95"
        },
        {
            "symbol":"JETAIRWAYS",
            "open":"146.15",
            "high":"152.90",
            "low":"142.35",
            "ltP":"147.70",
            "ptsC":"-7.75",
            "per":"-4.99"
        },
        {
            "symbol":"JUSTDIAL",
            "open":"744.00",
            "high":"744.00",
            "low":"673.35",
            "ltP":"680.60",
            "ptsC":"-66.35",
            "per":"-8.88"
        }
    ],
    "totQty":"14,259.87",
    "totQtyMil":"1,425.99",
    "totValMil":"353.59",
    "time":"May 24, 2019 16:00:00",
    "totVal":"35,358.82" }

I expect to parse and extract values from data objects, but i am getting the following error:

PHP Fatal error: Cannot use object of type stdClass as array in /home/fut/public_html/on.php

2

2 Answers 2

1

I cannot reproduce the error your getting, but there are a few issues with the code you currently have.

You are trying to use $item as the contents of the array instead of $value, $tem is the index of the array and not the contents.

Also as keys are case sensitive - you must use 'ltP' and not 'ltp'...

foreach ($json_data['data'] as $item => $value) {
    print $value['symbol'];
    print ' - ';
    print $value['ltP'];
    print ' - ';
    print '<br>';
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Also how do i read header part of json file in the same code. "noChg":0, "adv":10, "dec":3,
As they are at the top level - $json_data['noChg'] etc.
0

You need to pass second parameter True if you want to have JSON decoded as an array.

<?php
$json = file_get_contents('./f.json');

$json_data = json_decode($json, true);

print_r($json_data);

//to see header
$header = array_keys($json_data['data'][0]);
var_export($header);

$header2 = [];
foreach($json_data as $key => $item) {
    if(!is_array($item)) {
        $header2[$key] = $item;
    }
}
//to see the other header
var_export($header2);

foreach ($json_data['data'] as $item => $value) {
print $item['symbol'];
print ' - ';
print $item['ltp'];
print ' - ';
print '<br>';

}

1 Comment

Thanks. How to read header objects before foreach loop? I need to read both.

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.