How can I add value assertions for my JSON payload that has root element
I've simplified my actual JSON response from my traffic viewer to make this example easier. Let's say I want to add assertions to validate that the first number returned is 111 and the second number is 222 (they are strings). I added a JSON Assertor but the only element that shows up is "root", so I tried manually editing the xpath to something like these, but when I click Evaluate XPath, I get the same response "No nodes found". What am I doing wrong or is there a better way? Also tried JSONPath format but didn't seem to recognize that either.
Tried:
- /root/A/A-1/return[1]/number/string()
- /root/A/A-1/return[1]/number[1]/string()
Here is my JSON response example:
root, {
"A": {
"A-1": {
"return": [
{
"number": "111",
"year": "2001",
},
{
"number": "222",
"year": "2003",
}
]
}
},
"B": "No_Data: No data returned"
}
Answers
-
Try
- /root/A/A-1/return/item[1]/number/string()
- /root/A/A-1/return/item[2]/number/string()
0 -
The payload as you typed it looks malformed. I cannot see the tree in the JSON Assertor either. But when I fix the format like the following I see all nodes:
{ "A" : { "A-1" : { "return" : [ { "number" : "111", "year" : "2001" }, { "number" : "222", "year" : "2003" } ] } }, "B" : "No_Data: No data returned" }
0 -
@Matt Love - thanks, but that didn't work either:
@jakubiak - that's the format that came in automatically when I ran the test. I can't post the actual json results as it contains PII but here is a screenshot of that traffic viewer just to show you how the only element that shows up is "root". I also included screenshots of the JSON assertor. Is there a way to edit the JSON response for the JSON assertor to remove the "root" part? I tried in the Expected JSON tab but it wouldn't take (after saving it just reverted when I ran the test).
0 -
@kathy_carino
Both XPaths provided by @Matt Love works on my side with JSON message example corrected by @jakubiak .I'm not sure if response is pure JSON, as you screenshot from Traffic Viewer with Tree tab view, you have just root element.
On my side it looks completely different:What content-type do you see in header of the response?
Maybe you have XML message with embedded JSON or maybe you have converted message?Show us whole response, but just change strings into something more general.
Use Write File tool to save response message as file and then edit and validate it before posting here.
As @jakubiak wrote, your example can not pass JSON validation.--
Ireneusz Szmigiel
http://www.catb.org/esr/faqs/smart-questions.html0 -
Here is the header on my response:
HTTP/1.1 200 OK
Date: Fri, 18 Mar 2022 18:30:00 GMT
Content-Type: application/json; charset=utf-8
Server: Kestrel
Transfer-Encoding: chunkedI added a Write File to capture the response traffic and only edited it to remove any PII - see attached. No idea what format this is I can take the well formatted JSON results that appear in the "Tree" tab of the Traffic Viewer response and put it in the JSON Assertor on the Expected JSON tab and it looks fine and gives me the tree structure to select nodes. However, even though I have the checkbox checked "Save Expected JSON", as soon as I run the test again, it overwrites what I pasted in with the results you see in the attachment (and obviously the test fails). Is there some kind of setting I need to find that's making the JSON response come in weird? If I look at the response tab in the traffic viewer it looks like this in the Literal tab (which I then copied and pasted into expected JSON):
But it looks like this in the "Tree" tab, so the format looks good in the right side window, but there's no detection of the nodes, it's just all in "root":
And here's a screenshot of when I try to save the right format in the expected JSON tab, it looks great and I can add my assertions (but they won't stick):
0 -
I think I have it working now! I noticed in the header it was set to Accept "application/json" so I tried just changing it to "text/plain" because that's what I saw in the swagger and seems to work fine now. But do you know why when I generated the automated tests from the swagger the header was set to application/json? I tried a few times just to make sure it wasn't a fluke, but it's always being set that way. Is there an easy way to globally change all my tests (there are a ton!)
0 -
The traffic viewer shows your service returning "application/json" and the response does indeed look like valid JSON. However, the response is a JSON string that has a JSON object encoded inside of it. So, the Tree view is correct in showing this as well.
Perhaps this is a defect in your service? Is your service really supposed to return a JSON string containing an encoded JSON object or should it be returning the JSON object directly?
0