4

I currently have this code running. It is splitting the variable $json where there are },{ but it also removes these characters, but really I need the trailing and leading brackets for the json_decode function to work. I have created a work around, but was wondering if there is a more elegant solution?

<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5},{"a":1,"b":2,"c":3,"d":4,"e":5}';
$individuals = preg_split('/},{/',$json);

$loop_count =1;
foreach($individuals as $object){
    if($loop_count == 1){$object .='}';}
    else{$object ="{".$object;}
    print_r(json_decode($object));
    echo '<br />';
$loop_count++;
}
?>

EDIT: The $json variable is actually retrieved as a json object. An proper example would be

[{"id":"foo","row":1,"col":1,"height":4,"width":5},{"id":"bar","row":2,"col":3,"height":4,"width":5}]

3
  • if you're trying to use JSON data,why would you not simply use PHP's JSON functions to get at it? You can encode the string into a JSOn object and access the variables. Commented May 17, 2013 at 22:15
  • Yes, JSON_decode() works just fine for turning JSON data into usable php objects (or arrays if you set the parameter). Otherwise you might want to look into "passive" matching with regexp. Commented May 17, 2013 at 22:17
  • The variable $json is sent to this file. I have include a hardcoded example for this question. Look at the question for an edit and explanation. Commented May 17, 2013 at 22:20

2 Answers 2

4

As you (presumably) already know, the string you have to start with isn't valid json because of the comma and the two objects; it's basically two json strings with a comma between them.

You're trying to solve this by splitting them, but there's a much easier way to fix this:

The work-around is simply to turn the string into valid JSON by wrapping it in square brackets:

$json = '[' . $json . ']';

Voila. The string is now valid json, and will be parsed successfully with a single call to json_decode().

Hope that helps.

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

5 Comments

Thank you that worked perfectly as I did not understand JSON that well. I am still interested to know if there is a more elegant solution to the problem I posed
a better solution, of course, would be to get the originating system fixed -- if it's sending out invalid json, then the real problem is at that end, and in an ideal world that's where the fix would be done, not at the client end.
there isn't really anything more "elegant" than this to fix it. This is about as elegant as it gets. When dealing with broken data, things tend to get inelegant pretty quickly. (you're lucky; I've seen much more badly broken input than this in the past!)
The input wasn't broken I simply was not formatting it correctly. I am still interested if we put the JSON element aside if there was a better way to obtain the bracketed info and retain the brackets and not the commas. Are you saying the way I wrote this is about as good as it gets
sorry I misunderstood what you were asking in that previous comment. No, in fact, it's very difficult to do that kind of thing with regex, because of the nature of JSON. You'd really need a parser, not a regex engine to do it properly (similar reasons for not using regex to parse HTML or XML). Conveniently enough, json_decode() provides exactly the parser you need. Just use json_decode() as described in the answer, then json_encode() each part of the array separately, and you'll have the separated strings you want.
0

You can always add them again. Try this:

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5},{"a":1,"b":2,"c":3,"d":4,"e":5}';
$individuals = preg_split('/},{/',$json);

foreach($individuals as $key=>$val)
   $individuals[$key] = '{'.$val.'}';

1 Comment

That would break the first and last items in the list.

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.