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.
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.
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.
$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.
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.
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.
I've written a SAX-like JSON streaming parser that should do the trick. Let me know if it works!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With