Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading Data from JSON
#1
Hello looking to see if anyone could help me out here. Ive been trying to figure this out but seem to keep getting stuck. Im not very advance in python however im learning new things each day and time I write or try to write something new...

Long story short, I opening a JSON file pulling out only certain data from the dict once this data is found I print it, however I want to also print this data to a text file. When I'm doing the print statement its only writing the last record to the file when there is a total of around say 500 items printing on my screen.. I cannot for the life of me figure out or see what I am doing wrong... So any help or guidance on this would be great.

#! /usr/bin/python3.6
# parseJson.py

import json

f = open('response_1664244998587.json')

CONSTRUCT = json.loads(f.read())

for element in CONSTRUCT['data']:
    try:
        print(element['id'])
    except:
        continue
path = "/home/tpolim001/PNCChecker.txt"
print("PATH", path)

with open(path, "w+") as x:
    # Write Data to file
     x.write (element['id'])
     x.close()
Reply
#2
The print statement is in a loop. The file write is after the loop. Maybe the file write should be in the loop.
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  encrypt data in json file help jacksfrustration 1 195 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  TypeRoor reading json GreenLynx 3 846 May-16-2023, 01:47 PM
Last Post: buran
  Reading All The RAW Data Inside a PDF NBAComputerMan 4 1,342 Nov-30-2022, 10:54 PM
Last Post: Larz60+
  Read nested data from JSON - Getting an error marlonbown 5 1,359 Nov-23-2022, 03:51 PM
Last Post: snippsat
  Convert nested sample json api data into csv in python shantanu97 3 2,808 May-21-2022, 01:30 PM
Last Post: deanhystad
  Struggling with Juggling JSON Data SamWatt 7 1,886 May-09-2022, 02:49 AM
Last Post: snippsat
  json api data parsing elvis 0 925 Apr-21-2022, 11:59 PM
Last Post: elvis
  Initializing, reading and updating a large JSON file medatib531 0 1,770 Mar-10-2022, 07:58 PM
Last Post: medatib531
  Capture json data JohnnyCoffee 0 1,194 Nov-18-2021, 03:19 PM
Last Post: JohnnyCoffee
  Help reading data from serial RS485 korenron 8 13,943 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