Home > database >  Convert JSON Dict to Pandas Dataframe
Convert JSON Dict to Pandas Dataframe

Time:04-07

I have what appears to be a very simple JSON dict I need to convert into a Pandas dataframe. The dict is being pulled in for me as a string which I have little control over.

{
  "data": "[{'key1':'value1'}]"
}

I have tried the usual methods such as pd.read_json() and json_normalize() etc but can't seem to get it anywhere close. Has anyone a few different suggestions to try. I think ive see every error message python has at this stage.

CodePudding user response:

It seems to me that your JSON data is improperly formatted. The double quotations around the brackets indicate that everything within those double quotes is a string. Essentially the data is considered a string and not an array of values. Remove the double quotes and to create an array in your JSON file.

{
  "data": [{"key1":"value1"}]
}

This will create the array and allow your JSON to be properly parsed using your previous stated methods.

CodePudding user response:

The example provided is a single key, but in general you can use pandas to load json and nested json with pd.json_normalize(yourjsonhere)

  • Related