Answered

API - JSONPath Question

I want to get all the Name values from below JSON response.

JSON response:

{
"TaxRates": null,
"ItemTypes": [
{
"Id": 1,
"Name": "Product"
},
{
"Id": 2,
"Name": "Access"
},
{
"Id": 3,
"Name": "Service"
},
{
"Id": 4,
"Name": "Fee"
},
{
"Id": 5,
"Name": "Bundle"
},
{
"Id": 6,
"Name": "Rental"
},
{
"Id": 7,
"Name": "GiftCard"
}
],
"DiscountCodes": null,
"ItemCategories": null,
"RefundReasonCodes": null
}

 

If I use JSONPath as ItemTypes[*].Name in Subject7 it returns me only 1st Item's Name:Product

Expecting:["Product","Access","Service","Fee","Bundle","Rental","GiftCard"]

I tried to use a external website to test my JSON Path expression and found that above JSONPath gives what i expected on external website but in Subject7 it returns only 1st Item's Name:Product

Please guide me with a solution to get the expected return values.

0
1 comment
Avatar
Jonathan Hotz

Hi.

GET_REST_VALUE command always gets the first found value that fits pattern to associate it with related variable. That's why your pattern finds all 7 objects, but returns only first one.

Questions:

1) is number of ItemType objects fixed or not?

2) Could you describe values usage please?

 

If you want to get all 7 values, but number of items is fixed, you should do next:

1) provide all 7 values names separated by commas, like val1,val2,val3, etc.

2) add all 7 paths like ItemTypes[1].Name, ItemTypes[2].Name, etc.

The problem is that it works only for fixed number of nodes. Also it's hard to operate with values like val1,val2,etc. if order can be changed.

 

That's why I had this 2nd question about usage.

I think this scenario will help you:

1) use Get_Rest_Value with BODY_JSON_COUNT type to get number of ItemTypes (ItemTypes.Name).

2) write LOOP - LOOP_END block from 0 to number of nodes -1

3) use Get_Rest_Value inside LOOP with same REST connection, but with search pattern like ItemTypes[@number].Name

4) do whatever you want with read value

 

Here is code example:

{
"steps" : [
{
"index" : 1,
"key" : "GET_REST_VALUE",
"model" : {
"statusVariableName" : "status",
"validationRequired" : false,
"variableName" : "number",
"fields" : [
{
"key" : "ItemTypes.Name",
"type" : "BODY_JSON_COUNT"
}
],
"excludeCharacters" : [ ],
"rest_connection" : "a",
"validation_file" : "<NOT SELECTED>"
},
"skip" : false,
"runtimeOption" : "HALT",
"customCaption" : ""
},
{
"index" : 2,
"key" : "SET_VAR",
"model" : {
"variableName" : "stack",
"sourceType" : "STATIC",
"value" : {
"encrypted" : false,
"raw" : "@System.empty"
},
"alphabet" : null,
"regex" : null,
"expressionLength" : 0
},
"skip" : false,
"runtimeOption" : "HALT",
"customCaption" : ""
},
{
"index" : 3,
"key" : "LOOP",
"model" : {
"variableName" : "index",
"from" : "0",
"to" : "@number-1",
"increment" : "1"
},
"skip" : false,
"runtimeOption" : "HALT",
"customCaption" : ""
},
{
"index" : 4,
"key" : "GET_REST_VALUE",
"model" : {
"statusVariableName" : "status",
"validationRequired" : false,
"variableName" : "name_var",
"fields" : [
{
"key" : "ItemTypes[@index].Name",
"type" : "BODY_JSON"
}
],
"excludeCharacters" : [ ],
"rest_connection" : "a",
"validation_file" : "<NOT SELECTED>"
},
"skip" : false,
"runtimeOption" : "HALT",
"customCaption" : ""
},
{
"index" : 5,
"key" : "PRINT",
"model" : {
"message" : "@name_var"
},
"skip" : false,
"runtimeOption" : "HALT",
"customCaption" : ""
},
{
"index" : 6,
"key" : "SET_VAR",
"model" : {
"variableName" : "stack",
"sourceType" : "STATIC",
"value" : {
"encrypted" : false,
"raw" : "@stack,@name_var"
},
"alphabet" : null,
"regex" : null,
"expressionLength" : 0
},
"skip" : false,
"runtimeOption" : "HALT",
"customCaption" : ""
},
{
"index" : 7,
"key" : "LOOP_END",
"model" : { },
"skip" : false,
"runtimeOption" : "HALT",
"customCaption" : ""
},
{
"index" : 8,
"key" : "PRINT",
"model" : {
"message" : "@stack"
},
"skip" : false,
"runtimeOption" : "HALT",
"customCaption" : ""
}
]
}

 

Replace connection with the one you use and put it to advanced view.

0
Comment actions Permalink

Please sign in to leave a comment.