I am new to python and have tried to get data from a python json document , what I try to do is pass the information to python and json from python print a pdf with a table style. My code in json is
[
{
"files": 0,
"data": [
{"name": "RFC", "value": "XXXXXXX", "attId": 01},
{"name": "NOMBRE", "value": "JOSE", "attId": 02},
{"name": "APELLIDO PATERNO", "value": "MONTIEL", "attId": 03},
{"name": "APELLIDO MATERNO", "value": "MENDOZA", "attId": 04},
{"name": "FECHA NACIMIENTO", "value": "1989-02-04", "attId": 05}
],
"dirId": 1,
"docId": 4,
"structure": {
"name": "personales",
"folioId": 22
}
},
{
"files": 0,
"data": [
{"name": "CALLE", "value": "AMOR", "attId": 06},
{"name": "No. EXTERIOR", "value": "4", "attId": 07},
{"name": "No. INTERIOR", "value": "2", "attId": 08},
{"name": "C.P.", "value": "55060", "attId": 09},
{"name": "ENTIDAD", "value": "ESTADO DE MEXICO", "attId": 10},
{"name": "MUNICIPIO", "value": "ECATEPEC", "attId": 11},
{"name": "COLONIA", "value": "INDUSTRIAL", "attId": 12}
],
"dirId": 1,
"docId": 4,
"structure": {
"name": "direccion",
"folioId": 22
}
}
]
and in python i tip the next code
import json
f= open(prueba.json)
prueba = json.load(f)
prueba
print correctly content of json,but my idea is get only for example:
Nombre,Jose
and then use the parameters to build the table in pdf
I have tried the following
import json
json_data = []
with open('prueba.json') as json_file:
json_data = json.load(json_file)
for key, value in json_data.iteritems():
print key;
for item in value:
print item
for key, value in json_data.iteritems():
print key;
for item in value:
print item
But i have the next error:
AttributeError : 'list' object has no attribute 'iteritems'
I 'm trying to do something for them but I must get each data of json
To convert a JSON String to Python List, use json. loads() function. loads() function takes JSON Array string as argument and returns a Python List.
Steps for Appending to a JSON File In Python, appending JSON to a file consists of the following steps: Read the JSON in Python dict or list object. Append the JSON to dict (or list ) object by modifying it. Write the updated dict (or list ) object into the original file.
json_data = [] # your list with json objects (dicts)
for item in json_data:
for data_item in item['data']:
print data_item['name'], data_item['value']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With