Home > OS >  Convert string of list of JSON which is separated by '\n' to list of JSON(data from mixpa
Convert string of list of JSON which is separated by '\n' to list of JSON(data from mixpa

Time:10-29

The data I retrieved from Mixpanel has the format:

"{"event":"info1xxxxx","Id":"0001"}
 {"event":"info2xxxxx","Id":"0002"}
 {"event":"info3xxxxx","Id":"0003"}
 {"event":"info3xxxxx","Id":"0003","other_key":"value"}..."

It's a string of list of JSON which are separated by '\n'. And each JSON may have a different structure.

I expect to convert it to list of JSON like:

[{"event":"info1xxxxx","Id":"0001"},
 {"event":"info2xxxxx","Id":"0002"},
 {"event":"info3xxxxx","Id":"0003"},
 {"event":"info3xxxxx","Id":"0003","other_key":"value"},...]

How could I do this? Looking for the help, thank you!

CodePudding user response:

You can try str.splitlines:

import json

s = """\
{"event":"info1xxxxx","Id":"0001"}
{"event":"info2xxxxx","Id":"0002"}
{"event":"info3xxxxx","Id":"0003"}
{"event":"info3xxxxx","Id":"0003","other_key":"value"}"""


lst = json.loads(f"[{', '.join(s.splitlines())}]")
print(lst)

Prints:

[
    {"event": "info1xxxxx", "Id": "0001"},
    {"event": "info2xxxxx", "Id": "0002"},
    {"event": "info3xxxxx", "Id": "0003"},
    {"event": "info3xxxxx", "Id": "0003", "other_key": "value"},
]

CodePudding user response:

I am not certain exactly about the format of the string since I am not familiar with Mixpanel, but assuming you do not have '\n' at the end of the string

s = "{"event":"info1xxxxx","Id":"0001"}
 {"event":"info2xxxxx","Id":"0002"}
 {"event":"info3xxxxx","Id":"0003"}
 {"event":"info3xxxxx","Id":"0003","other_key":"value"}"""
json_list = [json.loads(e) for e in s.split('\n')]

In case you have '\n' at the end of the string

s = "{"event":"info1xxxxx","Id":"0001"}
 {"event":"info2xxxxx","Id":"0002"}
 {"event":"info3xxxxx","Id":"0003"}
 {"event":"info3xxxxx","Id":"0003","other_key":"value"}
"""

json_list = [json.loads(e) for e in s[:-1].split('\n')]

For both cases the value of json_list will be:

[
    {'event': 'info1xxxxx', 'Id': '0001'},
    {'event': 'info2xxxxx', 'Id': '0002'},
    {'event': 'info3xxxxx', 'Id': '0003'},
    {'event': 'info3xxxxx', 'Id': '0003', 'other_key': 'value'}
]
  • Related