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
#5
This is a JSON response from an API request.
The request is named "RFE" in the collection "Web_test" and it's a GET request to the URL ......
So should not be saved as .csv but .json then keep the correct data structure.
If you do not have control how to get data from the API request and just have the .csv file then have to parse as shown by Pedroski55.
Or like this this.
import csv
import json

csv_file_path = 'data.csv'
json_output = []
with open(csv_file_path, mode='r', encoding='utf-8') as csv_file:
    csv_reader = csv.DictReader(csv_file)
    for row in csv_reader:
        if row['body']:
            row['body'] = json.loads(row['body'])
        json_output.append(row)
json_data = json.dumps(json_output, indent=2)
print(json_data)

with open('output.json', 'w', encoding='utf-8') as json_file:
    json_file.write(json_data)
Output:
[ { "iteration": "1", "collectionName": "Web_test", "requestName": "RFE", "method": "GET", "url": "https://indexa.com/_search", "status": "OK", "code": "200", "responseTime": "509", "responseSize": "1270", "executed": "", "failed": "", "skipped": "", "totalAssertions": "0", "executedCount": "0", "failedCount": "0", "skippedCount": "0", "body": { "took": 152, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 15.476334, "hits": [ { "_index": "dossiers-bui1_v7", "_type": "_doc", "_id": "A07", "_score": 15.476334, "_source": { "idAffaire": "A07", "idContrat": "F003", "donneesPoint": { "codePostal": "27700", "commune": { "libelle": "HARQUENCY" }, "departement": "27" }, "statut": "COURS", "demande": { "prestation": { "code": "F8", "valeur": "Rac" }, "dateCreation": "2023-05-14T22:00:00.000+00:00", "client": { "categorieClient": "RES", "personnePhysique": { "nom": "MOUSS" }, "personneMorale": {} }, "dateModification": "2023-09-25T22:00:00.000+00:00", "dateEffet": "2023-05-21T22:00:00.000+00:00", "motifCloture": {}, "initiateur": { "login": "[email protected]", "acteurAppartenance": { "codeAcm": "ACM_7", "libelle": "B" } }, "affaireEtat": { "libelle": "Demande re\u00e7ue", "code": "DMRI" }, "affaireEtatExterne": { "libelle": "Demande transmise", "code": "DMTR" }, "affaireAvecRelance": "false", "interventionEnCours": false }, "segment": "C", "acteurMarche": { "codeAcm": "ACM_", "libelle": "BCM " }, "frnSortant": {}, "applicationSource": "AD", "recevabilite": { "prestations": { "prestation": { "option": {} } } } } } ] } } } ]
lebossejames likes 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 449 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  parse/read from file seperated by dots giovanne 5 1,248 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Python Script to convert Json to CSV file chvsnarayana 8 2,777 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,412 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  [split] Parse Nested JSON String in Python mmm07 4 1,686 Mar-28-2023, 06:07 PM
Last Post: snippsat
  Converting a json file to a dataframe with rows and columns eyavuz21 13 5,255 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  validate large json file with millions of records in batches herobpv 3 1,393 Dec-10-2022, 10:36 PM
Last Post: bowlofred
  Writing to json file ebolisa 1 1,097 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Trying to parse only 3 key values from json file cubangt 8 3,753 Jul-16-2022, 02:05 PM
Last Post: deanhystad
  Initializing, reading and updating a large JSON file medatib531 0 1,882 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