Issue
I have the follwing file consising of two concatendated JSON strings:
{
"hello": 2,
"world": 3
}{
"something": 5,
"else": 6
}
Both of them are correct individually (they are more complex than that, but are always two JSON dicts one after each other).
Since I can predict the format of the first one (close to my example above), I will parse the file with a regex expression and end up separating them (I just need the second JSON):
{[\s\S]*?}([.\n]*?)
While this solution works, I wanted to make sure that there is not a more generic way to address this problem.
Solution
You can simply format your string by converting it to a valid python object like a list of dictionary then load it with json module:
In [60]: s = """{
"hello": 2,
"world": 3
}{
"something": 5,
"else": 6
}"""
In [61]: json.loads("[{}]".format(s.replace('}{', '},{')))
Out[61]: [{'hello': 2, 'world': 3}, {'something': 5, 'else': 6}]
Answered By - Mazdak
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.