Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Get a JSON node's child whose attribute equals a specific value?

charlesharold
charlesharold Posts: 2

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?
Tagged:

Best Answer

  • benken_parasoft
    benken_parasoft Posts: 1,388 ✭✭✭
    edited April 8 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 of 2.

    verify "content" has a child whose "contentType" is "EXTENDED"

    Use XPath count(/root/data/content[*/contentType='EXTENDED']) with expected value of 1.

Answers

Tagged