简体   繁体   English

在Python中用多列组成嵌套JSON

[英]Compose nested JSON with multi columns in Python

I have a csv file and trying to compose JSON from it. 我有一个csv文件,并尝试从中撰写JSON。 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. 文件中有多条记录,但我在这里仅给出一组示例记录。此结构由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. 在ClaimLineDetail和ClaimSpecDiag上有嵌套。我想我必须创建某种列表来处理此问题,然后问题是如何将其附加到所需的结构中。 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 : csv文件:

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 所需的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",
         }

    ]   
  }
]

When you read a csv, each line represents variables separated by a special char, in your case, comas: ",". 读取csv时,每一行代表由特殊字符分隔的变量,在本例中为逗号:“,”。

You can get each variable separated by doing line_variables = row.split(',') 您可以通过执行line_variables = row.split(',')分离每个变量

Just pass the first line, and for all the other, do something like: 只需通过第一行,对于其他所有代码,请执行以下操作:

result = {
   "claimsHeader":" {
    "claimId": line_variables[0],
    "subscriberId": line_variables[1],
    "claimType":{
        "code": line_variables[2]
    }
...

Finaly, just add the result to a list (created just before your for loop) with your_list.append(result) . 最后,只需将结果添加到带有your_list.append(result)的列表(在for循环之前创建your_list.append(result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM