If you need to handle a series of JSON objects (NOT a JSON array) from stdin or some other stream you’ll need to use a
less common method of parsing json than pythons json.loads(str)
Let’s say your input looks like this
You need to parse a variety of json objects but you don’t know how many there will be. We’re going to use raw_decode from python3’s json implementation.
loads |
raw_decode |
|
---|---|---|
Accepts | str | str |
Returns | parsed json | parsed json index the json object ended at |
Extra chars at the end? | json.decoder.JSONDecodeError |
Leaves them |
So let’s try it out
echo "{\"content\": 5}4" | python3 run
> [{'content': 5}, 4]