Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading Data from JSON
#3
This is untested, but should allow you to load and display contents

import json
from pathlib import Path
import os


#set path to same as script
os.chdir(os.path.abspath(os.path.dirname(__file__)))
homepath = home = Path('.')

def display_dict(dictname, level=0):
    indent = " " * (4 * level)
    for key, value in dictname.items():
        if isinstance(value, dict):
            print(f'\n{indent}{key}')
            level += 1
            display_dict(value, level)
        else:
            print(f'{indent}{key}: {value}')
        if level > 0:
            level -= 1

def main():
    jsonfile = homepath / 'response_1664244998587.json'

    print(jsonfile.resolve())
    with jsonfile.open() as fp:
        jsondata = json.load(fp)

    display_dict(jsondata)

main()
Reply


Messages In This Thread
Reading Data from JSON - by tpolim008 - Sep-27-2022, 02:58 AM
RE: Reading Data from JSON - by deanhystad - Sep-27-2022, 04:34 AM
RE: Reading Data from JSON - by Larz60+ - Sep-27-2022, 06:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  encrypt data in json file help jacksfrustration 1 268 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  TypeRoor reading json GreenLynx 3 898 May-16-2023, 01:47 PM
Last Post: buran
  Reading All The RAW Data Inside a PDF NBAComputerMan 4 1,393 Nov-30-2022, 10:54 PM
Last Post: Larz60+
  Read nested data from JSON - Getting an error marlonbown 5 1,406 Nov-23-2022, 03:51 PM
Last Post: snippsat
  Convert nested sample json api data into csv in python shantanu97 3 2,893 May-21-2022, 01:30 PM
Last Post: deanhystad
  Struggling with Juggling JSON Data SamWatt 7 1,940 May-09-2022, 02:49 AM
Last Post: snippsat
  json api data parsing elvis 0 946 Apr-21-2022, 11:59 PM
Last Post: elvis
  Initializing, reading and updating a large JSON file medatib531 0 1,807 Mar-10-2022, 07:58 PM
Last Post: medatib531
  Capture json data JohnnyCoffee 0 1,221 Nov-18-2021, 03:19 PM
Last Post: JohnnyCoffee
  Help reading data from serial RS485 korenron 8 14,114 Nov-14-2021, 06:49 AM
Last Post: korenron

Forum Jump:

User Panel Messages

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