I have a dictionary below. I want to convert the string to list in Body section ("Body":"{"abc": "test"}"). My requirement is that I will post the whole dictionary and it will convert that into valid json object.
details = {
"bb":[
{
"bb_name":[
"baca"
]
}
],
"spd":[
{
"id_":1,
"schema":{
"method":"GET",
"url":"http://api.myapi.com/12232",
"Headers":[
],
"Parameters":[
],
"Body":"{\"abc\": \"test\"}"
}
}
]
}
Related
I have a json data file from which I need to fetch only the id attribute value into a separate list variable. I tried for loop but not able to get the required data using terraform. Can someone tell how to fetch the id part ? your help is much appreacited.
code:
locals {
data = jsondecode(file("./data.json"))[*]
sections = [ for item in local.data : item ]
}
output "ids" {
value = [for a in local.sections[0]: a]
}
json file:
{
"c":[
{
"id":"6",
"key":"c",
"name":"s01"
}
],
"l":{
"id":"7",
"key":"l",
"name":"s02"
},
"m":{
"id":"8",
"key":"mp",
"name":"s03"
},
"n":{
"id":"5",
"key":"cn",
"name":"s04"
},
"od":"odk",
"s":{
"id":"9",
"key":"cs",
"name":"s05"
},
"ss":{
"id":"1",
"key":"ss",
"name":"s06"
},
"in":{
"id":"65",
"key":"cn",
"name":"s07"
},
"b":{
"id":"2",
"key":"cb",
"name":"s08"
}
}
Not sure if "od":"odk", is mistake or not, but you can do the following:
locals {
data = jsondecode(file("./data.json"))
}
output "ids" {
value = [for v in values(local.data): v.id if can(v.id)]
}
I have a JSON representation that looks like the following:
{
"results": [
{
"vulnerabilities": [
],
}
]
}
I tried to output just the vulnerabilities portion, but the following doesnt work:
for key, value in json_object.items():
print(key, ' : ', value)
This prints out the whole results but not just the vulnerabilities
Assuming multiple dicts in results, each with a vulnerabilities key, you can do:
json_object = {
"results": [
{
"vulnerabilities": [
],
}
]
}
for result in json_object['results']:
for vuln in result['vulnerabilities']:
print(vuln)
dataset_bindings = {
"infra":[
"group:infra-team#xxxx.com",
],
"finance":[
"group:finance-data#xxx.com",
],
"marketing": [
"group:marketing#xxx.com"
]
}
How can I get all the emails as string. I need to loop thru the dict and get the values and convert those values to string.
You can do this with values and flatten:
locals {
dataset_bindings = {
"infra":[
"group:infra-team#xxxx.com",
],
"finance":[
"group:finance-data#xxx.com",
],
"marketing": [
"group:marketing#xxx.com"
]
}
list_of_emails = flatten(values(local.dataset_bindings))
}
results in:
list_of_emails = [
"group:finance-data#xxx.com",
"group:infra-team#xxxx.com",
"group:marketing#xxx.com",
]
I have the following JSON object r2,
[
{
"reserva_id":"200",
"estancias":[
{
"reserva_estancia":"266",
"huespedes":[
{
"reserva_huesped":"272",
"reserva_estancia":"266",
"numero_huesped":"1",
"huesped":"123",
"huesped_nombre":"dos dos, dos",
"rfid":null
},
{
"reserva_huesped":"276",
"reserva_estancia":"266",
"numero_huesped":"2",
"huesped":"183",
"huesped_nombre":"MUESTRA MUESTRA, CARMEN",
"rfid":null
}
]
}
]
},
{
"reserva_id":"201",
"estancias":[
{
"huespedes":[
{
"reserva_huesped":"273",
"reserva_estancia":"267",
"numero_huesped":"1",
"huesped":"148",
"huesped_nombre":"MUESTRA MUESTRA, CARMEN",
"rfid":null
},
{
"reserva_huesped":"277",
"reserva_estancia":"267",
"numero_huesped":"2",
"huesped":"187",
"huesped_nombre":"TEST TEST, TESTCIVITFUN",
"rfid":null
}
]
}
]
}
]
I am trying to get the first huesped for each reservation, and for that I am using the following script, to create a list called profiles and store profileId's:
def profiles = jsonpath(r2,'$..[:].estancias[:].huespedes[0].huesped')
The output should be the following:
[
"123",
"148"
]
However, when I print profiles.text I get all the content of the estancias object, instead of just the huesped number.
When using Jayway's JSONPath like this I get the desired oputput:
$..[*].estancias[*].huespedes[0].huesped
You can try the path expression with your JSON here online.
I have a json data like:
{
"cse_thumbnail": [
{
"width": "188",
"height": "268",
"src": "http://abc.dk"
}
],
"metatags": [
{
"referrer": "origin-when-cross-origin",
"og:image": "http://def.dk"
}
],
"cse_image": [
{
"src": "http://ghi.dk"
}
]
}
There are 3 array lists coming in the JSON. I want to check if the corresponding keys are existing when I'm getting the response:
cse_thumbnail
metatags
cse_image
I'm tried all the key:value pair check in python like (hasattr, key in list etc) which are not working at all.
Please help to get it resolved.
You can use in to check if a key is present
import json
with open('data.json') as thing:
data = json.load(thing)
keys = ('cse_thumbnail', 'metatags', 'cse_image')
for key in keys:
print(key in data)