GEPD
GEPD

Reputation: 308

Handling JSON file with python

I'm developing a plugin for sublime text and I'm have some problems handling a json file.

This is the json file

{
   "desktop":{
      "name":"build",
      "upload":{
         "maximum":512,
      },
      "load":{
         "core":"i7",
      }
   },
   "table":{
      "name":"clean",
      "upload":{
         "maximum":1024,
      },
      "load":{
         "core":"i3",
      }
   }
}

An this is how I load it

with open(self.path, 'r') as f:
    text = f.read()
    datas = json.loads(text)

    for data in datas:
        print(data['desktop'])

The output show me this error

TypeError: string indices must be integers

But when I try with data[0] I get one character from the json file.

I know I'm doing something wrong with the parse and encoding, but I don't know where. I been all day reading previous posts from here and trying to figure out what is the right way to do it.

I'll apreciate any help.

Upvotes: 1

Views: 618

Answers (7)

yyh
yyh

Reputation: 1

{
   "desktop":{
      "name":"build",
      "upload":{
         "maximum":512
      },
      "load":{
         "core":"i7"
      }
   },
   "table":{
      "name":"clean",
      "upload":{
         "maximum":1024
      },
      "load":{
         "core":"i3"
      }
   }
}

code image

json when one of the attributes that should be no comma

Upvotes: 0

brainless coder
brainless coder

Reputation: 6430

First you need to understand that this is not a JSON array. This is a json object -

{
   "desktop":{
      "name":"build",
      "upload":{
         "maximum":512,
      },
      "load":{
         "core":"i7",
      }
   },
   "table":{
      "name":"clean",
      "upload":{
         "maximum":1024,
      },
      "load":{
         "core":"i3",
      }
   }
}

so the code -

for data in datas:
    print(data['desktop'])

is not iterating through the array items, it is actually iterating through the properties/attributes of that object datas. So doing this gives you item already -

for data in datas:
    print(datas[data])

but if you want to manipulate and find each attribute you can check like this -

for data in datas:
    print(data)
    print(datas[data])

    if data == 'desktop':
        print('do somthing')

Here is an online view of it- https://repl.it/B61g

EDIT

If datas can be array or object, you can check like this -

if type(datas) is list:
    # datas is list do whatever you like
else:
    # datas is obj

Upvotes: 2

yyh
yyh

Reputation: 1

my case

data = res.read() //<<--HTTP DATA
resDic = json.loads(data.decode())
if resDic["code"] == 1000:
    //some thing

Try data.decode()

Upvotes: 0

Klaus D.
Klaus D.

Reputation: 14369

Looping through a dictionary will gave you the keys as strings only. What you might want to have is:

for key, value in datas.items()
    print(key, value)

Upvotes: 1

Incerteza
Incerteza

Reputation: 34884

Try this:

for k, v in datas.iteritems():
   # your stuff

Upvotes: 1

tdelaney
tdelaney

Reputation: 77347

Try print(type(datas)), you'll see its the outer dict. Enumerating the dict enumerates its keys which are strings. data is a string and not surprisingly, data['desktop'] fails. Many questions are answered with a few well-placed prints!

Upvotes: 0

Aditya Balwani
Aditya Balwani

Reputation: 23

Iterating through a JSON object using a for each loop will give you a key in the json object. Hence, in your loop data will refer to the string 'desktop' itself (and other keys in the object) and hence when you try to do data['desktop'] you are actually attempting to get the index of a string but since string indexes must be numbers, it fails

To print the 'desktop' key from the file you can simply write print(datas['desktop'])

Upvotes: 1

Related Questions