0

I have a single entry in a JSON file. Within the "objects":[] list, I have "title":. I want to create a list that appends the value of every "title": in every JSON list. In the end, my goal is to have a list like this:

titles = [Mastercard, Mastercard, Alienware, Oppo, Alienware, ...]

{
  "ID": "ckksku7b800003e5m5vne0v7c",
  "DataRow ID": "ckkskgs561yh00sgog3vf1v29",
  "Labeled Data": "https://storage.labelbox.com/ckkqt5y5ulks20757tbduodbw%2Fe2510252-76ba-df14-193b-43d4527d705a-Screenshot%202021-02-05%20at%2017.46.56.png?Expires=1614084797277&KeyName=labelbox-assets-key-1&Signature=e69tqkxYhNNWkJL3TaxA9wjtYGM",
  "Label": {
    "objects": [
      {
        "featureId": "ckkskpirs0koo0y771e2veiik",
        "schemaId": "ckkskil310j1o0y8r0fu252jy",
        "title": "Mastercard",
        "value": "mastercard",
        "color": "#006FA6",
        "bbox": {
          "top": 631,
          "left": 891,
          "height": 25,
          "width": 40
        },
        "instanceURI": "https://api.labelbox.com/masks/feature/ckkskpirs0koo0y771e2veiik?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja2txdDV5NmFtbm1iMDc0MGpsem03d2RwIiwib3JnYW5pemF0aW9uSWQiOiJja2txdDV5NXVsa3MyMDc1N3RiZHVvZGJ3IiwiaWF0IjoxNjEyODc1MTk3LCJleHAiOjE2MTU0NjcxOTd9.P-MyOgZidmSZSTLfhwpSsRfOaovsReP5MzpDtsrnWs0"
      },
      {
        "featureId": "ckkskqn410l530y770svj1z3g",
        "schemaId": "ckkskil310j1o0y8r0fu252jy",
        "title": "Mastercard",
        "value": "mastercard",
        "color": "#006FA6",
        "bbox": {
          "top": 374,
          "left": 1248,
          "height": 22,
          "width": 33
        },
        "instanceURI": "https://api.labelbox.com/masks/feature/ckkskqn410l530y770svj1z3g?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja2txdDV5NmFtbm1iMDc0MGpsem03d2RwIiwib3JnYW5pemF0aW9uSWQiOiJja2txdDV5NXVsa3MyMDc1N3RiZHVvZGJ3IiwiaWF0IjoxNjEyODc1MTk3LCJleHAiOjE2MTU0NjcxOTd9.P-MyOgZidmSZSTLfhwpSsRfOaovsReP5MzpDtsrnWs0"
      },
      {
        "featureId": "ckkskshox0luo0y77gozzbuzu",
        "schemaId": "ckksks2sh0lrl0y8rcq970g3u",
        "title": "Alienware",
        "value": "alienware",
        "color": "#A30059",
        "bbox": {
          "top": 436,
          "left": 1901,
          "height": 31,
          "width": 86
        },
        "instanceURI": "https://api.labelbox.com/masks/feature/ckkskshox0luo0y77gozzbuzu?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja2txdDV5NmFtbm1iMDc0MGpsem03d2RwIiwib3JnYW5pemF0aW9uSWQiOiJja2txdDV5NXVsa3MyMDc1N3RiZHVvZGJ3IiwiaWF0IjoxNjEyODc1MTk3LCJleHAiOjE2MTU0NjcxOTd9.P-MyOgZidmSZSTLfhwpSsRfOaovsReP5MzpDtsrnWs0"
      },
      {
        "featureId": "ckksktu8c0mhq0y8r6a82cjgm",
        "schemaId": "ckkskil300j1g0y8r5kbgguk9",
        "title": "OPPO",
        "value": "oppo",
        "color": "#1CE6FF",
        "bbox": {
          "top": 1101,
          "left": 1837,
          "height": 19,
          "width": 51
        },
        "instanceURI": "https://api.labelbox.com/masks/feature/ckksktu8c0mhq0y8r6a82cjgm?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja2txdDV5NmFtbm1iMDc0MGpsem03d2RwIiwib3JnYW5pemF0aW9uSWQiOiJja2txdDV5NXVsa3MyMDc1N3RiZHVvZGJ3IiwiaWF0IjoxNjEyODc1MTk3LCJleHAiOjE2MTU0NjcxOTd9.P-MyOgZidmSZSTLfhwpSsRfOaovsReP5MzpDtsrnWs0"
      },
      {
        "featureId": "ckkxvfpv305j00y8k1hvn03e8",
        "schemaId": "ckksks2sh0lrl0y8rcq970g3u",
        "title": "Alienware",
        "value": "alienware",
        "color": "#A30059",
        "bbox": {
          "top": 505,
          "left": 2276,
          "height": 23,
          "width": 92
        },
        "instanceURI": "https://api.labelbox.com/masks/feature/ckkxvfpv305j00y8k1hvn03e8?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJja2txdDV5NmFtbm1iMDc0MGpsem03d2RwIiwib3JnYW5pemF0aW9uSWQiOiJja2txdDV5NXVsa3MyMDc1N3RiZHVvZGJ3IiwiaWF0IjoxNjEyODc1MTk3LCJleHAiOjE2MTU0NjcxOTd9.P-MyOgZidmSZSTLfhwpSsRfOaovsReP5MzpDtsrnWs0"
      }
    ],
    "classifications": []
  },
  ...
}

Update This is what I changed my code to per suggestion

    for i in range(0, len(data)):
        line = data[i]
        objects = line['Label']['objects']
        classes = [o['title'] for d in data for o in objects]
        print(classes)

This is the output:

['Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', ...
'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard']

['OPPO', 'OPPO', 'OPPO', 'OPPO', 'OPPO', 'OPPO', 'OPPO', 'OPPO', 'OPPO', 'OPPO', 'OPPO', 'OPPO', 'OPPO', 'OPPO', 'OPPO', 'OPPO', 'OPPO', 'OPPO', ...
'OPPO', 'OPPO', 'OPPO', 'OPPO', 'OPPO', 'OPPO', 'OPPO', 'OPPO', 'OPPO', 'OPPO', 'OPPO', 'OPPO', 'OPPO', 'OPPO', 'OPPO']

['Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard',  ...
'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard']

['Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', ...
'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard', 'Mastercard']

['Mastercard', 'Mastercard', 'Alienware', 'OPPO', 'Alienware', 'Mastercard', 'Mastercard', 'Alienware', 'OPPO', 'Alienware', 'Mastercard', 'Mastercard', 'Alienware', 'OPPO', 'Alienware', 
...
'Mastercard', 'Mastercard', 'Alienware', 'OPPO', 'Alienware']

2 Answers 2

2

It seems you should be able to do this with a list comprehension:

titles = [o['title'] for d in data for o in d['Label']['objects']]

Output (for a list containing the above object):

['Mastercard', 'Mastercard', 'Alienware', 'OPPO', 'Alienware']
Sign up to request clarification or add additional context in comments.

10 Comments

Check the edits. I am running this for multiple JSON entries.
@oo92 sorry I didn't make it clear, you should replace your entire loop, including the for i in range(0, len(data)): with this one line.
@oo92 see for example rextester.com/JWV90069
KeyError: 'objects' when I replace everything with your code
@oo92 is your data value not the same as the one in that demo?
|
0

Simple way is :

title = []

for j in data['Label']['objects']:
    title.append(j['title'])

output:

['Mastercard', 'Mastercard', 'Alienware', 'OPPO', 'Alienware']

if data is list of dict:

title = []

for d in data:
    for obj in d['Label']['objects']:
        title.append(obj['title'])

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.