-2

This is my JSON.

{"recipes":{"58788":{"name”:”test1”,”status":"SUCCESSFUL","kitchen":"us-east","active":"YES","created_at":1514699619,"interval":5,"use_legacy_notifications":false},"40888":{"name":"Projects_1”,”status":"SUCCESSFUL","kitchen":"us-east","active":"YES","created_at":1493123978,"interval":10,"use_legacy_notifications":false},"40882":{"name":"Departments_2”,”status":"SUCCESSFUL","kitchen":"us-east","active":"YES","created_at":1493117601,"interval":10,"use_legacy_notifications":false},"59634":{"name”:”4synthetic","status":"SUCCESSFUL","kitchen":"us-east","active":"YES","created_at":1516071598,"interval":10,"use_legacy_notifications":false},"47635":{"name”:”Desitnation Search","status":"SUCCESSFUL","kitchen":"eu","active":"YES","created_at":1501672231,"interval":5,"use_legacy_notifications":false},"47437":{"name":"Gateway_5”,”status":"SUCCESSFUL","kitchen":"us-west","active":"YES","created_at":1501411588,"interval":10,"use_legacy_notifications":false},"65568":{"name":"Validation","status":"SUCCESSFUL","kitchen":"us-west","active":"YES","created_at":1522583593,"interval":5,"use_legacy_notifications":false}},"counts":{"total":7,"limited":7,"filtered":7}}

I need to extract only name and Status from the above.

Rakesh
  • 81,458
  • 17
  • 76
  • 113
  • 2
    The JSON posted here seems invalid. Did you try anything? Any code so far? – Andrej Kesely Jul 11 '18 at 18:29
  • How is it stored in python? in a dictionary that came through the requests package or is it a string? If it is one long string use regex, if it is a dictionary you should be able to index it. – lwileczek Jul 11 '18 at 18:31
  • you have wrong "double quotes" .. mixture of `"` and `”` fix the double quotes around many of these lines: example: `"Gateway_5”,”status"` see how the double quotes are? That's why your json is invalid – Amit Jul 11 '18 at 18:32
  • Your JSON format is not correct. See the correct format here: https://stackoverflow.com/questions/51292047/parsing-value-from-json-using-python/51292189#51292189 – Rajat Jain Jul 11 '18 at 18:37
  • Possible duplicate of [Parsing values from a JSON file?](https://stackoverflow.com/questions/2835559/parsing-values-from-a-json-file) – noɥʇʎԀʎzɐɹƆ Jul 11 '18 at 23:58

2 Answers2

1
import json
data = '''{"recipes":{"58788":{"name":"test1","status":"SUCCESSFUL","kitchen":"us-east","active":"YES","created_at":1514699619,"interval":5,"use_legacy_notifications":false},"40888":{"name":"Projects_1","status":"SUCCESSFUL","kitchen":"us-east","active":"YES","created_at":1493123978,"interval":10,"use_legacy_notifications":false},"40882":{"name":"Departments_2","status":"SUCCESSFUL","kitchen":"us-east","active":"YES","created_at":1493117601,"interval":10,"use_legacy_notifications":false},"59634":{"name":"4synthetic","status":"SUCCESSFUL","kitchen":"us-east","active":"YES","created_at":1516071598,"interval":10,"use_legacy_notifications":false},"47635":{"name":"Desitnation Search","status":"SUCCESSFUL","kitchen":"eu","active":"YES","created_at":1501672231,"interval":5,"use_legacy_notifications":false},"47437":{"name":"Gateway_5","status":"SUCCESSFUL","kitchen":"us-west","active":"YES","created_at":1501411588,"interval":10,"use_legacy_notifications":false},"65568":{"name":"Validation","status":"SUCCESSFUL","kitchen":"us-west","active":"YES","created_at":1522583593,"interval":5,"use_legacy_notifications":false}},"counts":{"total":7,"limited":7,"filtered":7}}'''
data = json.loads(data)
for k,v in data["recipes"].items():
    print(v["name"], v["status"])

Output:

(u'test1', u'SUCCESSFUL')
(u'4synthetic', u'SUCCESSFUL')
(u'Validation', u'SUCCESSFUL')
(u'Desitnation Search', u'SUCCESSFUL')
(u'Departments_2', u'SUCCESSFUL')
(u'Gateway_5', u'SUCCESSFUL')
(u'Projects_1', u'SUCCESSFUL')
  • Note: I have converted to " using .replace("”", '"') and then used json.loads
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

Your JSON format is not valid. I have corrected the JSON format. After changing to " try loading it.

{
   "recipes":{
      "58788":{
         "name":"test1",
         "status":"SUCCESSFUL",
         "kitchen":"us-east",
         "active":"YES",
         "created_at":1514699619,
         "interval":5,
         "use_legacy_notifications":false
      },
      "40888":{
         "name":"Projects_1",
         "status":"SUCCESSFUL",
         "kitchen":"us-east",
         "active":"YES",
         "created_at":1493123978,
         "interval":10,
         "use_legacy_notifications":false
      },
      "40882":{
         "name":"Departments_2",
         "status":"SUCCESSFUL",
         "kitchen":"us-east",
         "active":"YES",
         "created_at":1493117601,
         "interval":10,
         "use_legacy_notifications":false
      },
      "59634":{
         "name":"4synthetic",
         "status":"SUCCESSFUL",
         "kitchen":"us-east",
         "active":"YES",
         "created_at":1516071598,
         "interval":10,
         "use_legacy_notifications":false
      },
      "47635":{
         "name":"Desitnation Search",
         "status":"SUCCESSFUL",
         "kitchen":"eu",
         "active":"YES",
         "created_at":1501672231,
         "interval":5,
         "use_legacy_notifications":false
      },
      "47437":{
         "name":"Gateway_5",
         "status":"SUCCESSFUL",
         "kitchen":"us-west",
         "active":"YES",
         "created_at":1501411588,
         "interval":10,
         "use_legacy_notifications":false
      },
      "65568":{
         "name":"Validation",
         "status":"SUCCESSFUL",
         "kitchen":"us-west",
         "active":"YES",
         "created_at":1522583593,
         "interval":5,
         "use_legacy_notifications":false
      }
   },
   "counts":{
      "total":7,
      "limited":7,
      "filtered":7
   }
}

To get your values you can do as below. Suppose you have loaded your JSON data in variable my_data.

my_data = json.loads(data)
my_recipes=my_data['recipes']
for i in my_recipes.keys():
    var_name = recipes[i]['name']
    var_status=recipes[i]['status']
Rajat Jain
  • 1,339
  • 2
  • 16
  • 29