0

The data is not real for privacy purposes. My goal is to convert this structure to a csv. The following columns are: {"city", "line/Address", "postalCode","state", "birthDate","Boolean", "url", "code", "display", "system"} I think that I need to make this into a dictionary.

{
  "address": [
    {
      "city": "Sam town",
      "line": [
        "PO BOX 454545415198"
      ],
      "postalCode": "99999999",
      "state": "999"
    }
  ],
  "birthDate": "2040-15-84",
  "Boolean": false,
  "extension": [
    {
      "url": "none",
      "valueCoding": {
        "code": "99999",
        "display": "None",
        "system": "https://bluebutton.cms.gov/resources/variables/race"
      }
    }
user17629522
  • 105
  • 8

1 Answers1

0

With the pandas library, this is as easy as using two commands!

df = pd.read_json()

read_json converts a JSON string to a pandas object (either a series or dataframe). Then:

df.to_csv()

Which can either return a string or write directly to a csv-file. See the docs for to_csv.

import pandas as pd

with open('jsonfile.json', encoding='utf-8') as inputfile:
    df = pd.read_json(inputfile)

df.to_csv('csvfile.csv', encoding='utf-8', index=False)
Gaston Alex
  • 151
  • 1
  • 6
  • So I am pulling this data form an API so it comes in as just that format. It has to be converted into Json format then I need to convert into pandas df. – user17629522 Feb 16 '22 at 18:00