Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get data from a list Json with python?

Tags:

python

json

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

like image 641
Colours123 Avatar asked May 28 '15 15:05

Colours123


People also ask

How do I read a JSON file and convert to list in Python?

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.

How do I add data to a JSON list in Python?

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.


1 Answers

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']
like image 103
konart Avatar answered Oct 06 '22 05:10

konart