26

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

5
  • "get only for example". Please describe in more detail which data you want to extract from the JSON file and how you want to output this data. Please edit your question and include a small sample of how the output is supposed to look like. Commented May 28, 2015 at 15:14
  • 2
    what is json_data? Commented May 28, 2015 at 15:14
  • That's because json_data is a list, not a dictionary. Commented May 28, 2015 at 15:30
  • Your JSON code is invalid. When I try to json.load(json_file) I get a ValueError: Expecting , delimiter: line 5 column 59 (char 104). Commented May 28, 2015 at 15:34
  • The problem with your JSON code is that whole numbers can't have a leading zero — like the "attId"s do in your example — see the syntax diagram of number at json.org. Commented May 28, 2015 at 15:49

2 Answers 2

28
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']
Sign up to request clarification or add additional context in comments.

5 Comments

I have de next error Traceback (most recent call last): File "prueba.py", line 5, in <module> json_data = json.load(json_file) File "C:\Python27\lib\json_init_.py", line 290, in load **kw) File "C:\Python27\lib\json_init_.py", line 338, in loads return _default_decoder.decode(s) File "C:\Python27\lib\json\decoder.py", line 368, in decode raise ValueError(errmsg("Extra data", s, end, len(s))) ValueError: Extra data: line 53 column 1 - line 53 column 2 (char 1736 - 1737)
Do you load json file as shown as in your post? If so - can you provide you json file? You can post it's content (or part of it) to Pastebin, for example
On the second thought - your json is invalid. You have values like 06 instead of 6. You have to fix this first. You can validate your json using jsonlint.com
Yes, because of the leading zeros in the numbers it's invalid. If a number starts with 0, it will be interpreted as octal, or base 8. Octal just uses digits from 0 to 7. So, it fails at 08 and 09. But how can we fix it? ...
another question i have work with reportlab but i don´t know as i can load this information in a table(PDF)
0

Something like

for key, value in json_data.iteritems():
    print key
    if isinstance(value, (list, tuple)):
        for item in value: 
            print item
    if isinstance(value, (dict)):
        for value_key,value_value in value.iteritems(): 
            print value_key,str(value_value)

Can be improved to manage more types and can be made recursive.

1 Comment

I get de same error AttributeError : 'list' object has no attribute 'iteritems'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.