Given a single-lined string of multiple, arbitrary nested json-files without separators, like for example:
contents = r'{"payload":{"device":{"serial":213}}}{"payload":{"device":{"serial":123}}}'
How can contents
be parsed into an array of dicts/jsons ? I tried
df = pd.read_json(contents, lines=True)
But only got a ValueError response:
ValueError: Unexpected character found when decoding array value (2)
CodePudding user response:
You can split the string, then parse each JSON string into a dictionary:
import json
contents = r'{"payload":{"device":{"serial":213}}}{"payload":{"device":{"serial":123}}}'
json_strings = contents.replace('}{', '}|{').split('|')
json_dicts = [json.loads(string) for string in json_strings]
Output:
[{'payload': {'device': {'serial': 213}}}, {'payload': {'device': {'serial': 123}}}]