0

I am trying to read the JSON file in python and it is successfully however some top values are skipped. I am trying to debug the reason. Here is the the code.

data = json.load(open('pre.txt'))

for key,val in data['outputs'].items():
    print key
    print data['outputs'][key]['feat_left']

EDIT Here is the snapshot the file. I want to read key and feat_left for outputs

{
  "outputs": {
    "/home/113267806.jpg": {
      "feat_left": [
        2.369331121444702, 
        -1.1544183492660522
      ], 
      "feat_right": [
        2.2432730197906494, 
        -0.896904468536377
      ]
    }, 
    "/home/115061965.jpg": {
      "feat_left": [
        1.8996189832687378, 
        -1.3713303804397583
      ], 
      "feat_right": [
        1.908974051475525, 
        -1.4422794580459595
      ]
    }, 
    "/home/119306609.jpg": {
      "feat_left": [
        -0.7765399217605591, 
        -1.690917730331421
      ], 
      "feat_right": [
        -1.1964678764343262, 
        -1.9359161853790283
      ]
    }
  }
 }

P.S: Thanks to Rahul K P for the code

5
  • please provide a minimal reproducible example, that shouldn't be too hard, and don't link to external file. Commented Feb 20, 2017 at 16:48
  • Can you make a small example json string and post it here? We can't log into your dropbox. Commented Feb 20, 2017 at 16:49
  • Pls define the exptected output what you await. At the moment, you print the keys and the array that is stored below the key "feat_left". Also, you can output "feat_left" by using val["feat_leaft"] and val["feat_right"] because the object containing the "feat"-arrays will be converted to value by using the shown for loop. Commented Feb 20, 2017 at 16:51
  • I want to /home/113267806.jpg and feat_left @Supahupe for outputs Commented Feb 20, 2017 at 16:55
  • 113267806.jpg is in the output. dict is unordered, so it won't appear in the same position as in the original json, but it is there. Commented Feb 20, 2017 at 17:01

4 Answers 4

2

No top values are skipped. There are 45875 items in your data['output'] object. Try the following code:

len(data['outputs'].items())

And there are exactly 45875 items in your JSON file. Just note that JSON object is an unordered collection in python, like dict.

Sign up to request clarification or add additional context in comments.

1 Comment

the answer just kill me, as I was expecting it to be an ordered list. Thanks. @Hossein
2

i think you want:

for key, val in data['outputs'].items():
    if 'feat_left' in val:
        print key, data['outputs'][key]['feat_left']

3 Comments

This is a better solution than mine above - didnt do python for some weeks
I think Hossein is the one I am looking for. There is no problem with the code. Just note that JSON object is an unordered collection in python, like dict.
If you need ordered dictionary keys, I would recommend using a collections.OrderedDict
0

If you just want to print the content of the file by using a for-loop, you can try like this:

data = json.load(open('pre.txt')
for key,val in data['outputs'].items():
      print key
      print val[0] #this will print the array and its values below "feat_left", if the json is consistent

A more robust solution could look like this:

data = json.load(open('pre.txt')
for key,val in data['outputs'].items():
      print key
      for feat_key, feat_val in val.items():
            if feat_key == 'feat_left':
                 print feat_val

Code is untested, give it a try.

Comments

0

Try this:

for key, val in data['outputs'].items():
    print key, val['feat_left'] 
#output /home/119306609.jpg [-0.7765399217605591, -1.690917730331421]

This get every key and element within 'feat_left' Then you can display in the page however you like

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.