"thing": [
{
"id": 1,
}
]
How do I assert that thing is an array with an object that contains ID
I tried
expect(response.thing).to.deep.include('id');
But that doesn't work
If you want to check:
the array contains String id
pm.expect(JSON.stringify(response.thing)).to.deep.include('id');
a nested object has key id
pm.expect(response.thing[0]).to.have.keys('id');
Related
I have a JSON request and I need to check that the value of one field is json.
I'm trying use regex:
"request": {
...
"bodyPatterns": [
{
"equalToJson": {
"field_1": "Value",
"field_2": "${json-unit.any-number}",
"field_3": "${json-unit.regex}^\u007B.\u007D$"
}
}
]
}
Also I have tried:
"field_3": "${json-unit.regex}\\{.\\}"
"field_3": "${json-unit.regex}{.}"
But none of this works. How will it be correct to determine that the value of the field contains JSON?
An example request that must match the stub:
{
"field_1": "Value",
"field_2": 100,
"field_3": {
"1.1": 2,
"1.2": null,
"1.3": null,
"1.4": null,
...
}
}
You can only use the regex placeholder against string values, so this won't work in this instance as the element's value is an object.
However, if you just want to say "I want this element to be present but don't care about the contents" you can use "${json-unit.ignore}" as the value.
If you do need to be more explicit I'd suggest still doing the above, but adding a second body matcher with a JSONPath expression stating what the object should contain.
I'm attempting to extract a value from list contained in a list of maps, but am
getting the following error: The operator '[]' isn't defined for the type 'Object'
From the following list, I'd like to access a value such as 'Pizza':
List<Map<String,Object>> questions = [
{
'question': 'What is your favorite food',
'answers': [
'Pizza',
'Tacos',
'Sushi',
],
},
];
When I try to print an answer within the list of answers, it doesn't work:
// Does not work
// The operator '[]' isn't defined for the type 'Object'
print(questions[0]['answers'][0]);
I'm able to save the answers list into a variable to of type list then print a specific list item:
// Works
List answerList = questions[0]['answers'];
print(answerList[0]);
Why doesn't the first way work, and how can I get this to work with one command?
Rather than returning an Object return a dynamic as it has an operator []
List<Map<String, dynamic>> questions = [
{
'question': 'What is your favorite food',
'answers': [
'Pizza',
'Tacos',
'Sushi',
],
},
];
I'm using json for modern c++.
And I have a json file which contains some data like:
{
"London": {
"Adress": "londonas iela 123",
"Name": "London",
"Shortname": "LL"
},
"Riga": {
"Adrese": "lidostas iela 1",
"Name": "Riga",
"Shortname": "RIX"
}
And I found out a way to modify the values of "Adrese", "Name", "Shortname".
As you can see I have "name" and key element name set to the same thing.
But I need to change both the key element and value "name".
So at the end when somehow in the code I modify it, it would look like:
{
"Something_New": {
"Adress": "londonas iela 123",
"Name": "Something_New",
"Shortname": "LL"
},
"Riga": {
"Adrese": "lidostas iela 1",
"Name": "Riga",
"Shortname": "RIX"
}
I've tried:
/other_code/
json j
/functions_for_opening_json file/
j["London"]["Name"] = "Something_New"; //this changes the value "name"
j["London"] = "Something_New"; //But this replaces "London" with
"Something_new" and deletes all of its inside values.
Then I tried something like:
for(auto& el : j.items()){
if(el.key() == "London"){
el.key() = "Something_New";}
}
But that didn't work either.
I would like something like j["London"] = "Something_new", and for it to keep all the values that originally was for "London".
The value associated with key "London" is the entire subtree json object containing the other 3 keys with their values. This line j["London"] = "Something_New"; does not change the key, "London" but its value. So you end up with the pair "London" : "Something new", overwriting the json subtree object. The keys are stored internally as std::map . Therefore you can't simply rename a key like that. Try:
void change_key(json &j, const std::string& oldKey, const std::string& newKey)
{
auto itr = j.find(oldKey); // try catch this, handle case when key is not found
std::swap(j[newKey], itr.value());
object.erase(itr);
}
And then
change_key(j, "London", "Something_New");
I need to pass 2 arrays and 2 vars. with API to LAmbda function
I get everytime this:
{"message": "Could not parse request body into json: Unexpected character (\',\' (code 44)): expected a valid value (number, String, array, object, \'true\', \'false\' or \'null\')\n at [Source: [B#5a648099; line: 5, column: 11]"}
My template mapping:
{
"items":
[
#foreach($elem in $input.params('items').split(','))
{
"ids": $elem.ids,
"contents": $elem.contents
}#if($foreach.hasNext),#end
#end
],
"QueryID": $input.params('QueryID'),
"nR": $input.params('nR')
}
Try quoting your values:
{
"items": [
#foreach($elem in $input.params('items').split(','))
{
"ids": "$elem.ids",
"contents": "$elem.contents"
}#if($foreach.hasNext),#end
#end
],
"QueryID": "$input.params('QueryID')",
"nR": "$input.params('nR')"
}
This looks like you are trying to pass items in the "params" field. If you are passing in items, QueryID, and nR every time, just put $input.json('$') (only that, remove everything else, even the surrounding {}). If that doesn't work, refer to #dave-maple's answer
I have just started working with JSON objects with the help of wxJSON. It works fine when I try to work a single JSON object (example given below), it works fine.
{
"wxWidgets" : 1
{
"Version" :1
{
"Major" : 2,
},
"Languages" :
[
"C++",
"Python",
"Perl",
"C#/Net"
]
}
}
However, when there are an array of values in the JSON object, I fail to parse the JSON object and extract the required key value pair.
Example :
[ {
"wxWidgets" : 1
{
"Version" :1
{
"Major" : 2,
},
"Languages" :
[
"C++",
"Python",
"Perl",
"C#/Net"
]
}
}
{
"wxWidgets" : 2
{
"Version" :2
{
"Major" : 3
},
"Languages" :
[
"java",
"j2ee",
"j2me"
]
}
}
]
when it is a single object, I access the elements like this:
wxJSONValue root; //this will have the json object created above.
wxString c = root[_T("wxWidgets")][_T("Version")].AsString(); // to get the version.
How do I do it in case there is an array of JSONOBJECTs as given in the second example?
Since you're working with an array,
root[_T("wxWidgets")][_T("Version")]
Doesn't deference the first object of the array (index 0 in this case) in the JSON object. The code needs to reflect that you're using an array now, so:
root[0][_T("wxWidgets")][_T("Version")]