Unable to parse json string using JsonPath in rest assured - json

I'm trying to parse a given JSON string and abstract some fields from it.
my Test method looks like this
Steps followed
Making a rest api call and getting response using restassured
response is stored in Response object from restassured
From response ResponseBody is being extracted using getBody() method in ResponseBody object
ResponseBody object is being converted to string using asString()
JsonPath object is being created from string value
Json is parsed by providing Jway Expression
#Test
Response searchResult = this.getEndPoint().SearchRecallAgreement(payload);
ResponseBody b = searchResult.getBody();
String responseBody = b.asString();
JsonPath j = new JsonPath(responseBody);
List<String> ls = j.getList("payload.returnTerms[*].returnTermPolicies[?(#.policyType == 'RC_ITEMS_POLICY')].policies[?(#.policyKey=='/575330172')].policyKey");
System.out.println(ls);
Error in console
Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 1: Unexpected input: '['; Expecting <EOF> # line 1, column 46.
RootObject.payload.returnTerms[*].return
Json string looks like
{
"status": "OK",
"header": {
"headerAttributes": {}
},
"errors": [],
"payload": {
"totalCount": 6,
"returnTerms": [
{
"returnTermType": "RECALL_AGREEMENT",
"returnTermPolicies": [
{
"policyType": "RC_ITEMS_POLICY",
"keyTemplate": "/itemNo/storeNo",
"policies": [
{
"policyKey": "/583919815",
"attributeList": [
]
},
{
"policyKey": "/575330172",
"attributeList": [
]
},
{
"policyKey": "/583919815/2345"
}
]
}
]
},
{
"returnTermPolicies": [
{
"policyType": "RC_ITEMS_POLICY",
"keyTemplate": "/itemNo/storeNo",
"policies": [
{
"policyKey": "/583919815",
"attributeList": [
]
},
{
"policyKey": "/575330172",
"attributeList": [
]
},
{
"policyKey": "/583919815/2346"
},
{
"policyKey": "/583919815/2345"
}
]
}
]
}
]
}
}
Expected output :
[
"/575330172",
"/575330172"
]

Related

Looping through groovy object(List) using each{it} and pass the the elements into a json payload in Jenkins

I have a list containing the name of workspaces in groovy Jenkinsfile. I wrote an each() loop to iterate through the list and use the names in the endpoint below to get the workspace ID from the api response.
def getWorkspaceId() {
def result = []
Listworkspace.each{
def response = httpRequest(
customHeaders: [
[ name: "Authorization", value: "Bearer " + env.BEARER_TOKEN ],
[ name: "Content-Type", value: "application/vnd.api+json" ]
],
url: "https://app.terraform.io/api/v2/organizations/${TF_ORGNAME}/workspaces/$it
)
def data = new JsonSlurper().parseText(response.content)
println ("Workspace Id: " + data.data.id)
result << data.data.id
}
return result
}
After getting the IDs, I want to pass them as part of a json payload.
def buildPayload() {
def workspaceID = new JsonSlurper().parseText(getWorkspaceId())
workspaceID.each{
def payload = """
{
"data": {
"attributes": {
"is-destroy":false,
"message":
},
"type":"runs",
"relationships": {
"workspace": {
"data": [
{"id": "$it", "type": "workspaces"}
]
}
}
}
}
}
"""
return payload
}
Is there a way I can iterate through the list of IDs returned and append each json object for the key "data" after iteration. See the code below
"relationships": {
"workspace": {
"data": [
{"id": "id1", "type": "workspaces"},
{"id": "id2", "type": "workspaces"},
{"id": "id3", "type": "workspaces"}
]
When making the api call, it throws a 400 Bad request error. I tried to print the payload and I found out it attaches the whole list of IDs to the payload.
Any suggestion will be greatly appreciated. Thank you.
def buildPayload() {
def workspaceID = new JsonSlurper().parseText(getWorkspaceId())
workspaceID.each{
def payload = """
{
"data": {
"attributes": {
"is-destroy":false,
"message":
},
"type":"runs",
"relationships": {
"workspace": {
"data": [
[id1, id2, id3]
]
}
}
}
}
}
"""
return payload
}
I'd recommend using the JsonOutput class to make your life easier. In essence, as long as your getWorkspaceId() method is returning a list of ids, you can do something like this:
import groovy.json.JsonOutput
def buildPayload(def ids) {
def payload = [
data: [
attributes: [
"is-destroy": false,
"message" : "",
],
type: "runs",
relationships: [
workspace: [
data: ids.collect {
return [id: it, type: "workspaces"]
}
]
]
]
]
return JsonOutput.toJson(payload)
}
This will take each id in your ids list and build a map where each id is identified by its number and the type: workspaces key pair. This is all then included in the larger payload.

Parsing Iterative JSON from Firebase to Flutter

I have data saved into Realtime Firebase as an iterative JSON as shown in the picture.
Realtime Firebase data
[
{
"name": "Math",
"subMenu": [
{
"name": "Math1",
"subMenu": [
{
"name": "Math 1.1"
},
{
"name": "Math 1.2",
"subMenu": [
{
"name": "Math 1.2.1",
"subMenu": [
{
"name": "Math 1.2.1.1"
},
{
"name": "Math 1.2.1.2"
}
]
},
{
"name": "Math 1.2.2"
}
]
}
]
},
{
"name": "Math2"
},
{
"name": "Math3",
"subMenu": [
{
"name": "Math 1.3.1"
},
{
"name": "Math 1.3.2"
}
]
}
]
},
{
"name": "Marketing",
"subMenu": [
{
"name": "Promotions",
"subMenu": [
{
"name": "Catalog Price Rule"
},
{
"name": "Cart Price Rules"
}
]
},
{
"name": "Communications",
"subMenu": [
{
"name": "Newsletter Subscribers"
}
]
}
]
}
]
How the JSON look like in Realtime Firebase
'Click the image'
datamodel.dart
class Menu {
String? name;
int? font;
List<Menu>? subMenu = [];
Menu({this.name, this.subMenu, this.font});
Menu.fromJson(Map<String, dynamic> json) {
font = json['font'];
name = json['name'];
if (json['subMenu'] != null) {
json['subMenu'].forEach((v) {
subMenu?.add(Menu.fromJson(v));
});
}
}
}
My goal is to build a multilevel list view in Flutter that reflexes iterative JSON structure. So, I implemented a method that returns List<Menu>, and then pass it to a Futurebuilder to build a multilevel list View.
The method.
final ref = FirebaseDatabase.instance.ref();
Future<List<Menu>> firebaseCalls(DatabaseReference ref) async {
final snapshot = await ref.child('Task').get();
final jsondata = snapshot.value as Map<String, dynamic>;
final list = json.decode(jsondata) as List<dynamic>; // Error Location
return list.map((e) => Menu.fromJson(e)).toList();
}
and I got the following
The Error
error: The argument type 'Map<String, dynamic>' can't be assigned to the parameter type 'String'. (argument_type_not_assignable at [flutter_multilevel_list_from_json] lib\main.dart:28)
tried to change the list type to List<dynamic> but still give me an error.
json.decode() takes a String as input, and you are passing a Map<String,dynamic> into it.
That is your problem, not that you are trying to cast it to a List<dynamic>
May be this will be helpful (jsondata as List).map((e) => Menu.fromJson(e)).toList();

Groovy: How to parse the json specific key's value into list/array

I am new to groovy and trying
1) from the output of prettyPrint(toJson()), I am trying to get a list of values from a specific key inside an json array using groovy. Using the below JSON output from prettyPrint example below, I am trying to create a list which consists only the values of the name key.
My Code:
def string1 = jiraGetIssueTransitions(idOrKey: jira_id)
echo prettyPrint(toJson(string1.data))
def pretty = prettyPrint(toJson(string1.data))
def valid_strings = readJSON text: "${pretty}"
echo "valid_strings.name : ${valid_strings.name}"
Output of prettyPrint(toJson(string1.data))is below JSON:
{
"expand": "places",
"places": [
{
"id": 1,
"name": "Bulbasaur",
"type": {
"grass",
"poison"
}
},
{
"id": 2,
"name": "Ivysaur",
"type": {
"grass",
"poison"
}
}
}
Expected result
valid_strings.name : ["Bulbasaur", "Ivysaur"]
Current output
valid_strings.name : null
The pretty printed JSON content is invalid.
If the JSON is valid, then names can be accessed as follows:
import groovy.json.JsonSlurper
def text = """
{
"expand": "places",
"places": [{
"id": 1,
"name": "Bulbasaur",
"type": [
"grass",
"poison"
]
},
{
"id": 2,
"name": "Ivysaur",
"type": [
"grass",
"poison"
]
}
]
}
"""
def json = new JsonSlurper().parseText(text)
println(json.places*.name)
Basically, use spray the attribute lookup (i.e., *.name) on the appropriate object (i.e., json.places).
I've used something similar to print out elements within the response in ReadyAPI
import groovy.json.*
import groovy.util.*
def json='[
{ "message" : "Success",
"bookings" : [
{ "bookingId" : 60002172,
"bookingDate" : "1900-01-01T00:00:00" },
{ "bookingId" : 59935582,
"bookingDate" : "1900-01-01" },
{ "bookingId" : 53184048,
"bookingDate" : "2019-01-15",
"testId" : "12803798123",
"overallScore" : "PASS" },
{ "bookingId" : 53183765,
"bookingDate" : "2019-01-15T13:45:00" },
{ "bookingId" : 52783312,
"bookingDate" : "1900-01-01" }
]
}
]
def response = context.expand( json )
def parsedjson = new groovy.json.JsonSlurper().parseText(response)
log.info parsedjson
log.info " Count of records returned: " + parsedjson.size()
log.info " List of bookingIDs in this response: " + parsedjson.bookings*.bookingId

how to parse the json file and read the json elements in groovy

Hi i am trying to parse the below json file. I tried using jsonsluper and parsed the file. I executed below command. Nothing works.
def test =newjsonslurper().parsetext(organist)
test.resources.each{
println it.resources.metadata. "guid"
println it.resources.entity. "name"
}
This is the json file format
resources: [
{
"metadata" :{
"guid":"cya"
"url": "dummy.test"
},
"entity" :
{
"name": "system"
"status": "active"
}
}
{
"metadata" :
{
"guid":"cya"
"url": "dummy.test"
},
"entity" :
{
"name": "system"
"status": "active"
}
}
]
There were a couple of problems:
JsonSlurper().parseText() expects a String. If you're wanting to parse a file, use something like def response = new JsonSlurper().parse(new File('JsonFile.json'))
The JSON payload is not valid: it's missing a few brackets and commas.
The following code should work:
import groovy.json.JsonSlurper
def test = new JsonSlurper().parseText '''
{"resources": [
{
"metadata": {
"guid": "cya",
"url": "dummy.test"
},
"entity": {
"name": "system",
"status": "active"
}
},
{
"metadata": {
"guid": "cya",
"url": "dummy.test"
},
"entity": {
"name": "system",
"status": "active"
}
}
]}
'''
test.resources.each {
println it.metadata.guid
println it.entity.name
}

Iterate through two loops when building the JSON String

I am invoking a restful service to get the available documents on the the server where I am getting the JSON as s response. I am building the JSON String with the JSONBuilder so when invoking the this link
http://localhost:8080/httpConnector/Rest/Documents?Accept=application/json
I am getting the JSON String below:
{
"results": [
{
"result": {
"name": "Test traisy",
"version": "sdvdsv",
"author": "sdvdsv"
}
},
{
"result": {
"name": "Jaspersoft Ultimate guide",
"version": "sdfdsv",
"author": "sdvdsv"
}
},
{
"result": {
"name": "Dohrn",
"version": "12.19.00",
"author": "sdfdsf"
}
}
]
}
Code
String accept = getValue("Accept");
accept = "application/xml";
if ("application/xml".equals(accept)){
builder=new groovy.xml.MarkupBuilder(writer);
}else{
builder=new groovy.json.JsonBuilder();
}
builder{
results foaList.collect{
[
//Here I want to loop through the otaList to do something like that "ota.getName(), foa.getFlexiObject().getByString(ota.getName())"
result: [
name: it.getFlexiObject().getByString("name"),
version: it.getFlexiObject().getByString("version"),
author: it.getFlexiObject().getByString("author")
]
]
}
}
Now I want to add the properties programatically. Therefore I have to loop through the otaList to do something like that
builder.'results'() {
for(FlexiObjectAttachment foa: foaList){
for(ObjectTypeAttribute ota : otaList){
param.put(ota.getName(), foa.getFlexiObject().getByString(ota.getName()));
}
result(param);
}
}
this version just works for the xml respose.
What you can try is to do the combination of foa and ota directly in your collect call.
That way your initially created dict would have the correct structure.
Something like the example below
def foaList = [1, 2, 3, 4]
def otaList = ['A', 'B', 'C']
foaList.collect { foa ->
result = [name: "Name$foa", version: "v$foa", author: "Author$foa"]
otaList.each { ota -> result[ota] = "$ota$foa" }
[ result: result ]
}