Home > OS >  Iterate through json response
Iterate through json response

Time:01-27

I'm trying to iterate through a json response, method is get.

this is the json

{
    "snapshots" : [ 
        {"snapshot" : "First"},
        {"snapshot" : "Second"}
    ]
}

I would like to know if any of you have an easy way to iterate through this and get the value of "snapshot" key, with python 3.X thanks in advance

CodePudding user response:

Here an example

import json

var= """
{
 "snapshots" : [
 {
 "snapshot" : "First"},
 {
 "snapshot" : "Second"}

 ]
 }

 """

data = json.loads(var)

for snapshot in data["snapshots"]:
        print(f'Value :{snapshot["snapshot"]}')

The output that i got :

Value :First
Value :Second

CodePudding user response:

obj = {
    "snapshots" : [ 
    {
    "snapshot" : "First"
    },
    {
    "snapshot" : "Second"
    }]
    }

for snapshot in obj['snapshots']:
    print(snapshot['snapshot'])
  •  Tags:  
  • Related