3

I have a JSON response from an API in the following format:

string(2228) "{"question":{"type":"single","text":"Have you noticed any insects on you or have you been bitten by any insect (lice, mosquitoes, ticks, bedbugs, etc.)?",
"items":
[
{"id":"p_48","name":"Insect bite","
choices":
[{"id":"present","label":"Yes"},
{"id":"absent","label":"No"},
{"id":"unknown","label":"Don't know"}
]}
],
"extras":{}
},
"conditions":[
{"id":"c_87","name":"Common cold","common_name":"Common cold","probability":0.028},
{"id":"c_10","name":"Gastroenteritis","common_name":"Gastroenteritis","probability":0.0238},
{"id":"c_49","name":"Migraine","common_name":"Migraine","probability":0.0096}
],
"extras":{},"should_stop":false}"

I need to get the question -> text,

id and choices

&& conditions with id, name and probability.

I tried the following:

<?php
$str = file_get_contents("test.json");
//var_dump($str);
$json = json_decode($str, true);
echo '<pre>' . print_r($json, true) . '</pre>';
?>

No output. I also tried the php RecursiveArrayIterator as follows:

$jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($json, TRUE)),
    RecursiveIteratorIterator::SELF_FIRST);

foreach ($jsonIterator as $key => $val) {
    if(is_array($val)) {
        echo "$key:\n";
    } else {
        echo "$key => $val\n";
    }
}

and get error message that:

Passed variable is not an array or object

I am new to php and unable to understand the reason ?? Help solicited from experts.

Update:

I called CURL directly from terminal and found that it was missing the string(2228) "and the trailing " part. So i did explode and substr. Since it is a multi-dimenisonal array i did the recursive array iterator as follows:

$jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($str, TRUE)),
    RecursiveIteratorIterator::SELF_FIRST);

foreach ($jsonIterator as $key => $val) {
    if(is_array($val)) {
        echo "$key:\n";
    } else {
        echo "$key => $val\n";
    }
}

Go the output required. Issue resolved.

6
  • check var_dump on $str on second line, I guess your file_get_contents is not reading your json file or the path is incorrect, is in the same directory where this file is running? Commented Dec 11, 2017 at 14:06
  • @pravindot17 I had checked var_dump earlier. It was working fine. Commented Dec 11, 2017 at 14:08
  • @Pamela you get the NULL array as you have the error in JSON structure. Commented Dec 11, 2017 at 14:20
  • @Nazir Could you please spell out the error. It is coming from an API and i just copied it to a file. Commented Dec 11, 2017 at 14:24
  • That's not valid json up there. Commented Dec 11, 2017 at 15:04

2 Answers 2

1

Try to copy this code in your test file and then get the result:

here is the error in JSON syntax:

"name": "Insect bite",
            "
            choices "

Try bellow code sample

{
    "question": {
        "type": "single",
        "text": "Have you noticed any insects on you or have you been bitten by any insect (lice, mosquitoes, ticks, bedbugs, etc.)?",
        "items": [{
            "id": "p_48",
            "name": "Insect bite",
            "choices": [{
                    "id": "present",
                    "label": "Yes"
                },
                {
                    "id": "absent",
                    "label": "No"
                },
                {
                    "id": "unknown",
                    "label": "Don't know"
                }
            ]
        }],
        "extras": {}
    },
    "conditions": [{
            "id": "c_87",
            "name": "Common cold",
            "common_name": "Common cold",
            "probability": 0.028
        },
        {
            "id": "c_10",
            "name": "Gastroenteritis",
            "common_name": "Gastroenteritis",
            "probability": 0.0238
        },
        {
            "id": "c_49",
            "name": "Migraine",
            "common_name": "Migraine",
            "probability": 0.0096
        }
    ],
    "extras": {},
    "should_stop": false
}
Sign up to request clarification or add additional context in comments.

8 Comments

As informed the result is coming from an external API and i am testing it copying it to a file. Please note that changing the JSON is not the way to go. And using your JSON does not provide CHOICES.
if we test original JSON code from your sample with decoding we get error Unexpected control character found
try json_decode($str); then print json_last_error()); php.net/manual/en/function.json-last-error.php
var_dump($json, $error === JSON_ERROR_UTF8); gives NULL bool(false)
and var_dump(json_last_error(), json_last_error_msg()); gives int(4) string(12) "Syntax error"
|
0

Hope this will help you, see here I am able access your json properties

<?php
$str = file_get_contents("https://api.myjson.com/bins/7gc8n");
$json = json_decode($str, true);

// checkout this I am getting json here
echo '<pre>';
// print_r($json, true);

print_r($json['question']['text']);
print_r($json['conditions']);
echo '</pre>';
?>

1 Comment

I am not getting any result ??

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.