Home > Mobile >  How do I get back the value from one of the nested dictionary from JSON file
How do I get back the value from one of the nested dictionary from JSON file

Time:12-21

i am new to python and json. I have this issue where i am unable to get the values of 'labels' print out. It is under a nested dictionary 'result' .

Code that i have tried for i in data['result']: print("Value:", i['value'])

        values = i['value']


        print("X:", values['x'])
        print("Y:", values['y'])
        print("Width:", values['width'])
        print("Height:", values['height'])

Output from the above code

        However when i try to do print("labels:", values['labels'])

        it return a keyerror: 'labels'



        for item in values:
             print(item)

this is the output shown

x y width height rotation x y width height rotation labels

May i ask how do i go out to print out the 'labels' value?

The json files is as follows:

{
  "id": 9,
  "created_username": "",
  "created_ago": "3\u00a0weeks, 3\u00a0days",
  "completed_by": {
    "id": 1,
    "first_name": "",
    "last_name": "",
    "email": ""
  },
  "task": {
    "id": 1,
    "data": {
      "image": "/data/upload/2/adf8540d-2-33.png"
    },
    "meta": {},
    "created_at": "2022-11-21T14:52:22.478537Z",
    "updated_at": "2022-11-22T12:51:18.105745Z",
    "is_labeled": true,
    "overlap": 1,
    "inner_id": 1,
    "total_annotations": 1,
    "cancelled_annotations": 0,
    "total_predictions": 0,
    "comment_count": 0,
    "unresolved_comment_count": 0,
    "last_comment_updated_at": null,
    "project": 2,
    "updated_by": 1,
    "file_upload": 70,
    "comment_authors": []
  },
  "result": [
    {
      "original_width": 512,
      "original_height": 512,
      "image_rotation": 0,
      "value": {
        "x": 50.898958419872464,
        "y": 44.0069438675168,
        "width": 2.397222452993284,
        "height": 2.0975696463691196,
        "rotation": 0
      },
      "id": "pgaiV8NjWC",
      "from_name": "rect",
      "to_name": "image",
      "type": "rectangle",
      "origin": "manual"
    },
    {
      "original_width": 512,
      "original_height": 512,
      "image_rotation": 0,
      "value": {
        "x": 50.898958419872464,
        "y": 44.0069438675168,
        "width": 2.397222452993284,
        "height": 2.0975696463691196,
        "rotation": 0,
        "labels" [
          "LM"*
        ]
      },
      "id": "pgaiV8NjWC",
      "from_name": "labels",
      "to_name": "image",
      "type": "labels",
      "origin": "manual"
    }
  ],
  "was_cancelled": false,
  "ground_truth": false,
  "created_at": "2022-11-22T12:51:18.001777Z",
  "updated_at": "2022-11-22T12:51:18.001777Z",
  "lead_time": 21.556,
  "project": 2,
  "parent_prediction": null,
  "parent_annotation": null
}

CodePudding user response:

Use the json module

import json

dict = json.load(json_filename)

Then you should access the values by the dictionary keys. Be aware that the result value is a list. Therefore the index 1 is necessary.

values = dict['result'][1]['value']
print("X:", values['x'])
  • Related