Python Forum
parse json field from csv file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
parse json field from csv file
#4
I don't know how to use pandas very well.

Hard to know what starts where and what ends where in the json.

I thinks this gets what you want.

Look carefully at the output of

for item in json_data.items():
    print(json.dumps(item, indent = 4, sort_keys=True))
Sub keys are further right than leading keys. Look for lists as well!

import json
import csv

myfile = '/home/pedro/myPython/json/csv/outputfile.csv'
with open(myfile) as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    data = []
    for row in csv_reader:
        data.append(row)
for d in data:
    print(d, len(d)

# get the last element of data[1] this is the json
json_data = json.loads(data[1][16])

for key in json_data.keys():
    print(json.dumps(key, indent = 4, sort_keys=True))
for item in json_data.items():
    print(json.dumps(item, indent = 4, sort_keys=True))

print(json_data["hits"]['hits'][0]['_source']['idAffaire'])
print(json_data["hits"]['hits'][0]['_source']['demande']['prestation']['code'])
snippsat and lebossejames like this post
Reply


Messages In This Thread
parse json field from csv file - by lebossejames - Nov-14-2023, 10:29 AM
RE: parse json field from csv file - by buran - Nov-14-2023, 11:16 AM
RE: parse json field from csv file - by Pedroski55 - Nov-14-2023, 10:06 PM
RE: parse json field from csv file - by snippsat - Nov-14-2023, 11:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  encrypt data in json file help jacksfrustration 1 489 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  parse/read from file seperated by dots giovanne 5 1,288 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Python Script to convert Json to CSV file chvsnarayana 8 2,823 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,459 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  [split] Parse Nested JSON String in Python mmm07 4 1,714 Mar-28-2023, 06:07 PM
Last Post: snippsat
  Converting a json file to a dataframe with rows and columns eyavuz21 13 5,341 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  validate large json file with millions of records in batches herobpv 3 1,415 Dec-10-2022, 10:36 PM
Last Post: bowlofred
  Writing to json file ebolisa 1 1,111 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Trying to parse only 3 key values from json file cubangt 8 3,838 Jul-16-2022, 02:05 PM
Last Post: deanhystad
  Initializing, reading and updating a large JSON file medatib531 0 1,906 Mar-10-2022, 07:58 PM
Last Post: medatib531

Forum Jump:

User Panel Messages

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