CP23
CP23

Reputation: 1

Convert string to dictionary to dataframe

I have data in string format as follows:

data = '{"ResponseStatus":404,"TieredFill":false} \n{"ResponseStatus":404,"TieredFill":false}\n'

when I try to dump this data to JSON, it gets converted into an empty file. I am beginner in Python, so thinking to solve this, I need to convert string into dictionary and then into dataframe.

Any input on this or if there's another way to convert string to json would be helpful.

Upvotes: 0

Views: 60

Answers (1)

Chris
Chris

Reputation: 29742

Use str.strip and split:

[json.loads(j) for j in data.strip().split("\n")]

Output:

[{'ResponseStatus': 404, 'TieredFill': False},
 {'ResponseStatus': 404, 'TieredFill': False}]

Upvotes: 3

Related Questions