1

I'm using IBM's Watson Visual Recognition API and I'm trying to format the output JSON data to display only the "Class","Score" and both of their values

I've used

print(json.dumps(classes_result, indent=2))

to get the output:

  "images": [
    {
      "classifiers": [
        {
          "classifier_id": "default",
          "name": "default",
          "classes": [
            {
              "class": "honey buzzard",
              "score": 0.639,
              "type_hierarchy": "/animal/bird/bird of prey/hawk/honey buzzard"
            },
            {
              "class": "hawk",
              "score": 0.891
            },
            {
              "class": "bird of prey",
              "score": 0.918
            },

Oblivion
  • 7,176
  • 2
  • 14
  • 33
Benz.Lu
  • 21
  • 3
  • Possible duplicate of [Filter dict to contain only certain keys?](https://stackoverflow.com/questions/3420122/filter-dict-to-contain-only-certain-keys) – AnsFourtyTwo Aug 26 '19 at 15:03

1 Answers1

0

Try this, with nested loops over your dict:

for image in images:
    classifiers = image['classifiers']
    for classifier in classifiers: 
        classes = classifier['classes']
        for _class in classes: 
            class_value = _class['class']
            score_value = _class['score']
            print(class_value, score_value)
sashaboulouds
  • 1,566
  • 11
  • 16