Suppose the below is my response body
body={
"message": {
"Code": 26181,
"rollnos": [
{
"ID": 1439173,
"date_input": "2022-01-31 14:09:30.206748",
}
],
"start_date": "2022-01-31 00:00:00",
}
}
I want to set the ID value in a variable.
I have tried the below but it did not work out.
${json_response}= set variable ${xyz.json()}
${temp}= Set Variable ${json_response['message']}
${value_1}= Set Variable ${temp['rollnos']}
${register_id} = Set Variable ${value_1["ID"]
Can someone please help where I went wrong in this.
If you have body already as a dictionary you can use only the last 4 lines from the test example. I have included also the transformation from string to dictionary in case you need it:
*** Settings ***
Library Collections
Resource robot/resources/environment_resources.robot
*** Variables ***
${body_temp} {"message": {"Code": "26181", "rollnos": [{"ID": "1439173", "date_input": "2022-01-31 14:09:30.206748"}],"start_date": "2022-01-31 00:00:00"}}
*** Keywords ***
Converting a JSON File
${body} evaluate json.loads($body_temp) json
[Return] ${body}
*** Test Cases ***
Example
# Get body as dict
${body}= converting a json file
# Get the ID
${message} = Get From Dictionary ${body} message
${rollnos} = Get From Dictionary ${message} rollnos
${id} = Get From Dictionary ${rollnos}[0] ID
# Log the ID
Log To Console ID:${id}
Can someone please help where I went wrong in this.
You are treating ${temp['rollnos']} as a dictionary, but it is a list of dictionaries.
Instead of this:
${value_1}= Set Variable ${temp['rollnos']}
${register_id} = Set Variable ${value_1["ID"]
Do this, to get the ID of the first item in the dictionary:
${value_1}= Set Variable ${temp['rollnos'][0]}
# ^^^
${register_id} = Set Variable ${value_1["ID"]}
Well, when you say:
but it did not work out.
You don't mention what were the error messages.
This is my interpretation of your code:
${json_response}= set variable ${xyz.json()}
# ${json_response} is now a dictionary with content: body=&{message}
# So the next step would be:
${temp}= Set Variable ${json_response['body']['message']}
# And now we have: &{temp} = { Code:26181, &{rollnos}, ...
# The next code will be correct
${value_1}= Set Variable ${temp['rollnos']}
${register_id} = Set Variable ${value_1['ID']
But you got the value of ID, and you wanted to Set it, so that is another question.
Related
I am attempting to use the extractjson() method that includes email addresses in the source data (specifically the # symbol).
let T = datatable(MyString:string)
[
'{"user#domain.com": {"value":10}, "userdomain.com": { "value": 5}}'
];
T
| project extractjson('$.["user#domain.com"].value', MyString)
This results in a null being returned, changing the JSONPath to '$.["userdomain.com"].value' does return the correct result.
Results
I know the # sign is a used as the current node in a filter expression, does this need to be escaped when used with KQL?
Just as a side note, I run the same test using nodes 'jsonpath' package and this worked as expected.
const jp = require('jsonpath');
const data = {"user#domain.com": {"value":10}, "name2": { "value": 5}};
console.log(jp.query(data, '$["user#domain.com"].score'));
you can use the parse_json() function instead, and when you don't have to use extract_json():
print MyString = '{"user#domain.com": {"value":10}, "userdomain.com": { "value": 5}}'
| project parse_json(MyString)["user#domain.com"].value
MyString_user#domain.com_value
10
From the documentation: https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/extractjsonfunction
I am creating a composer from terraform where I want to pass a json as input variable
Terraform code:
software_config{
env_variables{
AIRFLOW_VAR_MYJSON ="{'__comment1__': 'This the global section', 'project_id':'testproject', 'gce_zone':'us-east1-c', 'gce_region':'us-east1','networkname':'vpc1', 'subnetwork':'https://www.googleapis.com/compute/v1/projects/testproject/regions/us-east1/subnetworks/subnet1'}"
}
}
I am trying to read the value of AIRFLOW_VAR_MYJSON in DAG , but it is not working as the value is not recognized as JSON.
I tried converting it and then deserializing it with following code:
JSONList = Variable.get("MYJSON")
jsonvar = json.dumps(JSONList)
setting_var = Variable.set("settings", jsonvar)
dag_config = Variable.get("settings", deserialize_json=True)
but it is not working.
I have also tried using
dag_config =json.loads(jsonvar)
then reading value as
project_id = dag_config["project_id"]
but I get error : "string indices must be integers"
Please suggest a way to resolve this.
NOTE : I know the gcloud command to set variables from json file but that is not working in my case as the project is in VPC and kubernetes clusters are giving timeout or handshake error, so I have ruled out use of this option
Valid JSON can only be " not '. Try switching the quotes.
A value can be a string in double quotes, or a number, or true or false or null, or an object or an array.
software_config{
env_variables{
AIRFLOW_VAR_MYJSON ="{\"__comment1__\": \"This the global section\", \"project_id\":\"testproject\", \"gce_zone\":\"us-east1-c\", \"gce_region\":\"us-east1\",\"networkname\":\"vpc1\", \"subnetwork\":\"https://www.googleapis.com/compute/v1/projects/testproject/regions/us-east1/subnetworks/subnet1\"}"
}
}
Or a little nicer way:
software_config {
env_variables {
AIRFLOW_VAR_MYJSON = jsonencode({
"__comment1__" = "This the global section",
"project_id" = "testproject",
"gce_zone" = "us-east1-c",
"gce_region" = "us-east1",
"networkname" = "vpc1",
"subnetwork" = "https://www.googleapis.com/compute/v1/projects/testproject/regions/us-east1/subnetworks/subnet1",
})
}
}
I have a response body I believe is an array it looks like this:
{"id":982,"form_stage":"1","customer_hash":"fffae165253f494a95bdb753ca87716c"}
In this case I want to get "customer_hash" and I have tried every combination of $..customer_hash $.customer_hash, etc but none of them work because this doesn't start with "response" or "result" I guess it is an array not an object(?).
Can anyone tell me what would be the JSONPath mapping to get the parameter "customer_hash"?
Try using:
$.customer_hash
Output:
[
"fffae165253f494a95bdb753ca87716c"
]
I am new to robotframework.
Here I am parsing the response using robotframework FOR loop and Set Variable If keyword. but as I am calling in loop. one of the variable get set to None. I want to avoid that.
Response body
{
'services': [
{
'service': 'postgres',,
'replicationStatus': 'completed'
},
{
'service': 'mongodb',
'replicationStatus': 'completed'
}
]
}
Parsing response_body
:FOR ${item} in #{response_body["services"]}
\ Log ${item}
\ ${mongodb_replication_status}= Set Variable If "${item['service']}"=="mongodb" ${item['replicationStatus']}
\ ${postgres_replication_status}= Set Variable If "${item['service']}"=="postgres" ${item['replicationStatus']}
Log ${mongodb_replication_status}
Log ${postgres_replication_status}
I get ouptput like below:
${mongodb_replication_status} = completed
${postgres_replication_status} = None
After each condition and value, the default value is given, in your case:
:FOR ${item} in #{response_body["services"]}
\ Log ${item}
\ ${mongodb_replication_status}= Set Variable If "${item['service']}"=="mongodb" ${item['replicationStatus']} default value
\ ${postgres_replication_status}= Set Variable If "${item['service']}"=="postgres" ${item['replicationStatus']} default value
Log ${mongodb_replication_status}
Log ${postgres_replication_status}
Another multi-condition example:
${variable_value}= Set Variable If ${var} == 1 one
... ${var} == 2 two
... other than one or two
If the default value is not available, the variable is assigned as None.
This is explained quite well in the documentation.
In the following code fragment I am trying to find in my responseBody a specific number by a variable and thus save its brother value.
However when I try to do it that way he answers me null and if I remove the quotes he answers me:
The parameter "n0" was used but not defined. Define parameters using the JsonPath.params(...) function
Response response = get(keyrest);
System.out.println("Value 0: " + response.asString());
semilla = response.path("seed");
String v1 = response.path("keys.find{it.number == 'n0'}.value");