Python Forum
Compose nested JSON with multi columns in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compose nested JSON with multi columns in Python
#1
I have a csv file and trying to compose JSON from it. There are mulitple records in a file but I am just giving one set of sample records here.This structure is driven on the claimID. There is nesting on the claimLineDetail and claimSpecDiag.I guess I have to create some sort of list to handle this then the problem is how am I going to append it in the required structure. I really need some guidance here to achieve the desired result. Is it possible to break out different sections and append it later, I am not sure just assuming, as there are multiple columns.

Code
import csv,json
data = []
with open('JsonRequestPricingMedical.csv','r') as f:
    reader = csv.DictReader(f)
    for row in reader:
        print row
csv file :

claimId,subscriberId,claimType,claimSubType,providerId,totalChargeAmt,claimLineNo,pos_code,procedureCode,subdiagnosisCode,svcLineFromDt,svcLineToDt,chargedAmt,clmLineUnits,presentOnAdmit,diagnosisCode
18A000730400,101924200,M,M,002664514003,585,1,11,92014,H43393,2017-06-19,2017-06-19,160,1,U,H43393
18A000730400,101924200,M,M,002664514003,585,2,12,92015,H43395,2017-06-19,2017-06-19,160,2,U,H43394


Desired JSON :

[
{
"claimsHeader":" {
"claimId": "18A000730400",
"subscriberId": "101924200",
"claimType":{
"code": "M"
},
"claimSubType": {
"code": "M"
},
"providerId" :"002664514003",
"totalChargeAmt": "585",
"claimLineDetail" :[
{
"claimLineNo": "1",
"placeOfService": {
"code": "11"
},
"procedureCode": {
"code": "92014"
},
"subDiagnosisCd": {
"code": "H43393"
},
"svcLineFromDt": "2017-06-19",
"svcLineToDt": "2017-06-19",
"chargedAmt": "160",
"clmLineUnits": "1",
},
{
"claimLineNo": "2",
"placeOfService": {
"code": "12"
},
"procedureCode": {
"code": "92015"
},
"subDiagnosisCd": {
"code": "H433945
},
"svcLineFromDt": "2017-06-19",
"svcLineToDt": "2017-06-19",
"chargedAmt": "160",
"clmLineUnits": "2",
}
],
{
"claimSpecDiag": [

"presentOnAdmit": "",
"diagnosisCode": "H43393",

},
{
"presentOnAdmit": "",
"diagnosisCode": "H43394",
}

]
}
]
Reply
#2
This is totally untested code, but the concept is there, and with a small amount of tweaking should work fine:
import csv
import json

class CsvToJson:
    def __init__(self):
        self.convert_file()
    
    def convert_file(self):
        fields = (
            'claimId', 'subscriberId', 'claimType', 'claimSubType',
            'providerId', 'totalChargeAmt', 'claimLineNo', 'pos_code',
            'procedureCode', 'subdiagnosisCode', 'svcLineFromDt',
            'svcLineToDt', 'chargedAmt', 'clmLineUnits', 'presentOnAdmit',
            'diagnosisCode'
        )
        csvfile = open('JsonRequestPricingMedical.csv', 'r')
        jsonfile = open('', 'w')

        with open('JsonRequestPricingMedical.csv', 'r') as cp, 
            open('jsonfile.json', 'w') as jp:
            reader = csv.DictReader(cp, fields)
            for row in reader:
                json.dump(row, jp)
                jp.write('\n')

if __name__ == '__main__':
    CsvToJson()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Parse Nested JSON String in Python mmm07 4 1,523 Mar-28-2023, 06:07 PM
Last Post: snippsat
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,408 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  Read nested data from JSON - Getting an error marlonbown 5 1,358 Nov-23-2022, 03:51 PM
Last Post: snippsat
  Nested for loops: Iterating over columns of a DataFrame to plot on subplots dm222 0 1,707 Aug-19-2022, 11:07 AM
Last Post: dm222
  Python Split json into separate json based on node value CzarR 1 5,584 Jul-08-2022, 07:55 PM
Last Post: Larz60+
  Convert nested sample json api data into csv in python shantanu97 3 2,808 May-21-2022, 01:30 PM
Last Post: deanhystad
  Convert python dataframe to nested json kat417 1 6,328 Mar-18-2022, 09:14 PM
Last Post: kat417
  Problem with nested JSON Kalet 7 2,784 Dec-09-2021, 11:13 PM
Last Post: Gribouillis
  Docker-compose on Pycharm - Connection refused rstambach 3 3,393 Apr-08-2021, 03:07 AM
Last Post: rstambach
  how to run linux command with multi pipes by python !! evilcode1 2 6,332 Jan-25-2021, 11:19 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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