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
#2
I tried to run your code, planning to tinker with it to try to get a solution, but got an error:
Error:
Traceback (most recent call last): File "testit.py", line 11, in <module> for k, g in groupby(data, lambda r: (r['id'], r['date'])): File "testit.py", line 11, in <lambda> for k, g in groupby(data, lambda r: (r['id'], r['date'])): KeyError: 'id'
Reply
#3
Thanks for your reply. The 'id' must exactly same with the header in csv. It's case sensitive.
I updated the content

date, ref_num, description,debit,credit,coa_code, coa_name
2018-12-28, PV18/12/066, Jacky , ,59.80,310-1000, Samboi
import csv
import json
from itertools import groupby

with open('testing.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['ref_num'], r['date'])):
        groups.append({
            "date": k[1],
            "ref_num": k[0],
            "items": [{k: v for k, v in d.items() if k not in ['ref_num', 'date']} for d in list(g)]
        })

print(json.dumps(groups[:10], indent=4))
Output result
"date": "2018-12-28",
        "ref_num": "PV18/12/066",
        "items": [
            {
                "description": "Jacky",
                "debit": "",
                "credit": "59.80",
                "coa_code": "310-1000",
                "coa_name": "Samboi"
            },
Expected Result
"date": "2018-12-28",
        "ref_num": "PV18/12/066",
        "items": [
            {
                "description": "SHARMINI SHANKAR",
                "debit": "",
                "credit": "59.80",
                "new_of_account": {
                                    "id": "1",
                                    "coa_code": "310-1000",
                                    "coa_name": "samboi"
                                  }
            },
Reply


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