0

How can I convert this string:

{"id":"tag:search.twitter.com,2005:1"}{"id":"tag:search.twitter.com,2005:2"}
{"id":"tag:search.twitter.com,2005:3"}{"id":"tag:search.twitter.com,2005:4"}

Into this JSON format:

[
    {"id":"tag:search.twitter.com,2005:1"},
    {"id":"tag:search.twitter.com,2005:2"},
    {"id":"tag:search.twitter.com,2005:3"},
    {"id":"tag:search.twitter.com,2005:4"}
]
10
  • 2
    Decode them, merge the arrays and re-encode them back as json? Like so: echo json_encode(array_merge(json_decode($string1, true), json_decode($string2, true))); Commented Jun 5, 2014 at 7:00
  • 4
    Your second version isn't valid JSON, either. Get rid of the outer braces. Commented Jun 5, 2014 at 7:01
  • 1
    also, commas need to separate all members of a JSON array, so your second example is off. Commented Jun 5, 2014 at 7:02
  • 1
    if you had one JSON object per line it would be easy. But having multiple objects on each line means you need to parse them to find the beginning and end of each object. Commented Jun 5, 2014 at 7:04
  • 1
    JSON can contain just about any string in a text property, parsing it isn't as trivial as it may seem. Commented Jun 5, 2014 at 7:18

3 Answers 3

1

You can do it like this:

$str = '{"id":"tag:search.twitter.com,2005:1"}{"id":"tag:search.twitter.com,2005:2"}
{"id":"tag:search.twitter.com,2005:3"}{"id":"tag:search.twitter.com,2005:4"}';

// wrap the string in [] to make it an array (when decoded).
// replace all the '}<spaces/line breaks/tabs>{' to '},{' to make it valid JSON array.
// decode the new JSON string to an object.    
$obj = json_decode('[' . preg_replace('/}\s*{/', '},{', $str) . ']');

var_dump($obj);

Output:

array (size=4)
  0 => 
    object(stdClass)[424]
      public 'id' => string 'tag:search.twitter.com,2005:1' (length=29)
  1 => 
    object(stdClass)[517]
      public 'id' => string 'tag:search.twitter.com,2005:2' (length=29)
  2 => 
    object(stdClass)[518]
      public 'id' => string 'tag:search.twitter.com,2005:3' (length=29)
  3 => 
    object(stdClass)[519]
      public 'id' => string 'tag:search.twitter.com,2005:4' (length=29)
Sign up to request clarification or add additional context in comments.

1 Comment

I didn't think the regex necessary, as the input string seem to be machine-produced and so probably won't have any separator at all… but upvoted for the bullet-proof solution.
1

Thanks for giving your comments on my question...I have resolved my issue by putting below code

    foreach (preg_split("/((\r?\n)|(\r\n?))/", $tdata) as $line)
    {
    print_r(json_decode($line));      
    }

Comments

0

Assuming that your string is composed of valid JSON object put together without any delimiter, you can do the following:

  1. Split the string with }{ as delimiter
  2. Loop over each item in the resulting array and add the missing parts
  3. Merge the array into one string with a , as delimiter
  4. Add the JSON array marks before and after the string

Example (with $input as the input string):

$chunks = explode('}{', $input);
$n = count($chunks);
for($i = 0; $i < $n; $i++) {
    if($i == 0) {
        $chunks[$i] = $chunks[$i].'}';
    } else if($i == $n - 1) {
        $chunks[$i] = '{'.$chunks[$i];
    } else {
        $chunks[$i] = '{'.$chunks[$i].'}';
    }
}
$output = '['.implode(',', $chunks).']';

Note that it will work on imbricated structures, but will miserably fail if you have a }{ in the text. But it is unlikely.

EDIT: One simple way of checking if the current chunk is a wrong cut could be by checking is the next chunk start with a ", because JSON object's attributes are always quoted. If not, then you can merge the current and next chunk and repeat until finding the right character.

6 Comments

what about {"a":"}{ im fishy"} ?
As said, it is unlikely to happen. And there is nothing really smart and quick to do about it beside writting a special parser for the case. Which would be a waste of time in my opinion.
@Elwinar in this way I cant do because I have pasted smoll part of output so sometime I get different json.
If so post more exemples. Beside the case mentionned earlier, there is no obvious structure that will fail.
Actual JSON is too big so cant paste here
|

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.