Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incremental JSON parsing in php

In a php program, I want to parse JSON incrementally. For example, given the partial JSON

[1, 2, {"id": 3},

I want to get 1, 2 and the dictionary even before the rest of the JSON input is streamed. php's json_decode just returns NULL and there doesn't seem to be a way to get the position of the error.

like image 856
phihag Avatar asked Sep 05 '11 18:09

phihag


People also ask

How to parse JSON in PHP?

First, you need to get the data from the file into a variable by using file_get_contents() . Once the data is in a string, you can call the json_decode() function to extract information from the string. Keep in mind that JSON simply provides a way to store information as a string using a set of predefined rules.

How to encode and decode JSON in PHP?

PHP has built-in functions to encode and decode JSON data. These functions are json_encode() and json_decode() , respectively. Both functions only works with UTF-8 encoded string data.

How to echo JSON response in PHP?

$exchangeToken = json_decode($exchangeToken, true); should do what you need. Show activity on this post. just pass true as second parameter, json_decode() returns in array format instead of object if second parameter is true.

How to encode JSON string in PHP?

PHP JSON Encoding a JSON string $array = [ 'name' => 'Jeff', 'age' => 20, 'active' => true, 'colors' => ['red', 'blue'], 'values' => [0=>'foo', 3=>'bar'], ]; During encoding, the PHP data types string, integer, and boolean are converted to their JSON equivalent.


3 Answers

Update

I've written a small class that does char-by-char JSON input parsing.. https://github.com/janeklb/JSONCharInputReader

Fresh off the presses so it's probably got a few bugs.. if you decide to try it out, let me know!

--

Could you (while keeping track of '{', '[', ']', '}' scope) break the stream up on each comma that's not part of a string value? And then process each token using json_decode()?

This solution would work best if the json stream didn't have a lot of large objects (as they would only be parsed once they've arrived in full).

Edit: if it does have large objects, this strategy could be modified to look a little 'deeper' instead.. but the complexity here would shoot up.

like image 50
jlb Avatar answered Oct 18 '22 07:10

jlb


I've written a SAX-like JSON streaming parser that should do the trick. Let me know if it works!

like image 44
Rob Gonzalez Avatar answered Oct 18 '22 07:10

Rob Gonzalez


There's a simple work-around, if each individual element is guaranteed to be received in it's entirety, or in other words - you can't get e.g. just the half of an object like this:

{"a": 1,

json_decode() will return NULL because the string you're passing to it is not a valid JSON string. Replace the trailing comma with an ending bracket and there you go:

[1, 2, {"id": 3}]

There's no problem in decoding it now and wait for other parts of the stream to be received later.

like image 37
Narf Avatar answered Oct 18 '22 07:10

Narf