Validate nested response body rest assured - json

I know that there is a lot of information regarding validations in rest assure but unfortunately I am not able to find exactly the solution to my issue.
I am training to validate the response of the following JSON:
{
"A":[
{
"B":[
{
"C":"c",
"D":"d"
}
],
"E":[
{
"F":[
{
"G":"g1"
}
]
},
{
"F":[
{
"G":"g2"
}
],
},
{
"F":[]
}
]
}
],
"H": "h"
}
with the following code:
response.body("A.E.F.G", hasItems(expectedValues.get(G).toArray(new String[0])))
expectedValues.get(G) return a list of g1 and g2.
the ERROR that I get is:
JSON path A.E.F.G doesn't match.
Expected: an array containing ["g1", "g2"]
Actual: [[[g1], [g2], []]]
How can I get that done?

That solve the problem:
response.body("A.E.F.G.flatten()", hasItems(expectedValues.get(G).toArray(new String[0])))

Related

convert a JSON to another w/ circe

I would like to convert this JSON
{
"l1k1": {
"l2k1": "l2v1",
"l2k2": 1
},
"l1k2": [
{
"e1l1": "e1v1",
"e1l2": "e1v2"
},
{
"e2l1": "e2v1",
"e2l2": "e2v2"
}
]
}
to this one
{
"papa": {
"l1k1c": {
"l2k1c": {
"string": "l2v1"
},
"l2k2c": {
"int": 1
}
},
"l1k2c": {
"array": [
{
"e1l1": "e1v1",
"e1l2": "e1v2"
},
{
"e2l1": "e2v1",
"e2l2": "e2v2"
}
]
}
}
}
where:
"l" stands for level
"k" for key, "v" for value
"e" for element
"c" for copy (where "*" maps to "*c")
I'm using circe's Json but having a hard time renaming the keys or creating parents or children with it. As I'm writing this, I'm thinking I may need to use its ACursor instead. As you may have guessed, I'm trying to generate an AVRO doc from an input JSON. I'm open to help w/ my approach or any suggestions about how to go about it in a cleaner way.

JSON liquid mapping using payload

I want to access the the below JSON payload :
[
{
"ApplicantContactDetail": [
{
"ApplicantBaseDetails": {
"CATEGORY": "P",
"ULN": "1265847478"
} }
]
}
]
and need output as
{
"Number":2,
"Data":
{
"ResidencyCode":"CATEGORY"
}
}
i need to map CATEGORY in the output from liquid template, may i know the correct syntax for this to achieve.

What is wrong with the following JSON data

JSON -
[
{
"pagename":"1",
"url":"sadadsad",
"group":"ad"
},
{
"pagename":"2",
"url":"sasdsad",
"group":"eng"
},
{
"pagename":"3",
"url":"thfgv",
"group":"pd"
},
{
"pagename":"4",
"url":"tuyi",
"group":"pd"
},
{
"pagename":"5",
"url":"popop",
"group":"pd"
}
]
Error: Parse error on line 2:
... { pagename:"1",
---------------------^
Expecting 'STRING', '}'
Edit: With this rectified JSON data when I am printing the length, it's coming out to be 486, looks like some stray characters are getting added, how can I resolve that.
You need to put pagename and all other var names in quotes also
[
{
"pagename":"1",
"url":"sadadsad",
"group":"ad"
},
If you want to know why the quotes are needed, have a look at this answer: in JSON, Why is each name quoted?
The correct format is as follows
[
{
"pagename":"1",
"url":"sadadsad",
"group":"ad"
},
{
"pagename":"2",
"url":"sasdsad",
"group":"eng"
},
{
"pagename":"3",
"url":"thfgv",
"group":"pd"
},
{
"pagename":"4",
"url":"tuyi",
"group":"pd"
},
{
"pagename":"5",
"url":"popop",
"group":"pd"
}
]
Be sure to verify your JSON here. The errors are highlighted in black.

Jsonpath for a JSON payload

I have the following JSON payload. I would like to extract the value "value_for_key_attribute_Y" using JSON path expression in a generic manner (i.e. without hard coding any array values like [1]) Any ideas ?
{
"requests":[
{
"event":[
{
"parameter":"parameter_key_A",
"event":"event_key_A",
}
],
"data":[
{
"id":"id_xyz",
"payload_data":[
{
"key":"key_attribute_X",
"value":"value_for_key_attribute_X",
},
{
"key":"key_attribute_Y",
"value":"value_for_key_attribute_Y",
}
]
}
]
}
]
}
Something like this should work for you.
$.requests[*].data[*].payload_data[?(#.key_path == 'key_attribute_Y')].value

how to declare two arrays in json?

I want to create a json string for my web application. Actually i am new to this json format.In my json string i have to create two arrays in my json structure.But i have some syntax problem in creating two arrays. my json string is given below for your reference.
{
MarkUpdate:[
{
'FinalMarks':[
{
'studentId':'S1',
'Ques_Mark':[
{
'qId' :'Q1',
'mark':'14',
},
{
'qId':'Q2',
'mark':'10',
}
]
},
{
'studentId':'S2',
'Ques_Mark':[
{
'qId' :'Q1',
'mark':'12',
},
{
'qId':'Q2',
'mark':'13',
}
]
}
]
}
]
}
In my above json string format,my MarkUpdate contains one array object named as FinalMarks.So,here i have to create one more array object named as EvalMarks under MarkUpdate.
Acually my EvalMarks contains following elements...
'EvalMarks':[
{
'EvalId':'E1',
'Ques_Mark':[
{
'qId' :'Q1',
'studId':'S1',
'mark':'13',
},
{
'qId':'Q2',
'studId':'S1',
'mark':'13',
}
]
},
{
'EvalId':'E2',
'Ques_Mark':[
{
'qId' :'Q1',
'studId':'S2',
'mark':'10',
},
{
'qId':'Q2',
'studId':'S2',
'mark':'10',
}
]
}
]
So, i have declare this EvalMarks under the MarkUpdate.I missed the syntax...
Could you plz tell me how to add this array object under the MarkUpdate.
guide me to get out of this issue...
To declare two arrays in one JSON object, remember that the JSON object can only be a single object, therefore the array must be inside the enclosing curly braces. For example:
{
"array1":[1,2,3],
"array2":["jim","louise","mark"]
}
For your case, it's important to remember that you should properly indent your braces, square and curly, so that you can visually identify mistakes before they become problems. I stringly recommend http://jslint.com/ to validate your JSON before using it. It's also great for Javascript:
{
"MarkUpdate":[
{
"FinalMarks":[
{
"studentId":"S1",
"Ques_Mark":[
{
"qId" :"Q1",
"mark":"14"
},
{
"qId":"Q2",
"mark":"10"
}
]
},
{
"studentId":"S2",
"Ques_Mark":[
{
"qId" :"Q1",
"mark":"12"
},
{
"qId":"Q2",
"mark":"13"
}
]
}
]
}
],
"EvalMarks":[
{
"EvalId":"E1",
"Ques_Mark":[
{
"qId" :"Q1",
"studId":"S1",
"mark":"13"
},
{
"qId":"Q2",
"studId":"S1",
"mark":"13"
}
]
},
{
"EvalId":"E2",
"Ques_Mark":[
{
"qId" :"Q1",
"studId":"S2",
"mark":"10"
},
{
"qId":"Q2",
"studId":"S2",
"mark":"10"
}
]
}
]
}
Its not a valid JSON if you have commas after the last key-value pair in an object. I would start by knocking off all those unnecessary commas after the last key-value pairs inside most of your objects and validating the JSON in www.jslint.com
To be more clear ,
{
"qId":"Q2",
"studId":"S2",
"mark":"10",
}
is Not Valid.
On the other hand,
{
"qId":"Q2",
"studId":"S2",
"mark":"10"
}
Is valid.