Parse Two Json Strings In Python
A socket receives a JSON formatted string and might receive more than one which will result in a variable containing something like this: {'a':'1','b':'44'}{'a':'1','b':'44'} As y
Solution 1:
You can use the standard JSON parser and make use of the descriptive exception it throws when there is extra data behind the proper JSON string.
Currently (that is, my version of the JSON parser) throws a ValueError
with a message looking like this: "Extra data: line 3 column 1 - line 3 column 6 (char 5 - 10)"
.
The number 5
in this case (you can parse that out of the message easily with a regular expression) provides the information where the parsing failed. So if you get that exception, you can parse a substring of your original input, namely everything up to the character before that, and afterwards (I propose recursively) parse the rest.
import json, re
defjsonMultiParse(s):
try:
return json.loads(s)
except ValueError as problem:
m = re.match(
r'Extra data: line \d+ column \d+ - line \d+ column \d+ .char (\d+) - \d+.',
problem.message)
ifnot m:
raise
extraStart = int(m.group(1))
return json.loads(s[:extraStart]), jsonMultiParse(s[extraStart:])
print jsonMultiParse('{}[{}] \n\n["foo", 3]')
Will print:
({}, ([{}], [u'foo', 3]))
In case you prefer to get a straight tuple instead of a nested one:
return (json.loads(s),)
and
return (json.loads(s[:extraStart]),) + jsonMultiParse(s[extraStart:])
Return:
({}, [{}], [u'foo', 3])
Post a Comment for "Parse Two Json Strings In Python"