Python Forum
pandas.json_normalize question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pandas.json_normalize question
#3
You do not build a json file. You create a python object and use json.dump(obj, file) to create a json file.

This is what a the json file looks like for the list in your post.
[{"id": 1, "name": "Cole Volk", "fitness": {"height": 130, "weight": 60}}, {"name": "Mark Reg", "fitness": {"height": 130, "weight": 60}}, {"id": 2, "name": "Faye Raker", "fitness": {"height": 130, "weight": 60}}]

I created it like this:
import json

# This is not json.  This is a list of dictionaries.
data = [
    {
        "id": 1,
        "name": "Cole Volk",
        "fitness": {"height": 130, "weight": 60},
    },
    {"name": "Mark Reg", "fitness": {"height": 130, "weight": 60}},
    {
        "id": 2,
        "name": "Faye Raker",
        "fitness": {"height": 130, "weight": 60},
    },
]
with open("test.json", "w") as file:
    data = json.dump(data, file)  # This writes data to file using json syntax.
If you had some records from a database that you want to put in a json file:
1 Retrieve the records from the database.
2 Put the records in a list.
3 Open a file for writing.
4 Dump the list to the file.
You can use indent in the dump command to make the file prettier, but the purpose of json is portable storage and retrieval. json is not a human friendly presentation format.

This is a program that reads the json file and prints the list.
import json

with open("test.json", "r") as file:
    data = json.load(file)
print(data)
Output:
[{'id': 1, 'name': 'Cole Volk', 'fitness': {'height': 130, 'weight': 60}}, {'name': 'Mark Reg', 'fitness': {'height': 130, 'weight': 60}}, {'id': 2, 'name': 'Faye Raker', 'fitness': {'height': 130, 'weight': 60}}]
To make it pretty I can use json.
import json

with open("test.json", "r") as file:
    data = json.load(file)
print(json.dumps(data, indent=4))
Output:
[ { "id": 1, "name": "Cole Volk", "fitness": { "height": 130, "weight": 60 } }, { "name": "Mark Reg", "fitness": { "height": 130, "weight": 60 } }, { "id": 2, "name": "Faye Raker", "fitness": { "height": 130, "weight": 60 } } ]
json.dumps(obj) is like json.dump(obj, file) except it produces a string instead of writing to a file. There is a corresponding json.loads(string) that is similar to json.load(file).
Reply


Messages In This Thread
pandas.json_normalize question - by elsvieta - Apr-02-2025, 05:49 PM
RE: pandas.json_normalize question - by elsvieta - Apr-02-2025, 08:42 PM
RE: pandas.json_normalize question - by deanhystad - Apr-02-2025, 09:01 PM
RE: pandas.json_normalize question - by deanhystad - Apr-02-2025, 09:40 PM
RE: pandas.json_normalize question - by elsvieta - Apr-03-2025, 07:36 PM
RE: pandas.json_normalize question - by deanhystad - Apr-03-2025, 09:39 PM
RE: pandas.json_normalize question - by Pedroski55 - Apr-04-2025, 03:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas and MongoDB question Majority390 1 1,548 Dec-23-2024, 02:41 AM
Last Post: sakshi009
  pandas df inside a df question mbaker_wv 4 2,313 Dec-25-2022, 01:11 AM
Last Post: mbaker_wv
  Pandas usecols question rsearing 1 2,027 Aug-20-2022, 10:10 PM
Last Post: jefsummers
  Simple pandas question mcva 4 3,859 Dec-17-2021, 04:47 PM
Last Post: mcva
  Pandas question new2datasci 0 2,574 Jan-10-2021, 01:29 AM
Last Post: new2datasci
  Pandas merge question smw10c 2 6,634 Jul-02-2020, 06:56 PM
Last Post: hussainmujtaba
  Counting Criteria in Pandas Question Koenig 1 2,822 Sep-30-2019, 05:16 AM
Last Post: perfringo
  Function question using Pandas smw10c 7 8,899 Feb-12-2019, 06:52 PM
Last Post: Nathandsn
  Simple pandas dataframe question popohoma 1 4,526 Jan-03-2019, 05:00 PM
Last Post: ashlardev
  question on pandas datareader kit12_31 3 10,793 Feb-05-2018, 11:55 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020