1

I have rather very weird requirement now. I have below json and somehow I have to convert it into flat csv.

[
  {
    "authorizationQualifier": "SDA",
    "authorizationInformation": "          ",
    "securityQualifier": "ASD",
    "securityInformation": "          ",
    "senderQualifier": "ASDAD",
    "senderId": "FADA      ",
    "receiverQualifier": "ADSAS",
    "receiverId": "ADAD           ",
    "date": "140101",
    "time": "0730",
    "standardsId": null,
    "version": "00501",
    "interchangeControlNumber": "123456789",
    "acknowledgmentRequested": "0",
    "testIndicator": "T",
    "functionalGroups": [
      {
        "functionalIdentifierCode": "ADSAD",
        "applicationSenderCode": "ASDAD",
        "applicationReceiverCode": "ADSADS",
        "date": "20140101",
        "time": "07294900",
        "groupControlNumber": "123456789",
        "responsibleAgencyCode": "X",
        "version": "005010X221A1",
        "transactions": [
          {
            "name": "ASDADAD",
            "transactionSetIdentifierCode": "adADS",
            "transactionSetControlNumber": "123456789",
            "implementationConventionReference": null,
            "segments": [
              {
                "BPR03": "ad",
                "BPR14": "QWQWDQ",
                "BPR02": "1.57",
                "BPR13": "23223",
                "BPR01": "sad",
                "BPR12": "56",
                "BPR10": "32424",
                "BPR09": "12313",
                "BPR08": "DA",
                "BPR07": "123456789",
                "BPR06": "12313",
                "BPR05": "ASDADSAD",
                "BPR16": "21313",
                "BPR04": "SDADSAS",
                "BPR15": "11212",
                "id": "aDSASD"
              },
              {
                "TRN02": "2424",
                "TRN03": "35435345",
                "TRN01": "3435345",
                "id": "FSDF"
              },
              {
                "REF02": "fdsffs",
                "REF01": "sfsfs",
                "id": "fsfdsfd"
              },
              {
                "DTM02": "2432424",
                "id": "sfsfd",
                "DTM01": "234243"
              }
            ],
            "loops": [
              {
                "id": "24324234234",
                "segments": [
                  {
                    "N101": "sfsfsdf",
                    "N102": "sfsf",
                    "id": "dgfdgf"
                  },
                  {
                    "N301": "sfdssfdsfsf",
                    "N302": "effdssf",
                    "id": "fdssf"
                  },
                  {
                    "N401": "sdffssf",
                    "id": "sfds",
                    "N402": "sfdsf",
                    "N403": "23424"
                  },
                  {
                    "PER06": "Wsfsfdsfsf",
                    "PER05": "sfsf",
                    "PER04": "23424",
                    "PER03": "fdfbvcb",
                    "PER02": "Pedsdsf",
                    "PER01": "sfsfsf",
                    "id": "fdsdf"
                  }
                ]
              },
              {
                "id": "2342",
                "segments": [
                  {
                    "N101": "sdfsfds",
                    "N102": "vcbvcb",
                    "N103": "dsfsdfs",
                    "N104": "343443",
                    "id": "fdgfdg"
                  },
                  {
                    "N401": "dfsgdfg",
                    "id": "dfgdgdf",
                    "N402": "dgdgdg",
                    "N403": "234244"
                  },
                  {
                    "REF02": "23423342",
                    "REF01": "fsdfs",
                    "id": "sfdsfds"
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]

The column header name corresponding to deeper key-value make take nested form, like functionalGroups[0].transactions[0].segments[0].BPR15.

I am able to do this in java using this github project (here you can find the output format I desire in the explanation) in one line:

flatJson = JSONFlattener.parseJson(new File("files/simple.json"), "UTF-8");

The output was:

date,securityQualifier,testIndicator,functionalGroups[1].functionalIdentifierCode,functionalGroups[1].date,functionalGroups[1].applicationReceiverCode, ...
140101,00,T,HP,20140101,ETIN,...

But I want to do this in python. I tried as suggested in this answer:

with open('data.json') as data_file:
    data = json.load(data_file)
df = json_normalize(data, record_prefix=True)

with open('temp2.csv', "w", newline='\n') as csv_file:
    csv_file.write(df.to_csv())

However, for column functionalGroups, it dumps json as a cell value.

I also tried as suggested in this answer:

with open('data.json') as f:  # this ensures opening and closing file
    a = json.loads(f.read())

df = pandas.DataFrame(a)

print(df.transpose())

But this also seem to do the same:

                                                                          0
acknowledgmentRequested                                                   0
authorizationInformation                                                   
authorizationQualifier                                                  SDA
date                                                                 140101
functionalGroups          [{'functionalIdentifierCode': 'ADSAD', 'applic...
interchangeControlNumber                                          123456789
receiverId                                                  ADAD           
receiverQualifier                                                     ADSAS
securityInformation                                                        
securityQualifier                                                       ASD
senderId                                                         FADA      
senderQualifier                                                       ASDAD
standardsId                                                            None
testIndicator                                                             T
time                                                                   0730
version                                                               00501

Is it possible to do what I desire in python?

Mahesha999
  • 22,693
  • 29
  • 116
  • 189
  • 2
    Try the library https://github.com/amirziai/flatten, or if you want to write your own version, use this as a starting point: https://towardsdatascience.com/flattening-json-objects-in-python-f5343c794b10 – Alex Hall Apr 23 '19 at 09:10
  • well yeah it was working sort of...I am getting columns names in the form `functionalGroups_0_transactions_0_loops_0_segments_0_N101`. I was guessing something like `functionalGroups[0].transactions[0].loops[0].segments[0].N101`. But I guess this is simply not handled and will require considerable code changes... – Mahesha999 Apr 23 '19 at 10:39
  • 1
    See the second link if you want your version. – Alex Hall Apr 23 '19 at 10:40
  • Thanks for pointing me in right direction!!! Surprised its not possible out of box... – Mahesha999 Apr 23 '19 at 10:57

0 Answers0