I have a string containing multiple json, like this:
{"token":" kann","finish":false}{"token":" ich","finish":false}
I want to parse these mutliple Jsons, so I need to split them from the string.
I could search for }{
occurances and split there. But I am not sure if this would be best practice. Is there a canonical way to do this?
4
Answers
Since it’s not valid JSON in and of itself, there’s no "standard" approach.
So: this fails if you have a more complex object, like
If would be better if you could get your JSON like this:
That would parse directly, no additional manipulation needed. If you were on my team, that’s what I would push toward.
Without knowing how you’re getting the JSON, and just taking it at face value, if your string is predictable, the same spacing every time, and will NEVER change format (never trust input you’re not directly in charge of), then splitting as you’ve suggested is about all you can do, just understand that if anything ever changes, your code will break.
Although @Winter Dev makes a sensible suggestion in his answer here to prevent this problem in the first place, I’d still like to provide an answer to the asked question.
A more sensible way would be a stack-based search. You can read the string from the very first
{
character up until a matching}
.Here’s the pseudocode:
Note that this fails when an unmatched
{
or}
is present, i.e. when you have incorrect JSON syntax or when a{
is listed as a normal character in a string somewhere. You would have to consider such edge cases in a real implementation.As suggested by @Winter Dev, you could format your JSON to have an array.
Example:
Output:
NB: as @Daniels118 pointed out in the comment section, this solution would obviously add the commas if a pair of
}{
brackets is present in a string inside the JSON, thus corrupting it.This is the way: implement a minimal json parser to skip control chars when they are within strings, and split when the structure depth comes to zero.
I have added a { within one of the strings to show how strong is the algorithm.
Output:
Does not work if the json is invalid. If you want it more robust you can make it your own.
P.S. this is a naive implementation just for demo purposes, it is extremely inefficient in case of long jsons because of the single char concatenations. It can be speed up a lot by holding the start and end index of the subJson, and then use a single substring extraction.