Ad

JSON Decoding: Mixed Types - Not Valid Syntax Error

- 1 answer

This is my example of JSON

            {"tasks" : [
            {
            "id" : "27604_11",
            "quality" : "A4",
            "position" : "183567",
            "profile" : "https:\/\/example.com\/gallery\/profiles\/8764_2.jpg",
            "type" : "image",
            "valid" : "24 days",
            "member" : {
                "carrier" : null,
                "zone" : null,
                "offd" : null },
            "taketime" : "4minutes",
            "listeners" : {
                "count" : 0 },
            "message" : "stay prepared",
            "level" : "High"
            },
            {
            "id" : "27604_12",
            "quality" : "B3",
            "position" : "183569",
            "profile" : "https:\/\/example.com\/gallery\/profiles\/8232_1.jpg",
            "type" : "image",
            "valid" : "11 days",
            "member" : {
                "carrier" : null,
                "zone" : null,
                "offd" : null },
            "taketime" : "7minutes",
            "listeners" : {
                "count" : 2 },
            "message" : "ready",
            "level" : "High"
            },
            {
            "id" : "73754_35",
            "quality" : "A3",
            "position" : "13542",
            "profile" : "https:\/\/example.com\/gallery\/profiles\/1235_5.jpg",
            "type" : "image",
            "valid" : "5 days",
            "member" : {
                "carrier" : null,
                "zone" : null,
                "offd" : null },
            "taketime" : "22minutes",
            "listeners" : {
                "count" : 0 },
            "message" : "Good",
            "level" : "Normale" }
        ],
        "start_poz" : "36774263743"
        }
        {"tasks" : [
            {
            "id" : "27604_11",
            "quality" : "A4",
            "position" : "183567",
            "profile" : "https:\/\/example.com\/gallery\/profiles\/8764_2.jpg",
            "type" : "image",
            "valid" : "24 days",
            "member" : {
                "carrier" : null,
                "zone" : null,
                "offd" : null },
            "taketime" : "4minutes",
            "listeners" : {
                "count" : 0 },
            "message" : "stay prepared",
            "level" : "High"
            },
            {
            "id" : "27604_12",
            "quality" : "B3",
            "position" : "183569",
            "profile" : "https:\/\/example.com\/gallery\/profiles\/8232_1.jpg",
            "type" : "image",
            "valid" : "11 days",
            "member" : {
                "carrier" : null,
                "zone" : null,
                "offd" : null },
            "taketime" : "7minutes",
            "listeners" : {
                "count" : 2 },
            "message" : "ready",
            "level" : "High"
            },
            {
            "id" : "73754_35",
            "quality" : "A3",
            "position" : "13542",
            "profile" : "https:\/\/example.com\/gallery\/profiles\/1235_5.jpg",
            "type" : "image",
            "valid" : "5 days",
            "member" : {
                "carrier" : null,
                "zone" : null,
                "offd" : null },
            "taketime" : "22minutes",
            "listeners" : {
                "count" : 0 },
            "message" : "Good",
            "level" : "Normale" }
        ],
        "start_poz" : "36774263743"
        }
        {"tasks" : [
            {
            "id" : "27604_11",
            "quality" : "A4",
            "position" : "183567",
            "profile" : "https:\/\/example.com\/gallery\/profiles\/8764_2.jpg",
            "type" : "image",
            "valid" : "24 days",
            "member" : {
                "carrier" : null,
                "zone" : null,
                "offd" : null },
            "taketime" : "4minutes",
            "listeners" : {
                "count" : 0 },
            "message" : "stay prepared",
            "level" : "High"
            },
            {
            "id" : "27604_12",
            "quality" : "B3",
            "position" : "183569",
            "profile" : "https:\/\/example.com\/gallery\/profiles\/8232_1.jpg",
            "type" : "image",
            "valid" : "11 days",
            "member" : {
                "carrier" : null,
                "zone" : null,
                "offd" : null },
            "taketime" : "7minutes",
            "listeners" : {
                "count" : 2 },
            "message" : "ready",
            "level" : "High"
            },
            {
            "id" : "73754_35",
            "quality" : "A3",
            "position" : "13542",
            "profile" : "https:\/\/example.com\/gallery\/profiles\/1235_5.jpg",
            "type" : "image",
            "valid" : "5 days",
            "member" : {
                "carrier" : null,
                "zone" : null,
                "offd" : null },
            "taketime" : "22minutes",
            "listeners" : {
                "count" : 0 },
            "message" : "Good",
            "level" : "Normale" }
        ],
        "start_poz" : "36774263743"
        }

And this is php:

<?php
$file = file_get_contents("in.txt");
$json = json_decode($file, true);

/** Skipping json_last_error() code.. The error is this: JSON_ERROR_SYNTAX **/

$tasks = $json["tasks"];
foreach ($tasks as $key => $value)
{
    echo "<ul>";
    echo "<li>".$value['id']."</li>";
    echo "<li>".$value['quality']."</li>";
    echo "<li>".$value['position']."</li>";
    echo "<li>".$value['profile']."</li>";
    echo "<li>".$value['type']."</li>";
    echo "<li>".$value['valid']."</li>";
    echo "<li>".$value['member']['carrier']."</li>";
    echo "<li>".$value['member']['zone']."</li>";
    echo "<li>".$value['member']['offd']."</li>";
    echo "<li>".$value['taketime']."</li>";
    echo "<li>".$value['listeners']['count']."</li>";
    echo "<li>".$value['taketime']."</li>";
    echo "<li>".$value['message']."</li>";
    echo "<li>".$value['level']."</li>";
    echo "</ul>";
        }
    ?>

Actually, the error doesn't let to perform the code with JSON. But if you use only 1 part of JSON, like this:

{"tasks" : [
            {
            "id" : "27604_11",
            "quality" : "A4",
            "position" : "183567",
            "profile" : "https:\/\/example.com\/gallery\/profiles\/8764_2.jpg",
            "type" : "image",
            "valid" : "24 days",
            "member" : {
                "carrier" : null,
                "zone" : null,
                "offd" : null },
            "taketime" : "4minutes",
            "listeners" : {
                "count" : 0 },
            "message" : "stay prepared",
            "level" : "High"
            },
            {
            "id" : "27604_12",
            "quality" : "B3",
            "position" : "183569",
            "profile" : "https:\/\/example.com\/gallery\/profiles\/8232_1.jpg",
            "type" : "image",
            "valid" : "11 days",
            "member" : {
                "carrier" : null,
                "zone" : null,
                "offd" : null },
            "taketime" : "7minutes",
            "listeners" : {
                "count" : 2 },
            "message" : "ready",
            "level" : "High"
            },
            {
            "id" : "73754_35",
            "quality" : "A3",
            "position" : "13542",
            "profile" : "https:\/\/example.com\/gallery\/profiles\/1235_5.jpg",
            "type" : "image",
            "valid" : "5 days",
            "member" : {
                "carrier" : null,
                "zone" : null,
                "offd" : null },
            "taketime" : "22minutes",
            "listeners" : {
                "count" : 0 },
            "message" : "Good",
            "level" : "Normale" }
        ],
        "start_poz" : "36774263743"
        }

then it WORKS!

Could you, please, guide me where is my fault with syntax.. and.. maybe you can also help me understand how to get start_poz value with every loop of getting values from tasks Array ?

Thank you very much and Happy New Year! ;)

Ad

Answer

You have THREE separate JSON objects in your input file at the top level. You need to wrap the entire input as an array [ ... ] or parse each one individually.

Ad
source: stackoverflow.com
Ad