Skip to content
Advertisement

How to get data from a list Json with python

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

Advertisement

Answer

json_data = [] # your list with json objects (dicts)

with open('prueba.json') as json_file:
   json_data = json.load(json_file)

for item in json_data:
    for data_item in item['data']:
        print data_item['name'], data_item['value']
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement