We can use DataFrame.from_dict and orient='index':

v = {
    "assetPropertyValues": {
        "A001234": {
            "PV": 1.2345
        },
        "A001235": {
            "PV": 1.234678
        },
        "A001236": {
            "PV": 1.234678
        }
    }
}

df = pd.DataFrame.from_dict(v['assetPropertyValues'], orient='index')

Or when reading from a file with json.load:

import json

import pandas as pd

with open('source.json') as f:
    df = pd.DataFrame.from_dict(
        json.load(f)['assetPropertyValues'],
        orient='index'
    )

df:

               PV
A001234  1.234500
A001235  1.234678
A001236  1.234678
Answer from Henry Ecker on Stack Overflow
๐ŸŒ
Medium
medium.com โ€บ @sajuselvan โ€บ turning-nested-json-into-relational-tables-with-python-563de90eafa5
Turning Nested JSON into Relational Tables with Python | by Saju Selvan | Medium
October 18, 2025 - FKs: link child tables to parent PK. With --fk-all-ancestors, child tables inherit all ancestor PKs. Preserves column discovery order (no artificial sorting).--fk-all-ancestors ... #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ json_to_tables.py โ€” JSON โ†’ relational-style table modeling (single-file) New in this build: - --fk-all-ancestors: include every ancestor PK as FK in all descendant tables.
Discussions

Python Nested JSON Import - Convert to table - Stack Overflow
I'm trying to import a json file (in this case from a FB profile export) that has a number of nested levels. In Excel I'm able to create a query that converts all the data into a table in about one More on stackoverflow.com
๐ŸŒ stackoverflow.com
October 31, 2018
python - Convert nested JSON to CSV or table - Stack Overflow
I know this question has been asked many times but none of the answers satisfy my requirement. I want to dynamically convert any nested JSON to a CSV file or Dataframe. Some sample examples are: in... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Converting embedded JSON into flat table - Code Review Stack Exchange
This might also help speed things up, as Python accesses locals faster than nonlocals. Now you also have a minor correctness issue - you only handle dict and list, but what if there were a tuple inside? Some user defined type that quacks like a dict? Admittedly if you're parsing json that is unlikely, but this would be a fairly useful general utility to flatten all sorts of nested ... More on codereview.stackexchange.com
๐ŸŒ codereview.stackexchange.com
June 14, 2016
Better way to parse insanely complex nested json data
Two suggestions: First, when you want to print a json thing, you can do print(json.dumps(thing, indent=2)). This will apply indentation and newlines to make it clearer what the nested structure is. Second, the input data is what it is - but internal to your code you don't have to keep it that way. My suggestion would be to make a dataclass with the fields you care about, and write a classmethod for that that class to extract what you care about from the json. Then in your code, use instances of your class. I'm on my phone right now, but I'll edit with a small example of what I mean in a little bit. gross_nested_json = { 'books': [ { 'name': 'whatever', 'details1': { 'due_date': 'whenever', }, 'details2': { 'whatever_else': 'thing', } } ] } import dataclasses import typing as ty @dataclasses.dataclass class BookInfo: # If you're not familiar with these, google python dataclass, they're nice name: str due_date: str whatever: str @classmethod def from_gross_json(cls, gross_json: dict) -> ty.Self: return cls( name=gross_json['name'], due_date=gross_json['details1']['due_date'] whatever=gross_json['details2']['whatever_else'] ) books = [BookInfo.from_gross_json(gross_json) for gross_json in gross_nested_json['books'] You'll have to adjust for the pecularities of your particular input data, but if you make the data less gross for within your code consumption, it'll make the rest of your program nicer to write. More on reddit.com
๐ŸŒ r/learnpython
7
1
August 19, 2024
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 75832421 โ€บ how-to-parse-nested-json-and-load-the-data-into-a-table-dataframe
python - How to parse nested JSON and load the data into a table / dataframe - Stack Overflow
def flatten_nested_json_df(df): df = df.reset_index() s = (df.applymap(type) == list).all() list_columns = s[s].index.tolist() s = (df.applymap(type) == dict).all() dict_columns = s[s].index.tolist() while len(list_columns) > 0 or len(dict_columns) > 0: new_columns = [] for col in dict_columns: exploded = pd.json_normalize(df[col]).add_prefix(f'{col}.') exploded.index = df.index df = pd.concat([df, exploded], axis=1).drop(columns=[col]) new_columns.extend(exploded.columns) # inplace for col in list_columns: df = df.drop(columns=[col]).join(df[col].explode().to_frame()) new_columns.append(col) s = (df[new_columns].applymap(type) == list).all() list_columns = s[s].index.tolist() s = (df[new_columns].applymap(type) == dict).all() dict_columns = s[s].index.tolist() return df
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 53080998 โ€บ python-nested-json-import-convert-to-table
Python Nested JSON Import - Convert to table - Stack Overflow
October 31, 2018 - The point is, there should be a generic way to do this in python regardless of the number of nested levels, right? Also, the link included has samples that are similar, if not nested so deep. ... def flatten_json(org_json,sep='_'): flattened = {} def flatten_level(d, prefix=''): if type(d) is dict: for k in d: flatten_level(d[k], prefix + k + sep) elif type(d) is list: for i,k in enumerate(d): flatten_level(k, prefix + str(i) + sep) else: flattened[prefix[:-1]] = d flatten_level(org_json) return flattened
Top answer
1 of 3
1
  • standard techniques for dealing with nested json
    1. json_normalize()
    2. explode()
    3. apply(pd.Series)
  • finally some cleanup, drop unwanted rows and replace nan with empty string
import json
js = """{"menu": {
    "header": "SVG Viewer",
    "items": [
        {"id": "Open"},
        {"id": "OpenNew", "label": "Open New"},
        null,
        {"id": "ZoomIn", "label": "Zoom In"},
        {"id": "ZoomOut", "label": "Zoom Out"},
        {"id": "OriginalView", "label": "Original View"},
        null,
        {"id": "Quality"},
        {"id": "Pause"},
        {"id": "Mute"},
        null,
        {"id": "Find", "label": "Find..."},
        {"id": "FindAgain", "label": "Find Again"},
        {"id": "Copy"},
        {"id": "CopyAgain", "label": "Copy Again"},
        {"id": "CopySVG", "label": "Copy SVG"},
        {"id": "ViewSVG", "label": "View SVG"},
        {"id": "ViewSource", "label": "View Source"},
        {"id": "SaveAs", "label": "Save As"},
        null,
        {"id": "Help"},
        {"id": "About", "label": "About Adobe CVG Viewer..."}
    ]
}}"""

df = pd.json_normalize(json.loads(js)).explode("menu.items").reset_index(drop=True)
df.drop(columns=["menu.items"]).join(df["menu.items"].apply(pd.Series)).dropna(subset=["id"]).fillna("")

menu.header id label
0 SVG Viewer Open
1 SVG Viewer OpenNew Open New
3 SVG Viewer ZoomIn Zoom In
4 SVG Viewer ZoomOut Zoom Out
5 SVG Viewer OriginalView Original View
7 SVG Viewer Quality
8 SVG Viewer Pause
9 SVG Viewer Mute
11 SVG Viewer Find Find...
12 SVG Viewer FindAgain Find Again
13 SVG Viewer Copy
14 SVG Viewer CopyAgain Copy Again
15 SVG Viewer CopySVG Copy SVG
16 SVG Viewer ViewSVG View SVG
17 SVG Viewer ViewSource View Source
18 SVG Viewer SaveAs Save As
20 SVG Viewer Help
21 SVG Viewer About About Adobe CVG Viewer...

utility function

  • if you don't want to name columns, but take first list column
  • identify first column that contains lists
  • explode() and apply(pd.Series) to that column
  • provided option to expand all lists
def normalize(js, expand_all=False):
    df = pd.json_normalize(json.loads(js) if type(js)==str else js)
    # get first column that contains lists
    col = df.applymap(type).astype(str).eq("<class 'list'>").all().idxmax()
    # explode list and expand embedded dictionaries
    df = df.explode(col).reset_index(drop=True)
    df = df.drop(columns=[col]).join(df[col].apply(pd.Series), rsuffix=f".{col}")
    # any lists left?
    if expand_all and df.applymap(type).astype(str).eq("<class 'list'>").any(axis=1).all():
        df = normalize(df.to_dict("records"))
    return df

js = """{ "id": "0001", "type": "donut", "name": "Cake", "ppu": 0.55, "batters": { "batter": [ { "id": "1001", "type": "Regular" }, { "id": "1002", "type": "Chocolate" }, { "id": "1003", "type": "Blueberry" }, { "id": "1004", "type": "Devil's Food" } ] }, "topping": [ { "id": "5001", "type": "None" }, { "id": "5002", "type": "Glazed" }, { "id": "5005", "type": "Sugar" } ] }"""

normalize(js, expand_all=True)

id type name ppu id.topping type.topping id.batters.batter type.batters.batter
0 0001 donut Cake 0.55 5001 None 1001 Regular
1 0001 donut Cake 0.55 5001 None 1002 Chocolate
2 0001 donut Cake 0.55 5001 None 1003 Blueberry
3 0001 donut Cake 0.55 5001 None 1004 Devil's Food
4 0001 donut Cake 0.55 5002 Glazed 1001 Regular
5 0001 donut Cake 0.55 5002 Glazed 1002 Chocolate
6 0001 donut Cake 0.55 5002 Glazed 1003 Blueberry
7 0001 donut Cake 0.55 5002 Glazed 1004 Devil's Food
8 0001 donut Cake 0.55 5005 Sugar 1001 Regular
9 0001 donut Cake 0.55 5005 Sugar 1002 Chocolate
10 0001 donut Cake 0.55 5005 Sugar 1003 Blueberry
11 0001 donut Cake 0.55 5005 Sugar 1004 Devil's Food

consider each list independent

  • copy way this works https://data.page/json/csv
  • this is a limited use case, it does not honor general data modelling principles
def n2(js):
    df = pd.json_normalize(json.loads(js))
    # columns that contain lists
    cols = [i for i, c in df.applymap(type).astype(str).eq("<class 'list'>").all().iteritems() if c]
    # use list from first row
    return pd.concat(
        [df.drop(columns=cols)]
        + [pd.json_normalize(df.loc[0, c]).pipe(lambda d: d.rename(columns={c2: f"{c}.{c2}" for c2 in d.columns}))
            for c in cols],
        axis=1,
    ).fillna("")

2 of 3
0

In python you can use pandas to do this but It will repeat the header values for each line like below


code

output

๐ŸŒ
Pybites
pybit.es โ€บ articles โ€บ case-study-how-to-parse-nested-json
Case study: How to parse nested JSON โ€“ Pybites
This article will guide you through the necessary steps to parse this JSON response into a pandas DataFrame. I will focus heavily on the concepts and code development and less on explaining each line of code. Ideally, you should be already familiar with at least a little Python and its standard data types, most importantly dictionaries.
Top answer
1 of 1
1

I don't know anything about tkinter, so I'm just going to look at your flatten function.

I'm going to start with some overarching comments. When checking for the type of something, use isinstance instead of type(x) is some_type. Secondly, when you have a trivial loop of the form

list_ = []
for x in iterable:
    list_.append(function(x))

You almost always want to turn it into a list comprehension

list_ = [function(x) for x in iterable]

You have an unnecessary dict.copy which is definitely going to slow things down if you start having very large files.

You can get rid of the for i in range(len(data)) and just use for item in data.

I don't like that out isn't a local variable/parameter, and I find it a bit hacky that it is modified inside of flatten2 without being passed as a parameter. I think a better idea is to use an input parameter. This might also help speed things up, as Python accesses locals faster than nonlocals.

Now you also have a minor correctness issue - you only handle dict and list, but what if there were a tuple inside? Some user defined type that quacks like a dict? Admittedly if you're parsing json that is unlikely, but this would be a fairly useful general utility to flatten all sorts of nested mappings. There are all sorts of ways to handle this, but the easiest might be to ask forgiveness instead of permission

try:
    for key, value in x.items():
        # your dict stuff here
except (ValueError, AttributeError): # oh this doesn't support unpacking/the items() method
    for item in x: # treat it like a list

Not necessarily a perfect solution, but worth considering.

I doubt this is going to help a ton with speed. Short of parallelising this (and I don't think Python would do that effectively with either threads or processes) I'm not seeing a good solution.

One last minor thing - if you have a large and deeply nested json file this might choke with a recursion error. You could fix that by using a list or a queue and keep adding items to it as they're discovered. Something like this (sketched, may not work right away) might work

to_flatten = [(item, '') for item in y]
for item, name in to_flatten:
    if isinstance(item, dict):
       for key, value in item.items():
            if key == "name":
                to_flatten.append((item["value"], name + item[key] + '_'))
            else:
                to_flatten.append((item[key], name + key + '_'))
    elif isinstance(x, (list, tuple)):
       for member in item:
           to_flatten.append((member, name + '_'))
    else:
        result_dict[name[:-1]] = item

This may also help with speed - I honestly don't know.

One last note - like with most languages, string concatenation is slow in Python. In CPython, the peephole optimizer does some magic with just 2 strings (I wish I could find the source for this) but doesn't for more than that. And according to PEP8,

For example, do not rely on CPython's efficient implementation of in-place string concatenation for statements in the form a += b or a = a + b. This optimization is fragile even in CPython ... In performance sensitive parts of the library, the ''.join() form should be used instead.

(emphasis and code-formatting added)

You might be able to get a speedup by changing your string concatenation method.

to_flatten.append((item["value"], ''.join([name, item[key], '_'])))
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ alpharithms โ€บ tabulate-json-data-in-python-using-pandas-9545f5155490
Tabulate JSON Data in Python Using Pandas | by Zack West | alpharithms | Medium
July 4, 2022 - Tabulating JSON in Python is ... Pandas. The normalize_json the method helps ensure that parsing nested JSON results in appropriate column names whereby nested keys are converted to dot-addressable column names...
๐ŸŒ
Medium
medium.com โ€บ swlh โ€บ converting-nested-json-structures-to-pandas-dataframes-e8106c59976e
Converting nested JSON structures to Pandas DataFrames | by Derek | The Startup | Medium
July 7, 2020 - Converting nested JSON structures to Pandas DataFrames The Problem APIs and document databases sometimes return nested JSON objects and youโ€™re trying to promote some of those nested keys into โ€ฆ
๐ŸŒ
Medium
medium.com โ€บ towards-agi โ€บ how-to-switch-from-json-to-tabular-format-a-comprehensive-guide-f7b877ec9b86
How to Switch from JSON to Tabular Format: A Comprehensive Guide | by Ineza Felin-Michel | Towards AGI | Medium
September 24, 2024 - Python, combined with the Pandas library, offers a powerful and flexible way to convert JSON to tabular format. This method is particularly useful for data scientists and analysts who are comfortable with Python. First, ensure you have Python installed on your system. Then, install the required libraries using pip: ... This method is particularly effective for handling complex, nested JSON structures, as Pandas can automatically flatten nested data into a tabular format.
๐ŸŒ
Like Geeks
likegeeks.com โ€บ home โ€บ python โ€บ pandas โ€บ export json to html table using pandas in python
Export JSON to HTML Table using Pandas in Python
April 13, 2025 - In this tutorial, youโ€™ll learn how to transform JSON data into an HTML table using Python Pandas. By the end, you will understand how to handle standard and nested JSON structures, export large JSON files through chunking, and convert your data into HTML tables.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ better way to parse insanely complex nested json data
r/learnpython on Reddit: Better way to parse insanely complex nested json data
August 19, 2024 -

Hi all,

Long time programmer here, but new to Python. This will be a long and, I think, complicated issue, so appreciate anyone who reads through it all and has any suggestions. I've looked up different ways to pull this data and don't seem to be making any progress. I'm sure there's a much better way.

I'm writing a program that will connect to our library to pull a list of everything we have checked out and I want to output a sorted list by due date and whether it has holds or not. I've got the code working to log in and pull a json data structure, but I cannot get it to export the data in the correct order. The json data is(to me) hideously complex with some data(due date) in one section and other data in another section. I'm able to pull the fields I want, but keeping them together is proving challenging.

For example, the title and subtitle are in the 'bibs/briefinfo' section with a key value of 'title' or 'subtitle'. Due Date is also in the 'checkouts' section with a key value of 'dueDate'. When I loop through them, though, the Titles are in one order, the due dates are in another order and the subtitles another.

I used BeautifulSoup because it's a webpage with json in it, so used BS to read the webpage.

I'm wanting to pull the following fields for each book so I can display the info for each book:

title, subtitle, contentType from briefinfo section

duedate from checkouts section

heldcopies and availablecopies from the availability section

Here's the pertinent section of my code:

soup = BeautifulSoup(index_page.text, 'html.parser')
            all_scripts = soup.find_all('script', {"type":"application/json"})

            for script in all_scripts:
                jsondata = json.loads(script.text)
                print(jsondata)
                
                output = []
                for i in item_generator(jsondata, "bibTitle"):
                    ans = {i}
                    print(i)
                    output.append(ans)

                for i in item_generator(jsondata, "dueDate"):
                    ans = {i}
                    output.append(ans)

                print("Subtitle----------------------")
                for i in item_generator(jsondata, "subtitle"):
                    ans = {i}
                    print(i)
                    output.append(ans)

print(output)

Here's the json output from my print statement so I can see what I'm working with. I tried to format it so it's easier to read. I removed a lot of other elements to keep the size down. Hopefully I didn't break any of the brackets.

{

'app':

{

'coreCssFingerprint': '123123123',

'coreAssets':

{

'cdnHost': 'https://xyz.com',

'cssPath': '/dynamic_stylesheet',

'defaultStylesheet': 'xyz.css'

},

},

'entities':

{

'listItems': {},

'cards': {},

'accounts':

{

'88888888':

  {
  'barcode': '999999999',
  'expiryDate': None, 
  'id': 88888888, 
  }

},

'shelves':

  {
  '88888888': 
  	{
  	'1111222222': 

{

'id': 1111222222,

'metadataId': 'S00A1122334',

'shelf': 'for_later',

'privateItem': True,

'dateAdded': '2023-12-30',

},

  	}
  }, 

'users':

  {
  '88888888': 
  	{ 
  	'accounts': \[88888888\], 
  	'status': 'A', 
  	'showGroupingDebug': False, 
  	'avatarUrl': '', 
  	'id': 88888888, 
  	}
  }, 
  'eventPrograms': {}, 
  'checkouts': 
  	{
  	'112233445566778899': 

{

'checkoutId': '112233445566778899',

'materialType': 'PHYSICAL',

'dueDate': '2024-08-26',

'metadataId': 'S99Z000000',

'bibTitle': "The Lord of the Rings"

},

  	'998877665544332211': 

{

'checkoutId': 998877665544332211',

'materialType': 'PHYSICAL',

'dueDate': '2024-08-26',

'metadataId': 'S88Y00000',

'bibTitle': 'The Lord of the Rings'

},

  	}, 
  'eventSeries': {}, 
  'catalogBibs': {},
  'bibs': 
  	{
  	'S88Y00000': 

{

'id': 'S88Y00000',

'briefInfo':

{

'superFormats': ['BOOKS', 'MODERN_FORMATS'],

'genreForm': [],

'callNumber': '123.456',

'authors': ['Tolkien, J.R.R.'],

'metadataId': 'S88Y00000',

'jacket':

{

'type': 'hardcover',

'local_url': None

},

'contentType': 'FICTION',

'format': 'BK',

'subtitle': 'The Two Towers',

'title': 'The Lord of the Rings',

'id': 'S88Y00000',

},

'availability':

{

'heldCopies': 0,

'singleBranch': False,

'metadataId': 'S88Y00000',

'statusType': 'AVAILABLE',

'totalCopies': 3,

'availableCopies': 2

}

},

'S77X12345':

{

'id': 'S77X12345',

'briefInfo':

{

'superFormats': ['BOOKS', 'MODERN_FORMATS'],

'genreForm': [],

'callNumber': '123.457',

'authors': ['Tolkien, J.R.R.'],

'metadataId': 'S77X12345',

'jacket':

{

'type': 'hardcover',

'local_url': None

},

'contentType': 'FICTION',

'format': 'BK',

'subtitle': 'The Fellowship of the Ring',

'title': 'The Lord of the Rings',

'id': 'S77X12345',

},

'availability':

{

'heldCopies': 0,

'singleBranch': False,

'metadataId': 'S77X12345',

'statusType': 'AVAILABLE',

'totalCopies': 2,

'availableCopies': 1

}

}

Anyone know of a better way to parse this data? Thanks!

Top answer
1 of 4
5
Two suggestions: First, when you want to print a json thing, you can do print(json.dumps(thing, indent=2)). This will apply indentation and newlines to make it clearer what the nested structure is. Second, the input data is what it is - but internal to your code you don't have to keep it that way. My suggestion would be to make a dataclass with the fields you care about, and write a classmethod for that that class to extract what you care about from the json. Then in your code, use instances of your class. I'm on my phone right now, but I'll edit with a small example of what I mean in a little bit. gross_nested_json = { 'books': [ { 'name': 'whatever', 'details1': { 'due_date': 'whenever', }, 'details2': { 'whatever_else': 'thing', } } ] } import dataclasses import typing as ty @dataclasses.dataclass class BookInfo: # If you're not familiar with these, google python dataclass, they're nice name: str due_date: str whatever: str @classmethod def from_gross_json(cls, gross_json: dict) -> ty.Self: return cls( name=gross_json['name'], due_date=gross_json['details1']['due_date'] whatever=gross_json['details2']['whatever_else'] ) books = [BookInfo.from_gross_json(gross_json) for gross_json in gross_nested_json['books'] You'll have to adjust for the pecularities of your particular input data, but if you make the data less gross for within your code consumption, it'll make the rest of your program nicer to write.
2 of 4
2
That's a rather awkward structure alright. Are the records to be linked by metadataId? [In]: for checkout in data['entities']['checkouts'].values(): print(checkout['bibTitle'], checkout['dueDate'], checkout['metadataId']) for bib in data['entities']['bibs'].values(): print(bib['briefInfo']['title'], bib['briefInfo']['subtitle'], bib['briefInfo']['metadataId']) print(bib['availability']['heldCopies'], bib['availability']['availableCopies']) [Out]: # The Lord of the Rings 2024-08-26 S99Z000000 # The Lord of the Rings 2024-08-26 S88Y00000 # The Lord of the Rings The Two Towers S88Y00000 # 0 2 # The Lord of the Rings The Fellowship of the Ring S77X12345 # 0 1
๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ blog โ€บ how-to-convert-nested-json-to-pandas-dataframe-with-specific-format
How to Convert Nested JSON to Pandas DataFrame with Specific Format | Saturn Cloud Blog
October 23, 2023 - In this blog post, we explored how to convert a nested JSON file into a Pandas DataFrame with a specific format. We used the json_normalize() function from the pandas.io.json module to normalize the nested data and create a flat table.
๐ŸŒ
GitHub
github.com โ€บ pucca09 โ€บ json2table
GitHub - pucca09/json2table: flattens nested json object, extracts the column value, exports to MongoDB
flattens the nested json object, convert an array of non-hierarchial json objects to table-format data ยท python setup.py install ยท
Author ย  pucca09
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ converting-nested-json-structures-to-pandas-dataframes
Converting nested JSON structures to Pandas DataFrames - GeeksforGeeks
November 22, 2021 - To convert it to a dataframe we will use the json_normalize() function of the pandas library. ... Here, we see that the data is flattened and converted to columns. If we do not wish to completely flatten the data, we can use the max_level attribute ...
๐ŸŒ
Reddit
reddit.com โ€บ r/dataengineering โ€บ turning json arrays into tables - data flattening
r/dataengineering on Reddit: Turning JSON arrays into tables - Data flattening
April 14, 2022 -

I was giving the task to flatten a JSON file which basically means to take every arrays and turn it table (not a actual table, all I need to do is give them a spreadsheet with the mapping). The only thing that I need to make sure is that there are a key between those 2 tables. There are many layered / nested arrays in the JSON.

Is there any automatic way of achieving this? There isn't any logic, just making sure that all layers talk to each other.

๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ converting json to sql table
r/learnpython on Reddit: Converting JSON to SQL Table
June 24, 2020 - It can create a table object from JSON and save the table to SQL. You might also have a look at csvkit from the same developers. ... Not that I know of but what I usually do is use the normalize_json to get it into a workable format ( nested dict I believe) and then convert that into a pandas data frame and then that can easily be sucked into amy DB ... without searching too hard to find the perfect "pythonic" way to do it I would just parse the json data then just print the INSERT statements and then use the command line to run the sql.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-parse-nested-json-in-python
How to Parse Nested JSON in Python - GeeksforGeeks
July 23, 2025 - In this article, we will discuss multiple ways to parse nested JSON in Python using built-in modules and libraries like json, recursion techniques and even pandas.
๐ŸŒ
Medium
medium.com โ€บ @connect.hashblock โ€บ turn-json-into-tables-with-pandas-2ddaeeb3d814
Turn JSON into Tables with Pandas | by Hash Block | Medium
October 6, 2025 - JSON is great for storing structured data, but not so great for quick analysis. Rows? Columns? Forget it โ€” JSON prefers to nest, loop, and sprawl. Thatโ€™s where Pandas comes in. In just a few lines of Python, you can turn a deeply nested JSON into a clean, readable table ready for analytics.
Top answer
1 of 3
9
  1. You can convert the Json to a dictionary in python using json.load.

  2. This dictionary can be converted to a dataframe using Pandas.Dataframe.

  3. You can export this dataframe as .csv using pandas.Dataframe.to_csv to be consumed in Postgres.

Note: This requires Pandas library to be installed. Or else,you can simply install Anaconda (if you are using any other IDE) and most frequently used packages come installed with it.

2 of 3
7

Use the code below.

I have used PrettyTable module for printing in a table like structure. Use this - https://www.geeksforgeeks.org/how-to-make-a-table-in-python/ for table procedure.

Also, all the headers and values will be stored in headers and values variable.

import json
from prettytable import PrettyTable

value = ['''
       {
           "_id": {
               "$Col1": "XXXXXXX2443"
           },
           "col2": false,
           "col3": "359335050111111",
           "startedAt": {
               "$date": 1633309625000
           },
           "endedAt": {
               "$date": 1633310213000
           },
           "col4": "YYYYYYYYYYYYYYYYYY",
           "created_at": {
               "$date": 1633310846935
           },
           "updated_at": {
               "$date": 1633310846935
           },
           "__v": 0
       }''']

dictionary = json.loads(value[0])
headers = []
values = []
for key in dictionary:
    head = key
    value = ""
    if type(dictionary[key]) == type({}):
        for key2 in dictionary[key]:
            head += "/" + key2
            value = dictionary[key][key2]
            headers.append(head)
            values.append(value)

    else:
        value = dictionary[key]
        headers.append(head)
        values.append(value)

print(headers)
print(values)
myTable = PrettyTable(headers)

myTable.add_row(values)
print(myTable)

Output

['_id/$Col1', 'col2', 'col3', 'startedAt/$date', 'endedAt/$date', 'col4', 'created_at/$date', 'updated_at/$date', '__v']
['XXXXXXX2443', False, '359335050111111', 1633309625000, 1633310213000, 'YYYYYYYYYYYYYYYYYY', 1633310846935, 1633310846935, 0]

+-------------+-------+-----------------+-----------------+---------------+--------------------+------------------+------------------+-----+
|  _id/$Col1  |  col2 |       col3      | startedAt/$date | endedAt/$date |        col4        | created_at/$date | updated_at/$date | __v |
+-------------+-------+-----------------+-----------------+---------------+--------------------+------------------+------------------+-----+
| XXXXXXX2443 | False | 359335050111111 |  1633309625000  | 1633310213000 | YYYYYYYYYYYYYYYYYY |  1633310846935   |  1633310846935   |  0  |
+-------------+-------+-----------------+-----------------+---------------+--------------------+------------------+------------------+-----+