-2

This is my json data:

{
  "AccountRegion": "testaccount",
  "AssumeRole": "arn",
  "Policies": [
    "hello",
    "world"
  ],
  "Region": "usa"
},
{
  "AccountRegion": "testaccount-2",
  "AssumeRole": "arn",
  "Policies": "anotherpolicy",
  "Region": "usa"
}

I am not able figure out how to loop through the JSON. I would like to loop through the data and get policies for each member in the dataset. How can I accomplish this?

  • looks duplicate of http://stackoverflow.com/questions/20199126/reading-json-from-a-file – Om Prakash Apr 17 '17 at 10:56
  • I have put the json data in a variable and I have looped through the json like this with no success `for data in table_data['Items']: region = data['Region'] assumerole = data['AssumeRole'] policies = data['Policies'] for policy in table_scan['Items']['policies']: print policy` – Srinivas Anant Apr 17 '17 at 11:18

2 Answers2

0

Try this.

arr = [{
      "AccountRegion": "testaccount",
      "AssumeRole": "arn",
      "Policies": [
        "hello",
        "world"
      ],
      "Region": "usa"
    },
    {
      "AccountRegion": "testaccount-2",
      "AssumeRole": "arn",
      "Policies": "anotherpolicy",
      "Region": "usa"
    }]

for x in range(0,len(arr)):
  print arr[x]['AccountRegion']
maiky_forrester
  • 598
  • 4
  • 19
0

You can try this

import json
parsedJson = json.loads(data)
for i in parsedJson:
    print parsedJson["Policies"]
Nathaniel Brown
  • 61
  • 2
  • 4
  • 11