Reputation: 55
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 :
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 :
Upvotes: 2
Views: 98
Reputation: 28630
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, '', '', '', '', '', '', '']
Upvotes: 1