1

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! ;)

4
  • Yeah, but.. sorry, what does it mean: Expecting 'EOF', '}', ',', ']', got '{' as it said there. I'm kinda newbie ))) Commented Jan 1, 2016 at 1:37
  • If you paste your json into the form on that page, it will show you your error. Commented Jan 1, 2016 at 1:39
  • 2
    You're sending two independent objects in a row, like {"foo":"bar"} {"foo":"bar"}. They would need to be in an array instead. [{"foo":"bar"}, {"foo":"bar"}] Commented Jan 1, 2016 at 1:40
  • I see, but I can't understand why it is wrong and how can I fix that. In my JSON example I got this structure: {"tasks":[{ array }],"start_poz":"(integer_value)"}, {"tasks":[{ array }],"start_poz":"(integer_value)"}, {"tasks":[{ array }],"start_poz":"(integer_value)"} I can't find any manual that could help me to understand what's wrong with this syntax. Can anyone explain, please? Aha, thanks squint! It looks like I'm starting to understand that. Need to practice more ))) Actually I checked code with validator before, but I didn't understand my problem. Thanks you! Commented Jan 1, 2016 at 1:44

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

That's perfect answer! I manually managed to merge 3 JSON responses in 1 file and couldn't figure out what's the problem. Now I clearly know what caused that. Thanks for helping with basics!
Just for reference there is a problem with security in json files described at this site haacked.com/archive/2008/11/20/… so it might be better to use an object to combine them rather then an array
1

jsonlint.com says:

Error: Parse error on line 58:
...z": "36774263743"} { "tasks": [{     "id
----------------------^
Expecting 'EOF', '}', ',', ']', got '{'

So when it got to that '{' it was in a state where only

  • the END OF THE FILE,
  • a close-curly brace,
  • a comma,
  • or a close-bracket

would be valid.

Comments

1

Jim got it right, here is the corrected JSON

{
  "tasklist": [{
    "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"
  }]
}

Comments

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.