Python Forum
Python convert csv to json with nested array without pandas
Thread Rating:
  • 3 Vote(s) - 2.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python convert csv to json with nested array without pandas
#1
I'm trying to insert new array inside the array but I'm not sure where can I append the data.

Csv table

date, id, description, name, code
2016-07-01, S56202, Class A, Jacky, 300-E003

Currently, my result is

    "date": "2016-07-01",
    "id": "S56202",
    "items": [{"description": "Class A",
         "code": "300-E003",
         "name": "Jacky"},
Here is my code
    import csv
    import json
    from itertools import groupby

    with open('student.csv', 'r') as csv_ledger:
        r = csv.DictReader(csv_ledger)
        data = [dict(d) for d in r]

        groups = []

        for k, g in groupby(data, lambda r: (r['id'], r['date'])):
            groups.append({"date": k[1],
                           "id": k[0],
                           "items": [{k: v for k, v in d.items() if k not in 
                                    ['ref_num', 'date']} for d in list(g)]})
I need to insert another array in items then add the email, code and name into the new array.

Expected result

    "items": [{"description": "Class A",
               "new_account": {"email": "[email protected]",
                               "code": "300-E003",
                               "name": "Jacky"}}]
Reply


Messages In This Thread
Python convert csv to json with nested array without pandas - by terrydidi - Jan-11-2019, 03:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 5,908 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  Convert np Array A to networkx G IanAnderson 2 692 Jul-05-2023, 11:42 AM
Last Post: IanAnderson
  Python Script to convert Json to CSV file chvsnarayana 8 2,535 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  [split] Parse Nested JSON String in Python mmm07 4 1,539 Mar-28-2023, 06:07 PM
Last Post: snippsat
  Read nested data from JSON - Getting an error marlonbown 5 1,381 Nov-23-2022, 03:51 PM
Last Post: snippsat
  Convert Json to table format python_student 2 5,553 Sep-28-2022, 12:48 PM
Last Post: python_student
  Python Split json into separate json based on node value CzarR 1 5,630 Jul-08-2022, 07:55 PM
Last Post: Larz60+
  Convert nested sample json api data into csv in python shantanu97 3 2,848 May-21-2022, 01:30 PM
Last Post: deanhystad
  how to parse this array with pandas? netanelst 2 1,328 May-18-2022, 06:09 PM
Last Post: Axel_Erfurt
  Convert python dataframe to nested json kat417 1 6,353 Mar-18-2022, 09:14 PM
Last Post: kat417

Forum Jump:

User Panel Messages

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