Home > Software design >  How to remove square brackets on a JSON so it can be converted in a Dataframe in Python
How to remove square brackets on a JSON so it can be converted in a Dataframe in Python

Time:12-10

I have a Json File

{
   "pre_processing":{
      "type":"single_file",
      "file_rename":"hr-feed"
   },
   "source_name":"hr-feed-client",
   "source_contents":[
      {
         "file_name":"hr-client",
         "table_name":"raw_data",
         "parser":"delimited-csv-file",
         "parser_parameters":{
            "delimiter":",",
            "quotechar":"\"",
            "encoding":"utf-168"
         },
         "fields":{
            "employee_id":{
               "type":{
                  "field_type":"string"
               },
               "original_field_name":"Employee ID",
               "mode":"required"
            }
         }
      }
   ],
   "source_relationships":[
      
   ],
   "entity_mapping":{
      
   }
}

I want to convert a small section of the JSON to a Dataframe, using:

data = data["source_contents"]

df = pd.DataFrame.from_dict(data["fields"], orient="index")

But this creates an error

TypeError: list indices must be integers or slices, not str

When I manually run the section without the square brackets it runs fine, so how do I remove the [] so the from_dict doesn't break?

CodePudding user response:

[] indicates a list, so you need to get into the first element in the list first.

data = data["source_contents"][0]
df = pd.DataFrame.from_dict(data["fields"], orient="index")

Output:

                                 type original_field_name      mode
employee_id  {'field_type': 'string'}         Employee ID  required
  • Related