Struggling to parse response from API service - json

Struggling to parse what appears to be a simple response from an API service (GraphQL / JSON). The response is retrieved as String based on HTTP request, and have tried different approaches (using both JsonObject/JsonParse and JsonArrayfrom com.google.gsonlibrary to get desired string elements to String, although without being able to extract anything.
Anyone willing to share suggestions how to parse and get "total" and "startsAt" from the response shown below?
{
"data": {
"viewer": {
"homes": [
{
"currentSubscription": {
"priceInfo": {
"current": {
"total": 0.5626,
"energy": 0.4336,
"tax": 0.129,
"startsAt": "2019-11-14T20:00:00+01:00"
}
}
}
}
]
}
}
}

You can get the json value like this
Assign the response to some variable like res.
For ex :
var res = {
"data": {
"viewer": {
"homes": [
{
"currentSubscription": {
"priceInfo": {
"current": {
"total": 0.5626,
"energy": 0.4336,
"tax": 0.129,
"startsAt": "2019-11-14T20:00:00+01:00"
}
}
}
}
]
}
}
var total=res.data.viewer.homes[0].currentSubscription.priceInfo.current.total;
var startAt = res.data.viewer.homes[0].currentSubscription.priceInfo.current.startsAt;
console.log('total is: ',total);
console.log('startsAt: ', startsAt)

Related

JMeter - How to convert string value to a list

I have an API that returns the following json:
{
"GetAppendixListResponse": {
"GetAppendixListResult": {
"FileList": {
"string": ["3602","3587"]
},
"Code": "0"
}
}
}
The debug sampler looks like:
JMeterVariables:
AppendixCode_1=["3602","3587"]
AppendixCode_ALL=["3602","3587"]
AppendixCode_matchNr=1
How can I convert ["3602","3587"] from string to a list (for loop_controller)?

Validate nested response body rest assured

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])))

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.

Nodejs how to use map of object?

Hi i have a json like this called body:
{
data: {
id:1,
name:hallo
},
paging: {
cursors: {
next: "abc"
}
}
}
so I have to save it into a map and then I'll need to print or to modify it.
If I save it with this code
var Map = require("collections/map");
var new_body = JSON.parse(body)
var map = new Map(new_body);
I have some difficulties to print for example map.paging.cursor.next, how can I do it? maybe I had to define a structure of that map before inserting the json inside it, I don't know...
Your json have errors. Correct them like this.
{
"data": {
"id": 1,
"name": "hallo"
},
"paging": {
"cursors": {
"next": "abc"
}
}
}
Then you can parse the json like this and get the paging.cursors.next value.
var map = JSON.parse(body);//This will parse the json string
Now map.paging.cursors.next will have the value "abc"

jsonpath- Extracting json data using jsonpath

My json data is as follows:
"query": {
"count": 1,
"url": [
{
"execution-start-time": "1"
},
{
"execution-time": "745"
}
],
"service-time": "1481"
},
results": {
"div": {
"class": "makers",
"ul": {
"li": [
{
"a": {
"href": "nokia_x2_dual_sim-6383.php",
"img": {
"src": "Nokia-X2-Dual-SIM.jpg"
},
"strong": {
"br": null,
"content": "Nokia\nX2 Dual SIM"
}
}
},
{
"a": {
"href": "nokia_xl-6148.php",
"img": {
"src": "nokia-xl.jpg",
},
"strong": {
"br": null,
"content": "Nokia\nXL"
}
}
}
]
Now I want to extract the "content" part from this json data using jsonpath.I am using jsonpath-0.8.0.js for this purpose.
I have tried to parse the json data in the following way:
function ParseData(data) {
var result = jsonPath(data, "$.query.results[*].ul.li[*].strong.content");
$('#carousel').empty();
var html = "";
for (i = 0; i < result.length; i++) {
// do something
}
and I was expecting to get Nokia\nX2 Dual SIM and Nokia\nXL as output but this code of mine does not return anything.I cannot understand the problem.
How do I extract the content data from this json? please help.
Friends, I finally got the answer to my problem.I used the following code to extract content from the above given json data.
var result = jsonPath(data, "$.query.results[*].ul.li[*].a.strong.content");
Now I am getting the output data as expected.