Iam new to jolt.
Can you please tell me how can i trasform the below json message with array based upon the position in to the below output json message using jolt.
input message :
[
["20084541", "12020584", "Frohmann Dov", "2017", "2", "75", "T7", "DFZ", "CES", "", "", "0", "90", "2010"],
["20084541", "12020584", "Frohmann Dov", "2017", "3", "21", "T7", "DFZ", "CES", "", "", "0", "90", "2010"],
]
output Message :
[{
"policyReference": "20084541",
"insuredId": "12020584",
"insuredName": "Frohmann Dov",
"uwy": "2017",
"subLOB": "2",
"typeOfRisk": "75",
"aircraftcountryCode": "T7",
"aircraftId": "DFZ",
"manufacturerId": "CES",
"aircraftTypeCode": "",
"aircraftSubTypeCode": "",
"aircraftValueAmt": "0",
"aircraftWorkNo": "90",
"yearBuilt": "2010"
}, {
"policyReference": "20084541",
"insuredId": "12020584",
"insuredName": "Frohmann Dov",
"uwy": "2017",
"subLOB": "2",
"typeOfRisk": "75",
"aircraftcountryCode": "T7",
"aircraftId": "DFZ",
"manufacturerId": "CES",
"aircraftTypeCode": "",
"aircraftSubTypeCode": "",
"aircraftValueAmt": "0",
"aircraftWorkNo": "90",
"yearBuilt": "2010"
}]
Any help appreciated.
Spec
[
{
"operation": "shift",
"spec": {
"*": { // loop thru the outer array
// each item of the outer array, is an array
// match individual array indicies, and then send them to the
// output with the nice name.
"0": "[&1].policyReference",
"1": "[&1].insuredId",
"2": "[&1].insuredName"
// etc
}
}
}
]
Related
I'm struggling to trans my input response to a specific output using jolt, below is my example
The input that describe my message
{
"firstAttribute": true,
"secondAttribute": "12",
"data": {
"propertyKey1": "1",
"propertyKey2": [
"a"
],
"propertyKey3": "2",
"propertyKey4": "3",
"propertyKey_test": [
"option1",
"option2",
"option3"
],
"propertyKey5": "4",
"propertyKey6": "87.0"
},
"Keytest1": "value1",
"KeyTest2": "value2"
}
The used jolt Spec
[
{
"operation": "shift",
"spec": {
"data": {
"propertyKey2": {
"0": "propertyKey2"
},
"*": {
"#": "data.&"
}
},
"*": {
"#": "&"
}
}
}
]
My actual output after a jolt transfomration
{
"firstAttribute" : true,
"secondAttribute" : "12",
"data" : {
"propertyKey1" : "1",
"propertyKey3" : "2",
"propertyKey4" : "3",
"propertyKey_test" : [ "option1", "option2", "option3" ],
"propertyKey5" : "4",
"propertyKey6" : "87.0"
},
"propertyKey2" : "a",
"Keytest1" : "value1",
"KeyTest2" : "value2"
}
The desired output is to convert every array fields to separated fields such as propertyKey_test as below
{
"firstAttribute" : true,
"secondAttribute" : "12",
"data" : {
"propertyKey1" : "1",
"propertyKey3" : "2",
"propertyKey4" : "3",
"propertyKey_test": "option1",
"propertyKey5" : "4",
"propertyKey6" : "87.0"
},
"propertyKey2" : "a",
"Keytest1" : "value1",
"KeyTest2" : "value2"
}
Any help would be appreciated. Thanks in advance
PS: we can receive a dynamic array fields (ex: today we have propertyKey_test:["option1","option2","option3"] tomorrow we could receive another array field for example new_field:["a","b","c"].
My goal is to check everytime if the field is an array and take just the first element as described above
The current desired output is not a valid JSON value, since there may not exist more than one attribute with identical key within a common object. But, if you need to differentiate the components of the "propertyKey_test" array, then using the indexes of the array as below might be preferred such as
[
{
"operation": "shift",
"spec": {
"data": {
"propertyKey2": {
"0": "&1"
},
"*": {
"#": "&2.&"
},
"propertyKey_*": {
"*": {
"#": "&3.&2&1"
}
}
},
"*": {
"#": "&"
}
}
}
]
in order to get
{
"firstAttribute" : true,
"secondAttribute" : "12",
"data" : {
"propertyKey1" : "1",
"propertyKey3" : "2",
"propertyKey4" : "3",
"propertyKey_test0" : "option1",
"propertyKey_test1" : "option2",
"propertyKey_test2" : "option3",
"propertyKey5" : "4",
"propertyKey6" : "87.0"
},
"propertyKey2" : "a",
"Keytest1" : "value1",
"KeyTest2" : "value2"
}
as output.
How to get a new list from the list of objects?
I need a new list of objects to POST request
this list of objects i get from response:
{
"success": true,
"body": {
"users": [
{
"type": "unknown",
"data": [
{
"id": "8",
"firstName": "Jackson",
"lastName": "Baker",
"group": "false"
},
{
"id": "11",
"firstName": "Charlotte",
"lastName": "Garcia",
"group": "false"
},
{
"id": "7",
"firstName": "Henry",
"lastName": "Thompson",
"group": "false"
},
{
"id": "24",
"firstName": "Elijah",
"lastName": "Miller",
"group": "false"
}
]
}
]
}
}
I need to form a new object from response:
{
"success": true,
"body": {
"users": [
{
"type": "unknown",
"data": [
{
"id": "8",
"group": "false"
},
{
"id": "11",
"group": "false"
},
{
"id": "7",
"group": "false"
},
{
"id": "24",
"group": "false"
}
]
}
]
}
}
This new list of object I need to post a request.
I have a JSON Schema, but i don't know how i can make this structure.
My problem is that I can't get the values from fields. I'm using the path "body.users.data.find {it.id} .id" but it finds the id array but I need a users list with values from two fields.
As the users node and data node are all list type, you need use two loop:
import groovy.json.JsonSlurper
// text is your json response text
def result = new JsonSlurper().parseText(text)
result.body.users.each{it.data.each{it.remove("firstName");it.remove("lastName")}}
// result is the what you wanted
println(result)
I have json array ob object list name of array is value in this array i have on inside value object i have another object called filed i want access filed object and find duplicate any help?.
Update: i used System.out.println("a : " + Collections.frequency(stringList, "Accounts Receivable")); method but its give me 0 always
{
"",
"value": [
{
"#odata.etag": """,
"createdDateTime": "",
"eTag": """,
"id": "",
"lastModifiedDateTime": "",
"webUrl": "",
"createdBy": {
"user": {
"email": "",
"id": "",
"displayName": ""
}
},
"lastModifiedBy": {
"user": {
"email": ",
"id": "",
"displayName": ""
}
},
"parentReference": {},
"contentType": {
"id": ""
},
"fields#odata.context": "",
"fields": {
"#odata.etag": """,
"id": "",
"ContentType": "",
"Modified": "",
"Created": "",
"AuthorLookupId": "",
"EditorLookupId": "",
"_UIVersionString": "",
"Attachments": false,
"Edit": "",
"ItemChildCount": "0",
"FolderChildCount": "0",
"_ComplianceFlags": "",
"_ComplianceTag": "",
"_ComplianceTagWrittenTime": "",
"_ComplianceTagUserId": "",
"JobCategory": "",--> "check this value for duplicate"
"Competency": "",
"Level": "",
"CompetencyType": "",
"Index": "0",
"FormID#odata.type": "",
"FormID": 25
}
}]
in side value array list there is fields object but while trying its given only object here is my Code.
Set<Example> uniqueSet = new HashSet<Example>(borrowModelList);
for (Example temp : uniqueSet) {
txtInfo.setText(String.valueOf(Collections.frequency(borrowModelList,temp.getValue().get(0).getFields())));
textInfoOne.setText(String.valueOf(Collections.frequency(borrowModelList,temp.getValue().get(0).getFields())));
textInfoTwo.setText(String.valueOf(Collections.frequency(borrowModelList,temp.getValue().get(0).getFields())));
textInfoThree.setText(String.valueOf(Collections.frequency(borrowModelList,temp.getValue().get(0).getFields())));
textInfoFour.setText(String.valueOf(Collections.frequency(borrowModelList,temp.getValue().get(0).getFields())));
}
I/System.out: 1
in side field object there is JobCategory field i want to check only and show on textview.
i trying to loop through JSON in Java for Android.
my JSON- looks like this:
{
"departureList": [
{
"stopID": "80000589",
"x": "10.88553",
"y": "48.36553",
"mapName": "WGS84[dd.ddddd]",
"area": "4",
"platform": "5",
"platformName": "5",
"stopName": "Augsburg Hbf",
"nameWO": "Hauptbahnhof",
"pointType": "Gleis",
"countdown": "1",
"dateTime": {
"year": "2018",
"month": "8",
"day": "28",
"weekday": "3",
"hour": "9",
"minute": "54"
},
"servingLine": {
"key": "57120",
"code": "6",
"number": "RB 57120 Regionalbahn",
"symbol": "",
"motType": "0",
"mtSubcode": "0",
"realtime": "0",
"direction": "Donauwörth, Bahnhof",
"directionFrom": "München Hbf",
"trainType": "RB",
"trainName": "Regionalbahn",
"trainNum": "57120",
"name": "Regionalbahn",
"liErgRiProj": {
"line": "90910",
"project": "j18",
"direction": "R",
"supplement": " ",
"network": "ddb"
},
"destID": "2505500",
"stateless": "ddb:90910: :R:j18"
},
"operator": {
"code": "00",
"name": "DB AG",
"publicCode": ""
},
"prevStopSeq": {
"name": "",
"nameWO": "",
"place": "",
"nameWithPlace": "",
"omc": "-1",
"placeID": "-1",
"platformName": "",
"desc": "",
"ref": {
"id": "0",
"area": "",
"platform": "",
"attrs": [],
"coords": "",
"arrDelay": "0",
"arrValid": "0",
"depDelay": "0",
"depValid": "0"
}
},
"onwardStopSeq": {
"name": "",
"nameWO": "",
"place": "",
"nameWithPlace": "",
"omc": "-1",
"placeID": "-1",
"platformName": "",
"desc": "",
"ref": {
"id": "0",
"area": "",
"platform": "",
"attrs": [],
"coords": "",
"arrDelay": "0",
"arrValid": "0",
"depDelay": "0",
"depValid": "0"
}
}
},
{
"stopID": "80000589",
"x": "10.88566",
"y": "48.36558",
"mapName": "WGS84[dd.ddddd]",
"area": "4",
"platform": "4",
"platformName": "4",
"stopName": "Augsburg Hbf",
"nameWO": "Hauptbahnhof",
"pointType": "Gleis",
"countdown": "2",
"dateTime": {
"year": "2018",
"month": "8",
"day": "28",
"weekday": "3",
"hour": "9",
"minute": "55"
},
"servingLine": {
"key": "511",
"code": "6",
"number": "ICE 511 InterCityExpress",
"symbol": "",
"motType": "0",
"mtSubcode": "0",
"realtime": "0",
"direction": "München Hbf",
"directionFrom": "Köln Hbf",
"trainType": "ICE",
"trainName": "InterCityExpress",
"trainNum": "511",
"name": "InterCityExpress",
"liErgRiProj": {
"line": "98X42",
"project": "j18",
"direction": "H",
"supplement": "A",
"network": "ddb"
},
"destID": "80000689",
"stateless": "ddb:98X42:A:H:j18"
},
"operator": {
"code": "00",
"name": "DB AG",
"publicCode": ""
},
"attrs": [
{
"name": "lineType",
"value": "HIGHSPEEDTRAIN"
},
{
"name": "lineType",
"value": "LONG_DISTANCE_TRAINS"
},
{
"name": "lineType",
"value": "SUPPLEMENT"
}
],
"prevStopSeq": {
"name": "",
"nameWO": "",
"place": "",
"nameWithPlace": "",
"omc": "-1",
"placeID": "-1",
"platformName": "",
"desc": "",
"ref": {
"id": "0",
"area": "",
"platform": "",
"attrs": [],
"coords": "",
"arrDelay": "0",
"arrValid": "0",
"depDelay": "0",
"depValid": "0"
}
},
"onwardStopSeq": {
"name": "",
"nameWO": "",
"place": "",
"nameWithPlace": "",
"omc": "-1",
"placeID": "-1",
"platformName": "",
"desc": "",
"ref": {
"id": "0",
"area": "",
"platform": "",
"attrs": [],
"coords": "",
"arrDelay": "0",
"arrValid": "0",
"depDelay": "0",
"depValid": "0"
}
}
}
]
}
My goal is to get the values for the attribute number under the array servingLine.
JSONObject parentObject = new JSONObject(finalJson);
deparray = parentObject.getJSONArray("departureList");
JSONObject getdep= deparray.toJSONObject(deparray);
JSONArray getservingArray = getdep.getJSONArray("servingLine");
JSONObject lineobject= getservingArray.toJSONObject(getservingArray);
String trainnumber = lineobject.getString("number");
My Idea behind the code:
Convert the whole JSON-String in a JSON Object.
Look in this Object for the JSON-Array departureList
then look in this Array for servingLine, get it and convert it to an JSON-Object.
Now look for the specific string.
What is wrong on this idea ?
My problem is, that, i get the error "No value for servingLine". But why? And how can i fix it? It seems, he can't parse the whole JSON-String. And how can i prevent this?
JSONObject parentObject = new JSONObject(finalJson);
JSONArray deparray = parentObject.getJSONArray("departureList");
for (int i = 0; i < deparray.length(); i++) {
JSONObject getdep = deparray.getJSONObject(i);
JSONObject lineobject = getdep.getJSONObject("servingLine");
String trainnumber = lineobject.getString("number");
}
This is how I would do it assuming that given json is correct one.
I am having this in JSONArray ary2;
JSON string is :
[
{
"item_id": "1",
"Head_item_id": "1",
"major_item_id": "1",
"Quantity": "10",
"selling_prize": "20",
"MRP": "90",
"title": "JK Lakshmi PPC Cement",
"SKU": "B2WBUICEM2"
},
{
"item_id": "2",
"Head_item_id": "1",
"major_item_id": "1",
"Quantity": "10",
"selling_prize": "30",
"MRP": "80",
"title": "JK Lakshmi PPC Cement",
"SKU": "B2WBUICEM2"
},
{
"item_id": "3",
"Head_item_id": "1",
"major_item_id": "1",
"Quantity": "10",
"selling_prize": "10",
"MRP": "70",
"title": "Shree Ultra OPC 43 Grade Cement",
"SKU": "B2WBUICEM5"
}
]
I want to get String array for each attribute , like :
String[] item_id;
String[] Head_item_id;
etc.
unable to get it. Please help.
Try This:
JSONArray arr = new JSONArray(yourJSONresponse);
String[] item_id=new String[array.length()];
String[] Head_item_id=new String[arr .length()];
for(int i = 0; i < arr.length(); i++){
item_id[i]=arr.getJSONObject(i).getString("item_id");
Head_item_id[i]=arr.getJSONObject(i).getString("Head_item_id");
}
You need to follow these steps:
-> Create POJO for your JSON data and provite getters and setters for each attribute.
-> Parse your JSON data using GSON
-> get the desired attribute(using getters) and store it in the respective string array