How can i read json data

Lakshmi53
Tera Contributor
  {
  "jobs": [
        {
                "details": {
                "code""123"
            },      
           "userinfo": [
                {
                    "user""test"
                },
                {
                    "user""abc"
                }
            ]
            }
]
}
above is the sample JSON i have. using above json how can i get user details.
 
5 REPLIES 5

Maddysunil
Kilo Sage

@Lakshmi53 

Please like below:

 

# Sample JSON data
json_data = '''
{
  "jobs": [
        {
                "details": {
                "code": "123"
            },      
           "userinfo": [
                {
                    "user": "test"
                },
                {
                    "user": "abc"
                }
            ]
        }
    ]
}
'''

# Load JSON data
data = json.loads(json_data)

# Extract user details
user_details = []
for job in data["jobs"]:
    for userinfo in job["userinfo"]:
        user_details.append(userinfo)

# Print user details
for user_info in user_details:
    print(user_info)

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

Harish KM
Kilo Patron
Kilo Patron

Hi @Lakshmi53 please find below script:

var json = {
  "jobs": [
        {
                "details": {
                "code": "123"
            },      
           "userinfo": [
                {
                    "user": "test"
                },
                {
                    "user": "abc"
                }
            ]
            }
]
};


json.jobs.forEach(function(job) {
  job.userinfo.forEach(function(user) {
    gs.info(user.user);
  });
});
HarishKM_0-1710491085628.png

 

Regards
Harish

how can i call this in scripted rest api

Hi @Lakshmi53 you need to call after you receive JSON response

Regards
Harish