2

How can I extract name and value from my JSON with an for and input all my data in my CSV ?

What I expect from my code is a list of name and value in my CSV. Like :

enter image description here

The code :

        if valueselector == 'PRINT':
           idMag = json_dict['data']['archivingLegal']['idMag']
           roomid = json_dict['data']['archivingLegal']['roomid']
           safeid = json_dict['data']['archivingLegal']['safeid']
           serieID = json_dict['data']['archivingLegal']['serieID']
           serieCode = json_dict['data']['archivingLegal']['serieCode']
           fdr = json_dict['data']['archivingLegal']['fdr']
           createSafe = json_dict['data']['archivingLegal']['createSafe']
           fromGif = json_dict['data']['archivingLegal']['fromGif']
           indexFileName = json_dict['data']['archivingLegal']['indexFileName']
           originalName = json_dict['data']['archivingLegal']['originalName']
           xmlns = json_dict['data']['archivingLegal']['archives']['-xmlns']
           for i in range(0, 8):
               indexName = json_dict['data']['archivingLegal']['archives']['archive']['index'][i]['name']
               indexValue = json_dict['data']['archivingLegal']['archives']['archive']['index'][i]['value']
           recordId = json_dict['data']['archivingLegal']['dataMode']['recordId']
           raw_data_archivingLegal = [idMag, roomid, safeid,
                                      serieID, serieCode, fdr,
                                      createSafe, fromGif, indexFileName,
                                      originalName, xmlns, indexName,
                                      indexValue, recordId, '',
                                      '', '', '',
                                      '', '', '']

My Index for the configuration for the DataFrame :

"----": ['idMag', 'roomid', 'safeid',
         'serieID', 'serieCode', 'fdr',
         'createSafe', 'fromGif', 'indexFileName',
         'originalName', 'xmlns', 'indexName',
         'indexValue', 'recordId', '',
         '', '', '',
         '', '', ''],
"archivingLegal": raw_data_archivingLegal,

The JSON :

"data": {
    "archivingLegal": {
        "archives": {
            "-xmlns": "google.com",
            "archive": {
                "index": [{
                        "name": "FILENAME",
                        "value": " "
                    }, {
                        "name": "TYPEDOC",
                        "value": " "
                    }, {
                        "name": "ORGANISATION",
                        "value": " "
                    }, {
                        "name": "ACTIVITE",
                        "value": " "
                    }, {
                        "name": "DOC_CODE",
                        "value": " "
                    }, {
                        "name": "CLIENT_CODE",
                        "value": " "
                    }, {
                        "name": "DATE_TRAITEMENT",
                        "value": " "
                    }, {
                        "name": "DUA",
                        "value": " "
                    }
                ]
            }
        },
        "createSafe": " ",
        "dataMode": {
            "recordId": " "
        },
        "fdr": " ",
        "fromGif": " ",
        "idMag": " ",
        "indexFileName": " ",
        "originalName": " ",
        "roomid": " ",
        "safeid": " ",
        "serieCode": " ",
        "serieID": " "
    }

My actual Output csv :

enter image description here

2
  • it's not clear what you want the expected output to look like. Commented Dec 20, 2019 at 15:34
  • @chitown88 It's now edited Commented Dec 20, 2019 at 16:00

1 Answer 1

1

Iterate them into a list. Then you can leave as a list, or join the values into a single string (as shown in your expected output:

json Data

json_dict = {"data": {
    "archivingLegal": {
        "archives": {
            "-xmlns": "google.com",
            "archive": {
                "index": [{
                        "name": "FILENAME",
                        "value": " "
                    }, {
                        "name": "TYPEDOC",
                        "value": " "
                    }, {
                        "name": "ORGANISATION",
                        "value": " "
                    }, {
                        "name": "ACTIVITE",
                        "value": " "
                    }, {
                        "name": "DOC_CODE",
                        "value": " "
                    }, {
                        "name": "CLIENT_CODE",
                        "value": " "
                    }, {
                        "name": "DATE_TRAITEMENT",
                        "value": " "
                    }, {
                        "name": "DUA",
                        "value": " "
                    }
                ]
            }
        },
        "createSafe": " ",
        "dataMode": {
            "recordId": " "
        },
        "fdr": " ",
        "fromGif": " ",
        "idMag": " ",
        "indexFileName": " ",
        "originalName": " ",
        "roomid": " ",
        "safeid": " ",
        "serieCode": " ",
        "serieID": " "
    }}}

Code modified

idMag = json_dict['data']['archivingLegal']['idMag']
roomid = json_dict['data']['archivingLegal']['roomid']
safeid = json_dict['data']['archivingLegal']['safeid']
serieID = json_dict['data']['archivingLegal']['serieID']
serieCode = json_dict['data']['archivingLegal']['serieCode']
fdr = json_dict['data']['archivingLegal']['fdr']
createSafe = json_dict['data']['archivingLegal']['createSafe']
fromGif = json_dict['data']['archivingLegal']['fromGif']
indexFileName = json_dict['data']['archivingLegal']['indexFileName']
originalName = json_dict['data']['archivingLegal']['originalName']
xmlns = json_dict['data']['archivingLegal']['archives']['-xmlns']

############## HERE's THE MODIFICATION ###############
indexName = [ i['name'] for i in json_dict['data']['archivingLegal']['archives']['archive']['index'] ]
indexValue = [ i['value'] for i in json_dict['data']['archivingLegal']['archives']['archive']['index'] ]

indexName = ', '.join(indexName)    
indexValue = ', '.join(indexValue)    

###################################################### 


recordId = json_dict['data']['archivingLegal']['dataMode']['recordId']
raw_data_archivingLegal = [idMag, roomid, safeid,  serieID, serieCode, fdr,  
                           createSafe, fromGif, indexFileName,  originalName, 
                           xmlns, indexName,  indexValue, recordId, '',  '', '', '',  '', '', '']    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.