Get a JSON node's child whose attribute equals a specific value?
The service I'm testing returns JSON. Before the process I'm currently testing, the JSON looks like this (greatly simplified, and I'm using UID-A, UID-B to represent the actual ids, which are long unique combinations of letters and numbers):
{
"data": {
"id": "UID-A",
"content": {
"UID-B": {
"id": "UID-B",
"contentType": "BASE",
}
}
}
}
I already know what UID-A is, and I can capture UID-B using XPath "/root/data/content//id/text()", because there is only one child under "content".
After processing, the JSON looks like:
{
"data": {
"id": "UID-A",
"content": {
"UID-B": {
"id": "UID-B",
"contentType": "BASE",
},
"UID-C": {
"id": "UID-C",
"contentType": "EXTENDED",
}
}
}
}
I need to:
- verify "content" now has two children
- verify the new child's "contentType" is "EXTENDED" (or, to put it another way, verify "content" has a child whose "contentType" is "EXTENDED")
Help?
Best Answer
-
You can use the JSON Assertor. For these you can create a "Value" assertion where you can click "Change Element" then select "Edit XPath manually".
verify "content" now has two children
Use XPath
count(/root/data/content/*)with expected value of2.verify "content" has a child whose "contentType" is "EXTENDED"
Use XPath
count(/root/data/content[*/contentType='EXTENDED'])with expected value of1.1
Answers
-
Awesome, thank you, that worked perfectly!
0