skip to Main Content
[
    [
        {
            "indexnum": 677,
            "type": 1,
            "user": 54846,
            "push": 0,
            
        },
        {
            
            "indexnum": 2321,
            "type": 1,
            "user": 77924,
            "push": 0,
            
        }
        
    ]
]

import json


with open('./79553/combined.json', 'r',encoding='UTF-8') as infile:
    my_data = json.load(infile)


datalist1 = []

print(my_data['indexnum'])

That is my saved json file and I want to extract only indexnum from that file

and append them in a new list.

(ex. datalist1 = [677,2321,…])

Whether the file was read successfully, all items were outputted normally when I ‘print(datalist1)’.

but ‘print(my_data[‘indexnum’])’ has ‘list indices must be integers or slices, not str’ error occured.

How to solve them?

try:

I try my_data[0][‘indexnumber’]

same problem

4

Answers


  1. In your example, my_data is the whole json:

    
    [
        [
            {
                "indexnum": 677,
                "type": 1,
                "user": 54846,
                "push": 0,
                
            },
            {
                
                "indexnum": 2321,
                "type": 1,
                "user": 77924,
                "push": 0,
                
            }
            
        ]
    ]
    

    This is my_data[0]

    [
        {
            "indexnum": 677,
            "type": 1,
            "user": 54846,
            "push": 0,
            
        },
        {
            
            "indexnum": 2321,
            "type": 1,
            "user": 77924,
            "push": 0,
            
        }
        
    ]
    

    This is my_data[0][0]

    {
        "indexnum": 677,
        "type": 1,
        "user": 54846,
        "push": 0,
        
    }
    

    This is my_data[0][0][‘indexnum’]

    677
    
    Login or Signup to reply.
  2. Your JSON format is not uniformly equal bracketed. Either the bracketing is made uniform for the entire file or you access it with the correct list command. my_data[0][0]["indexnum"]

    I would highly recommend to use {} brackets througout the whole file.

    Login or Signup to reply.
  3. json_data = '''[
        [
            {
                "indexnum": 677,
                "type": 1,
                "user": 54846,
                "push": 0            
            },
            {
                
                "indexnum": 2321,
                "type": 1,
                "user": 77924,
                "push": 0
            }        
        ]
    ]'''
    
    import json
    my_data = json.loads(json_data)
    datalist = [x['indexnum'] for x in my_data[0]]
    print(datalist)
    

    outputs [677, 2321].

    But you need to remote trailing commas in lines like "push": 0, , because it is not valid JSON this way

    Login or Signup to reply.
  4. Your json data is formatted as list nested in list

    [
        [
            {
                "indexnum": 677,
                "type": 1,
                "user": 54846,
                "push": 0
                
            },
            {
                
                "indexnum": 2321,
                "type": 1,
                "user": 77924,
                "push": 0
                
            }
            
        ]
    ]
    

    Therefore, to get the attribute "indexnum" of the items, you may want to use the double loop after reading the json data

    import json
    import os
    
    with open('./79553/combined.json', 'r',encoding='UTF-8') as infile:
        my_data = json.load(infile)
    
    
    datalist1 = []
    for inner_list in my_data:
        for item in inner_list:
            #only append item has the key "indexnum"
            if item.get("indexnum"):
                datalist1.append(item.get('indexnum'))
    

    If you make sure that the first inner list is the only list you have, use this

    datalist1 = []
    for item in my_data[0]:
        #only append item has the key "indexnum"
        if item.get("indexnum"):
            datalist1.append(item.get('indexnum'))
    

    or the list comprehensive form

    datalist1 = [ item.get('indexnum') for item in my_data[0] if item.get('indexnum')]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search