J'ai quelques fichier JSON:tableau Parse JSON en Python
{
"cis" : [ {
"ucmdbId" : "835cfedfaabc32a1358b322ff3bae056",
"type" : "running_software",
"properties" : {
"display_label" : "jboss (site1.ru)"
}
}, {
"ucmdbId" : "7ef9f21c132c12b3d8d2af0964cc5970",
"type" : "node",
"properties" : {
"display_label" : "site2.ru"
}
} ],
"relations" : [ {
"ucmdbId" : "80c42edbe32fbb4c25621756ec9e09d2",
"type" : "compound_f",
"properties" : null,
"end1Id" : "23e30baf2320a3274d0aa1e7f56cdaef",
"end2Id" : "15af0ba134327d32a0c5c72450e63fcd"
}, {
"ucmdbId" : "7fe9fb15d4462d1212aeee4aef2f32b4",
"type" : "compound_f",
"properties" : null,
"end1Id" : "23e30baf2320a3274d0aa327f56cdaef",
"end2Id" : "9232dd2621b814da632932e8cd33ffc8"
} ]
}
que je dois apporter à l'esprit:
[{
"ucmdbId" : "835cfedfaabc32a1358b322ff3bae056",
"type" : "running_software",
"display_label" : "jboss (site1.ru)"
}, {
"ucmdbId" : "7ef9f21c132c12b3d8d2af0964cc5970",
"type" : "node",
"display_label" : "site2.ru"
}]
et je ne ai besoin du tableau 'cis'. J'essaie en Python:
#!/usr/bin/python
import sys
import os
import tablib
import pandas as pd
import json
from pandas.io.json import json_normalize
f = open('/home/nik/test.json', 'rw')
jsonArray = f.read()
f.close
data = json.dumps(json.loads(jsonArray)['cis'])
jsonResult = pd.read_json(data)
array = json.loads(jsonArray)
print jsonArray
jsonResult.to_excel('/home/nik/output.xlsx', sheet_name='Sheet1')
mais comment puis-je obtenir les paramètres clés? i essayez d'utiliser.
* Les données d'impression [ 'type'] touches()
données d'impression [ 'type'] *
mais ont erreur:
AttributeError: « str 'objet n'a pas de' clé 'd'attribut.
comment puis-je obtenir le bon format json?
'data ['type']' renvoie quelque chose comme '" running_software "'. C'est une chaîne, pas un dictionnaire. – poke
'data ['type']' sera un objet 'str', à savoir' 'running_software '' ou '' node' '. Cet objet n'a pas de méthode '.key()'. Qu'espériez-vous arriver? – Alfe