1

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.

7 Answers 7

2

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

3 Comments

Thanks brainless coder for your explanation was very useful to understand what I was doing wrong. I think I was confusing because I was handling a Json array, so when tried with this, I forgot the diference.
There is a easiest way to convert that json object in a json array? I know the way with for loops, but I don't know if there is something simpler.
check the comment, you can check whether it is a array or not like that
1

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'])

1 Comment

Thanks @AdityaBalwani I was confusing it with a Json array as another user commented up.
1

Try this:

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

1 Comment

Thanks, Klaus D posted the same in the first coment, It was help full too.
1

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)

3 Comments

Not quite right... keys can be any hashable object and there is no conversion to string in the enumeration.
@tdelaney If you parse JSON, it will be a string. I could have pointed out the it relates to that more clearly.
Your answer helped also, I did not marked as answered, because the other user explained what I was doing wrong, but thanks for your time too.
0

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!

Comments

0

my case

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

Try data.decode()

2 Comments

The problem wasn't in loads. This isn't a solution to the problem.
as @tdelaney said the problem wasn't the load but thanks for your time anyway!
0
{
   "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

Comments

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.