2

I am having issues with validating the output of my json_encode() function.

I am pulling in an XML feed with cURL, converting it into an array, and converting that array into JSON with json_endode(). Ill spare you the cURL stuff:

  foreach ($xmlObjects->articleResult as $articleResult) {
    $article = array(
      "articleResult" =>
      array(
        'articleId' => (string)$articleResult->articleId,
        'title' => (string)$articleResult->title,
        'subhead' => (string)$articleResult->subhead,
        'tweet' => (string)$articleResult->tweet,
        'publishedDate' => (string)$articleResult->publishedDate,
        'image' => (string)$articleResult->image
      ),
    );
    $json = str_replace('\/','/',json_encode($article));
    echo $json;
  }

Which is giving me a JSON readout of:

{
    "articleResult": {
        "articleId": "0001",
        "title": "Some title",
        "subhead": "Some engaging subhead",
        "tweet": "Check out this tweet",
        "publishedDate": "January 1st, 1970",
        "image": "http://www.domain.com/some_image.jpg"
    }
}
{
    "articleResult": {
        "articleId": "0002",
        "title": "Some title",
        "subhead": "Some engaging subhead",
        "tweet": "Check out this tweet",
        "publishedDate": "January 1st, 1970",
        "image": "http://www.domain.com/some_image.jpg"
    }
}

This will give me a JSONLint error saying:

Parse error on line 10:
..._120x80.jpg"    }}{    "articleResult
---------------------^
Expecting 'EOF', '}', ',', ']'

So naturally I will add in the comma, which gives me an End of File expectation:

Parse error on line 10:
..._120x80.jpg"    }},{    "articleResu
---------------------^
Expecting 'EOF'

I am new to JSON, but I have checked the website and a few resources for proper JSON formatting and structure, and from what I can see my readout is following the guidelines. Any pointers?

Resources I've checked:

JSON.org naturally

Wikipedia has well documented page

W3Resource Had a good explanation of structure.

JSONLint

robabby
  • 2,160
  • 6
  • 32
  • 47

1 Answers1

2

You were encoding 2+ objects into json string, you need [ ] to wrap them

the correct syntax is

[ 
   { /* first object */ }
 , { /* second object */ }
 , { /* third object */ }
]

the things you need to look out are

  • [ ] wrap
  • separate objects by comma

Solution

$json = array();
foreach ($xmlObjects->articleResult as $articleResult) {
  $article = array(
    "articleResult" =>
    array(
      'articleId' => (string)$articleResult->articleId,
      'title' => (string)$articleResult->title,
      'subhead' => (string)$articleResult->subhead,
      'tweet' => (string)$articleResult->tweet,
      'publishedDate' => (string)$articleResult->publishedDate,
      'image' => (string)$articleResult->image
    ),
  );
  $json[] = $article;
}
echo json_encode($json);
Neverever
  • 15,890
  • 3
  • 32
  • 50
  • Hey thanks for your response! I am having a hard time grasping how I can add this solution into my code programmatically. And why isn't `json_encode()` taking care of this? Is there a syntax error in my `foreach` array? I tried adding `echo "[".$json."],";` but that throws an error as well. – robabby Sep 21 '12 at 01:12
  • PHP Replied with `Parse error: syntax error, unexpected '['`. It is referring to the `$json=[];` variable before the `foreach`. Turning it into a string responded with `Fatal error: [] operator not supported for strings`. However removing that variable all together would provide me with valid JSON readout. I assume this will suffice just fine. Thanks so much again for the response. Any chance you'd be willing to say why that solution fixes it, or perhaps a link explaining it? Thanks again. – robabby Sep 21 '12 at 02:08
  • 1
    what I did was to json_encode an array of objects at once, where as you were json_encode EACH object and then combine the strings, both way should work, just that your step of combining the strings were incorrect. – Neverever Sep 21 '12 at 02:12