Changing keys names in a nested josn object in java - java

Could anyone help me with this issue please, I have a JSONObject which is highly nested to many levels and I want to replace certain keys with different values? Below is my JSONOBject:
{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Put_Location_Request",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Location_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Location_Data",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Location_ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Effective_Date",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Location_Name",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Location_Usage_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Location_Type_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Location_Attribute_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Superior_Location_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Inactive",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Latitude",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Longitude",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Altitude",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Time_Profile_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Locale_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Display_Language_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Time_Zone_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Default_Currency_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "External_Name",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Default_Job_Posting_Location_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Trade_Name",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Worksite_ID_Code",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Global_Location_Number",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Location_Identifier",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Default_Ship-To_Location_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Associate_Deliver-To_Location_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Instructional_Site_Data",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Academic_Unit_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Instructional_Site_Data_Capacity",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Off-Site",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Instructional_Use_Only",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Student_Housing_Assignment_Data",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Academic_Unit_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Location_Training_Room_Data",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Link",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Capacity",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Directions",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Equipment",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Reservation_Details",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Video_Conference_Details",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Additional_Details",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, {
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Contact_Data",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Address_Data",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "Country_Reference",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": [{
"nullable": null,
"localPart": null,
"prefix": null,
"precision": null,
"name": "ID",
"namespace": null,
"scale": null,
"unicode": null,
"type": null,
"maxLength": null,
"srid": null,
"subEntities": null
}]
}, .........
In the above JSONObject I want to replace key nullable with $nullable and localPart with $localPart etc. Can anyone help me with Java code.

I solved the problem with the following code:
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class ReplaceKeyInNestedJson {
public static void removeJSONField(JSONObject obj) throws JSONException {
obj.put("$nullable", obj.remove("nullable"));
obj.put("$localPart", obj.remove("localPart"));
obj.put("$prefix", obj.remove("prefix"));
obj.put("$precision", obj.remove("precision"));
obj.put("$name", obj.remove("name"));
obj.put("$namespace", obj.remove("namespace"));
obj.put("$scale", obj.remove("scale"));
Iterator<String> it = obj.keys();
while (it.hasNext()) {
String key = it.next();
Object childObj = obj.get(key);
if (childObj instanceof JSONArray) {
JSONArray arrayChildObjs = ((JSONArray) childObj);
int size = arrayChildObjs.length();
for (int i = 0; i < size; i++) {
removeJSONField(arrayChildObjs.getJSONObject(i));
}
}
if (childObj instanceof JSONObject) {
removeJSONField(((JSONObject) childObj));
}
}
}
public static void main(String[] args) {
String jsonStr = "{\n" + " \"nullable\": null,\n" + " \"localPart\": null,\n" + " \"prefix\": null,\n"
+ " \"precision\": null,\n" + " \"name\": \"Put_Location_Request\",\n" + " \"namespace\": null,\n"
+ " \"scale\": null,\n" + " \"unicode\": null,\n" + " \"type\": null,\n" + " \"maxLength\": null,\n"
+ " \"srid\": null,\n" + " \"subEntities\": [\n" + " {\n" + " \"nullable\": null,\n"
+ " \"localPart\": null,\n" + " \"prefix\": null,\n" + " \"precision\": null,\n"
+ " \"name\": \"Location_Reference\",\n" + " \"namespace\": null,\n"
+ " \"scale\": null,\n" + " \"unicode\": null,\n" + " \"type\": null,\n"
+ " \"maxLength\": null,\n" + " \"srid\": null,\n" + " \"subEntities\": [{\n"
+ " \"nullable\": null,\n" + " \"localPart\": null,\n" + " \"prefix\": null,\n"
+ " \"precision\": null,\n" + " \"name\": \"ID\",\n"
+ " \"namespace\": null,\n" + " \"scale\": null,\n" + " \"unicode\": null,\n"
+ " \"type\": null,\n" + " \"maxLength\": null,\n" + " \"srid\": null,\n"
+ " \"subEntities\": null\n" + " }]\n" + " }, {\n" + " \"nullable\": null,\n"
+ " \"localPart\": null,\n" + " \"prefix\": null,.............";
try {
JSONObject obj = new JSONObject(jsonStr);
removeJSONField(obj);
System.out.println(obj);
} catch (JSONException e) {
e.printStackTrace();
}
}}

If you are using faster.Jackson library you can solve like below:
import java.io.IOException;
import java.util.Iterator;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class ReplaceKeyInNestedJson2 {
private static final String FIELD_TYPE = "FieldType";
public static JsonNode removeJSONField(JsonNode obj) {
ObjectNode object = (ObjectNode) obj;
object.put("$kind", FIELD_TYPE);
object.set("$nullable", object.get("nullable"));
object.remove("nullable");
object.set("$localPart", object.get("localPart"));
object.remove("localPart");
object.set("$prefix", object.get("prefix"));
object.remove("prefix");
object.set("$precision", object.get("precision"));
object.remove("precision");
object.set("$name", object.get("name"));
object.remove("name");
object.set("$namespace", object.get("namespace"));
object.remove("namespace");
object.set("$scale", object.get("scale"));
object.remove("scale");
Iterator<String> it = obj.fieldNames();
while (it.hasNext()) {
String key = it.next();
JsonNode childObj = obj.get(key);
if (childObj.isArray()) {
ArrayNode arrayChildObjs = ((ArrayNode) childObj);
int size = arrayChildObjs.size();
for (int i = 0; i < size; i++) {
removeJSONField(arrayChildObjs.get(i));
}
}
if (childObj.isObject()) {
removeJSONField(((JsonNode) childObj));
}
}
return obj;
}
public static void main(String[] args) throws IOException {
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode obj = mapper.readTree(StaticData.fieldsJson);
JsonNode result = removeJSONField(obj);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Related

Json is not extracted to Java POJO object

I'm using rest-assured with Gson for testing when getting a response from the BE service I'm creating a POJO object that deserializes the JSON and creates and object, but for some reason all object fields are null, FYI I'm using the same practice at another API calls and it works, but in this case, it's not.
{
"allLanguages": [
"English"
],
"Messages": [
{
"Messages": null
}
],
"Content": [
{
"Subject": "EmailWithDOCAttachmentJCBKMJJ Test Email with docx attachment"
},
{
"Content": "An email includes Docx file as an attachment"
},
{
"Content Html": "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:w=\"urn:schemas-microsoft-com:office:word\" xmlns:m=\"http://schemas.microsoft.com/office/2004/12/omml\" xmlns=\"http://www.w3.org/TR/REC-html40\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=us-ascii\"><meta name=\"Generator\" content=\"Microsoft Word 15 (filtered medium)\"><style><!--/* Font Definitions */#font-face{font-family:\"Cambria Math\";panose-1:2 4 5 3 5 4 6 3 2 4;}#font-face{font-family:Calibri;panose-1:2 15 5 2 2 2 4 3 2 4;}/* Style Definitions */p.MsoNormal, li.MsoNormal, div.MsoNormal{margin:0cm;font-size:11.0pt;font-family:\"Calibri\",sans-serif;}span.EmailStyle17{mso-style-type:personal-compose;font-family:\"Calibri\",sans-serif;color:windowtext;}.MsoChpDefault{mso-style-type:export-only;font-family:\"Calibri\",sans-serif;}#page WordSection1{size:612.0pt 792.0pt;margin:72.0pt 72.0pt 72.0pt 72.0pt;}div.WordSection1{page:WordSection1;}--></style></head><body lang=\"en-IL\" link=\"#0563C1\" vlink=\"#954F72\" style=\"word-wrap:break-word\"><div class=\"WordSection1\"><p class=\"MsoNormal\"><span lang=\"EN-US\">An email includes Docx file as an attachment<o:p></o:p></span></p></div></body></html>"
},
{
"Record Date": "2023-01-30 12:27:53.000"
}
],
"Generic": [
{
"Date": "2023-01-30 12:27:53.000"
},
{
"Date Added": "2023-01-30 10:30:51.146"
},
{
"Date Time Created": "2023-01-30 12:27:53.000"
},
{
"Date Time Received": "2023-01-30 10:27:58.090"
},
{
"Index Name": "dr_smtpemail_2023.01.01_999"
},
{
"Ecomm Id": "6BF42EAC97D5C20A071D34AB7BCE7B8C"
},
{
"archive Ids": "[2]"
},
{
},
{
},
{
"Record Date": "2023-01-30 12:27:53.000"
},
{
"Ecomm Type": "E-Mail"
},
{
"Platform Name": "SMTPEmail"
},
{
},
{
"Retention date": "2023-07-31 12:27:53.000"
},
{
"archiveRoot": "E-Mail/SMTPEmail/2023/01/30"
},
{
"platformDisplayName": "SMTP Email"
}
],
"Product": [
{
"Product": [
]
}
],
"transcribedMessages": [
{
"transcribedMessages": null
}
],
"Participants": [
{
"Participants": [
{
"Participant Account Bsk Country": null,
"Participant Is Surveillance Data": null,
"Participant Emp Global ID": null,
"Participant JOB ID": null,
"Participant Account Name": null,
"Participant AIM": null,
"Participant External Company": null,
"Participant Parent Group Display": null,
"Participant Type": "unknown",
"Participant LEV5": null,
"Participant First Name": null,
"Participant LEV4": null,
"Participant LEV6": null,
"Participant YJ": null,
"Participant Is Record Keeping": null,
"Participant Email": "monitor#ggg.com",
"Participant Unique Id": "monitor#ggg.com",
"Participant Manager Name": null,
"Participant Skype": null,
"Participant Bank Department": null,
"Participant Department Display": null,
"Participant Business Unit": null,
"Participant Division": null,
"Participant City": null,
"Participant Account Manager": null,
"Participant Department": null,
"Participant Name For Viewer": null,
"Participant Emp Sub Unit": null,
"Participant Job Description": null,
"Participant TCCID": null,
"Nationality Code": null,
"Participant Name": null,
"Participant Org Sub Unit": null,
"Participant Yahoo": null,
"Participant Id": null,
"Participant Display Name": null,
"Participant Region": null,
"Participant Trader ID": null,
"Participant Parent Group": null,
"Participant Manager ID": null,
"Participant Org Unit Name": null,
"Participant Country Code": null,
"Participant CLI": null,
"Participant Is Internal": "false",
"Participant Employee Status": null,
"Participant Last Name": null,
"Participant Delivery Type": null,
"Participant Nationality Code": null,
"Participant IMID": null,
"Participant SSN": null,
"Participant Account Number": null,
"Participant Secondary Email": null,
"Participant Role Type Name": null
},
{
"Participant Account Bsk Country": null,
"Participant Is Surveillance Data": "true",
"Participant Emp Global ID": "employeeIdGlobal-Adele",
"Participant JOB ID": "jobIdAdele",
"Participant Account Name": null,
"Participant AIM": null,
"Participant External Company": null,
"Participant Parent Group Display": "monitored group",
"Participant Type": "customer",
"Participant LEV5": "lev5-Adele",
"Participant First Name": "Adele",
"Participant LEV4": "lev4-Adele",
"Participant LEV6": "lev6-Adele",
"Participant YJ": null,
"Participant Is Record Keeping": "false",
"Participant Email": "adelev#ggg.onmicrosoft.com",
"Participant Unique Id": "Z778714Adele",
"Participant Manager Name": "manager-for Adele",
"Participant Skype": null,
"Participant Bank Department": null,
"Participant Department Display": "monitored group",
"Participant Business Unit": "businessUnit-Adele",
"Participant Division": "division-Adele",
"Participant City": "City-Adele",
"Participant Account Manager": null,
"Participant Department": "monitored_system_group",
"Participant Name For Viewer": null,
"Participant Emp Sub Unit": "division-Adele",
"Participant Job Description": "jobDescription-Adele",
"Participant TCCID": null,
"Nationality Code": "nationalityCode-Adele",
"Participant Name": "Adele Vance",
"Participant Org Sub Unit": "organizationalUnit-Adele",
"Participant Yahoo": null,
"Participant Id": "Z778714Adele",
"Participant Display Name": null,
"Participant Region": "AM",
"Participant Trader ID": null,
"Participant Parent Group": "monitored_system_group",
"Participant Manager ID": "11005328",
"Participant Org Unit Name": "orgUnitId-Adele",
"Participant Country Code": null,
"Participant CLI": null,
"Participant Is Internal": "true",
"Participant Employee Status": null,
"Participant Last Name": "Vance",
"Participant Delivery Type": null,
"Participant Nationality Code": "nationalityCode-Adele",
"Participant IMID": null,
"Participant SSN": null,
"Participant Account Number": null,
"Participant Secondary Email": null,
"Participant Role Type Name": null
},
{
"Participant Account Bsk Country": null,
"Participant Is Surveillance Data": "true",
"Participant Emp Global ID": "employeeIdGlobal-Alex",
"Participant JOB ID": "jobIdAlex",
"Participant Account Name": null,
"Participant AIM": null,
"Participant External Company": null,
"Participant Parent Group Display": "monitored group",
"Participant Type": "advisor",
"Participant LEV5": "lev5-Alex",
"Participant First Name": "Alex",
"Participant LEV4": "lev4-Alex",
"Participant LEV6": "lev6-Alex",
"Participant YJ": null,
"Participant Is Record Keeping": "false",
"Participant Email": "alexw#gggg.onmicrosoft.com",
"Participant Unique Id": "N306800Alex",
"Participant Manager Name": "manager-for Alex",
"Participant Skype": null,
"Participant Bank Department": null,
"Participant Department Display": "ggg monitored group",
"Participant Business Unit": "businessUnit-Alex",
"Participant Division": "division-Alex",
"Participant City": "City-Alex",
"Participant Account Manager": null,
"Participant Department": "monitored_system_group",
"Participant Name For Viewer": null,
"Participant Emp Sub Unit": "division-Alex",
"Participant Job Description": "jobDescription-Alex",
"Participant TCCID": null,
"Nationality Code": "nationalityCode-Alex",
"Participant Name": "Alex W",
"Participant Org Sub Unit": "organizationalUnit-Alex",
"Participant Yahoo": null,
"Participant Id": "N306800Alex",
"Participant Display Name": null,
"Participant Region": "WB",
"Participant Trader ID": null,
"Participant Parent Group": "monitored_system_group",
"Participant Manager ID": "53002941",
"Participant Org Unit Name": "orgUnitId-Alex",
"Participant Country Code": null,
"Participant CLI": null,
"Participant Is Internal": "true",
"Participant Employee Status": null,
"Participant Last Name": "W",
"Participant Delivery Type": null,
"Participant Nationality Code": "nationalityCode-Alex",
"Participant IMID": null,
"Participant SSN": null,
"Participant Account Number": null,
"Participant Secondary Email": null,
"Participant Role Type Name": null
}
]
},
{
"Participants Count": "3"
}
],
"Information": [
{
},
{
"Languages": null
},
{
"Legal hold": "false"
},
{
},
{
"Date Received": "2023-01-30 10:27:58.090"
},
{
"Date sent": "2023-01-30 12:27:53.000"
},
{
"Date Created": "2023-01-30 12:27:53.000"
},
{
"Retention Date": "2023-07-31 12:27:53.000"
},
{
"Ecomm Date": "2023-01-30 12:27:53.000"
},
{
},
{
},
{
},
{
},
{
},
{
"Message Id": "<643565394.0.1675074473937.JavaMail.alitaha#ip-192-168-31-20.eu-west-1.compute.internal>"
},
{
},
{
},
{
},
{
},
{
},
{
"Unique ID": "6BF42EAC97D5C20A071D34AB7BCE7B8C"
}
],
"Attachments": [
{
"Attachments": [
{
"parent": null,
"isPasswordProtected": "false",
"sha256": "60984071781d33399dd72ebbd3e06b9e481ec4cd1d89ef38218fc1653732df",
"fileSize": "6122",
"archiveFilename": "/7_2023-01-30_10-30-29.444/Tasks/27/attachments/488E00680FF11FFED143409A9F7575B5-Automationtest.docx",
"fileContent": "Automation test"
}
]
}
],
"primaryLanguage": "English"
}
here are the relevant POJOS files:
#Getter
#Setter
#ToString
#JsonIgnoreProperties(ignoreUnknown = true)
public class OriginalDTO {
#SerializedName("Attachments")
private List<Attachment> attachments;
#SerializedName("BccRecipients")
private List<BccRecipient> bccRecipients;
#SerializedName("CcRecipients")
private List<CcRecipient> ccRecipients;
#SerializedName("Content")
private List<Content> content;
#SerializedName("FromRecipients")
private List<FromRecipient> fromRecipients;
#SerializedName("Generic")
private List<Generic> generic;
#SerializedName("Information")
private List<Information> information;
#SerializedName("Messages")
private List<Message> messages;
#SerializedName("Participants")
private List<Participant> participants;
#SerializedName("Product")
private List<Product> product;
#SerializedName("ToRecipients")
private List<ToRecipient> toRecipients;
#SerializedName("transcribedMessages")
private List<TranscribedMessage> transcribedMessages;
}
#Getter
#Setter
#ToString
#JsonIgnoreProperties(ignoreUnknown = true)
public class OriginalDTO {
#SerializedName("Attachments")
private List<Attachment> attachments;
#SerializedName("BccRecipients")
private List<BccRecipient> bccRecipients;
#SerializedName("CcRecipients")
private List<CcRecipient> ccRecipients;
#SerializedName("Content")
private List<Content> content;
#SerializedName("FromRecipients")
private List<FromRecipient> fromRecipients;
#SerializedName("Generic")
private List<Generic> generic;
#SerializedName("Information")
private List<Information> information;
#SerializedName("Messages")
private List<Message> messages;
#SerializedName("Participants")
private List<Participant> participants;
#SerializedName("Product")
private List<Product> product;
#SerializedName("ToRecipients")
private List<ToRecipient> toRecipients;
#SerializedName("transcribedMessages")
private List<TranscribedMessage> transcribedMessages;
}
#Getter
#Setter
#ToString
#JsonIgnoreProperties(ignoreUnknown = true)
public class Attachment {
#SerializedName("archiveFilename")
private String archiveFilename;
#SerializedName("Attachments")
private List<Attachment> attachments;
#SerializedName("fileContent")
private String fileContent;
#SerializedName("fileSize")
private String fileSize;
#SerializedName("isPasswordProtected")
private String isPasswordProtected;
#SerializedName("parent")
private String parent;
#SerializedName("sha256")
private String sha256;
}
And do on, in order to create the java object I'm using this code:
OriginalDTO original = response.then().statusCode(200).extract().as(OriginalDTO.class);
but for some reason all OriginalDTO fields values are null.
what I need to fix in this code
I handled this with work around using
Gson gson = new GsonBuilder().create();
OriginalDTO original = gson.fromJson(response.body().asString(), OriginalDTO.class);
instead of
OriginalDTO original =response.then().statusCode(200).extract().as(OriginalDTO.class)

How to get org.apache.kafka.connect.data.Decimal value from Kafka JSON message [duplicate]

This question already has answers here:
How to deserialize BigDecimal value received from kafka broker through debezium CDC mechanism?
(3 answers)
Closed 3 years ago.
I use debizium to stream postgresql data to Kafka, and use Java to subscribe Kafka topic.
I receive Kafka message and get a JSON string, but one numeric value can not recognized.
The JSON is:
{
"schema":
{
"type": "struct",
"fields": [
{
"type": "struct",
"fields": [
{
"type": "string",
"optional": true,
"field": "creator"
},
{
"type": "int64",
"optional": true,
"name": "io.debezium.time.MicroTimestamp",
"version": 1,
"field": "createtime"
},
{
"type": "bytes",
"optional": true,
"name": "org.apache.kafka.connect.data.Decimal",
"version": 1,
"parameters":
{
"scale": "5",
"connect.decimal.precision": "32"
},
"field": "familyprice"
}],
"optional": true,
"name": "pssdev.public.order.Value",
"field": "before"
},
{
"type": "struct",
"fields": [
{
"type": "string",
"optional": true,
"field": "creator"
},
{
"type": "int64",
"optional": true,
"name": "io.debezium.time.MicroTimestamp",
"version": 1,
"field": "createtime"
},
{
"type": "bytes",
"optional": true,
"name": "org.apache.kafka.connect.data.Decimal",
"version": 1,
"parameters":
{
"scale": "5",
"connect.decimal.precision": "32"
},
"field": "familyprice"
}],
"optional": true,
"name": "pssdev.public.order.Value",
"field": "after"
},
{
"type": "struct",
"fields": [
{
"type": "string",
"optional": true,
"field": "version"
},
{
"type": "string",
"optional": true,
"field": "connector"
},
{
"type": "string",
"optional": false,
"field": "name"
},
{
"type": "string",
"optional": false,
"field": "db"
},
{
"type": "int64",
"optional": true,
"field": "ts_usec"
},
{
"type": "int64",
"optional": true,
"field": "txId"
},
{
"type": "int64",
"optional": true,
"field": "lsn"
},
{
"type": "string",
"optional": true,
"field": "schema"
},
{
"type": "string",
"optional": true,
"field": "table"
},
{
"type": "boolean",
"optional": true,
"default": false,
"field": "snapshot"
},
{
"type": "boolean",
"optional": true,
"field": "last_snapshot_record"
},
{
"type": "int64",
"optional": true,
"field": "xmin"
}],
"optional": false,
"name": "io.debezium.connector.postgresql.Source",
"field": "source"
},
{
"type": "string",
"optional": false,
"field": "op"
},
{
"type": "int64",
"optional": true,
"field": "ts_ms"
}],
"optional": false,
"name": "pssdev.public.order.Envelope"
},
"payload":
{
"before":
{
"creator": null,
"createtime": null,
"familyprice": null
},
"after":
{
"creator": "USER1E",
"createtime": 1554292597815000,
"familyprice": "W42A"
},
"source":
{
"version": "0.9.5.Final",
"connector": "postgresql",
"name": "pssdev",
"db": "pf",
"ts_usec": 1561459811737920,
"txId": 771604,
"lsn": 88282458880,
"schema": "public",
"table": "order",
"snapshot": false,
"last_snapshot_record": null,
"xmin": null
},
"op": "u",
"ts_ms": 1561459811747
}
}
The familyprice value is W42A I can't figure out how to convert it.
The actual familyprice value in database is 60.00000, the column type is numeric(32,5)
Please see https://debezium.io/documentation/faq/#how_to_retrieve_decimal_field_from_binary_representation for Java clients.
You can also set decimal.handling.mode to different value so you'll reciev the Decimal either as string or double if it easier for you.

how to parse json array using retrofit android [duplicate]

This question already has answers here:
Parse JSON array response using Retrofit & Gson
(4 answers)
Closed 4 years ago.
{
"name": "",
"link": "",
"menuId": "",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Dashboard",
"link": "/welcome",
"menuId": "das",
"icon": "fa fa-tachometer fa-2x",
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Master",
"link": "",
"menuId": "emp",
"icon": "fa fa-user-plus fa-2x",
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Roles",
"link": "/myRoles",
"menuId": "emp-usr-rol",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Create Role",
"link": null,
"menuId": "emp-usr-rol-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Delete Role",
"link": null,
"menuId": "emp-usr-rol-del",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Edit Role",
"link": null,
"menuId": "emp-usr-rol-edt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "List Role",
"link": null,
"menuId": "emp-usr-rol-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "User",
"link": "/users",
"menuId": "emp-usr",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Create User",
"link": null,
"menuId": "emp-usr-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Delete User",
"link": null,
"menuId": "emp-usr-del",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Edit User",
"link": null,
"menuId": "emp-usr-edt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "List User",
"link": null,
"menuId": "emp-usr-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Departments",
"link": "/department",
"menuId": "emp-mst-dpt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Create Department",
"link": null,
"menuId": "emp-mst-dpt-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Delete Department",
"link": null,
"menuId": "emp-mst-dpt-del",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Edit Department",
"link": null,
"menuId": "emp-mst-dpt-edt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "List Department",
"link": null,
"menuId": "emp-mst-dpt-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Salary Template",
"link": "",
"menuId": "emp-usr-rol",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Create Salary Template",
"link": null,
"menuId": "emp-usr-rol-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Delete Salary Template",
"link": null,
"menuId": "emp-usr-rol-del",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Edit Salary Template",
"link": null,
"menuId": "emp-usr-rol-edt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "List Salary Template",
"link": null,
"menuId": "emp-usr-rol-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Job Schedular",
"link": "",
"menuId": "emp-usr-rol",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Create Job Schedular",
"link": null,
"menuId": "emp-usr-rol-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Delete Job Schedular",
"link": null,
"menuId": "emp-usr-rol-del",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Edit Job Schedular",
"link": null,
"menuId": "emp-usr-rol-edt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "List Job Schedular",
"link": null,
"menuId": "emp-usr-rol-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Shift Management",
"link": "",
"menuId": "emp-usr-rol",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Create Shift Management",
"link": null,
"menuId": "emp-usr-rol-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Delete Shift Management",
"link": null,
"menuId": "emp-usr-rol-del",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Edit Shift Management",
"link": null,
"menuId": "emp-usr-rol-edt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "List Shift Management",
"link": null,
"menuId": "emp-usr-rol-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Leave Management",
"link": "/holidays",
"menuId": "hrm",
"icon": "fa fa-briefcase fa-2x",
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Holidays",
"link": "/holidays",
"menuId": "emp-mst-hdy",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Create Holiday",
"link": null,
"menuId": "emp-mst-hdy-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Delete Holiday",
"link": null,
"menuId": "emp-mst-hdy-del",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Edit Holiday",
"link": null,
"menuId": "emp-mst-hdy-edt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "List Holiday",
"link": null,
"menuId": "emp-mst-hdy-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Leave Template",
"link": "",
"menuId": "emp-usr-rol",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Create Leave Template",
"link": null,
"menuId": "emp-usr-rol-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Delete Leave Template",
"link": null,
"menuId": "emp-usr-rol-del",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Edit Leave Template",
"link": null,
"menuId": "emp-usr-rol-edt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "List Leave Template",
"link": null,
"menuId": "emp-usr-rol-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
}
]
}
]
},
{
"name": "Employee Management",
"link": "",
"menuId": "emng",
"icon": "fa fa-user fa-2x",
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Employee",
"link": "/users",
"menuId": "emng-emp",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Add Employees",
"link": "/addEmployee",
"menuId": "emng-emp-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Delete Employees",
"link": null,
"menuId": "emng-emp-del",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Edit Employees",
"link": null,
"menuId": "emng-emp-edt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "List Employees",
"link": null,
"menuId": "emng-emp-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Leave Approval",
"link": "/leaveApproval",
"menuId": "emng-lea",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "List Leave Approval",
"link": null,
"menuId": "emng-lea-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Edit Leave Approval",
"link": null,
"menuId": "emng-lea-edt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Monthly Attendance",
"link": "/monthlyAttendance",
"menuId": "emng-mta",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "List Monthly Attendance",
"link": null,
"menuId": "emng-mta-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Daily Attendance",
"link": "/dailyAttendance",
"menuId": "emng-dla",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "List Daily Attendance",
"link": null,
"menuId": "emng-dla-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "In Out Register",
"link": "/viewInOutRegister",
"menuId": "emng-ior",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "List In Out Register",
"link": null,
"menuId": "emng-ior-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
}
]
},
{
"name": "Payroll Management",
"link": "",
"menuId": "pmng",
"icon": "fa fa-paypal fa-2x",
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Salary Structure Template",
"link": "/overtime",
"menuId": "pmng-ot",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Add Salary Structure Template",
"link": null,
"menuId": "pmng-ot-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "List Salary Structure Template",
"link": null,
"menuId": "pmng-ot-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Association of template for Employee",
"link": "",
"menuId": "pmng-bnk",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": " View template for Employee",
"link": null,
"menuId": "pmng-bnk-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "delete template for Employee",
"link": null,
"menuId": "pmng-bnk-del",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Salary Slip",
"link": "",
"menuId": "pmng-ot",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Employee Profile",
"link": "",
"menuId": "edsh",
"icon": "fa fa-users fa-2x",
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Personal Details",
"link": "",
"menuId": "pmng-dtls",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Attendance",
"link": "",
"menuId": "edsh-emp",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "View Daily Attendance",
"link": null,
"menuId": "edsh-emp-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "View Monthly Attendance",
"link": null,
"menuId": "edsh-emp-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Salary Pacakge Structure",
"link": "",
"menuId": "pmng-bnk",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": " View Salary Pacakge Structure ",
"link": null,
"menuId": "edsh-ler-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": " Pay Slip ",
"link": null,
"menuId": "edsh-ler-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Leave Application",
"link": "/leaveRequest",
"menuId": "edsh-ler",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "Apply Leave ",
"link": null,
"menuId": "edsh-ler-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "View Leave Request",
"link": null,
"menuId": "edsh-ler-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Edit Leave Request",
"link": null,
"menuId": "edsh-ler-edt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Shift Timings",
"link": "",
"menuId": "edsh-ior",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "View Shift Timings",
"link": null,
"menuId": "edsh-ior-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Holiday List",
"link": "/holidayList",
"menuId": "hld-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": [
{
"name": "List Holiday",
"link": null,
"menuId": "emp-hld-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
}
]
},
{
"name": "Package",
"link": "/pack",
"menuId": "set-pkg",
"icon": "fa fa-cogs fa-2x",
"desc": null,
"selected": true,
"subMenus": [
{
"name": "Create package",
"link": null,
"menuId": "set-pkg-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Delete package",
"link": null,
"menuId": "set-pkg-del",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Edit package",
"link": null,
"menuId": "set-pkg-edt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "List package",
"link": null,
"menuId": "set-pkg-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
},
{
"name": "Organization",
"link": "/org",
"menuId": "set-org",
"icon": "fa fa-cogs fa-2x",
"desc": null,
"selected": true,
"subMenus": [
{
"name": "Create organization",
"link": null,
"menuId": "set-org-add",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Delete organization",
"link": null,
"menuId": "set-org-del",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "Edit organization",
"link": null,
"menuId": "set-org-edt",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
},
{
"name": "List organization",
"link": null,
"menuId": "set-org-lst",
"icon": null,
"desc": null,
"selected": false,
"subMenus": []
}
]
}
]
}
Use the below link or something similar to first create a Java object class to hold the data
http://www.jsonschema2pojo.org/
The use GsonConverterFactory with Retrofit to convert the object. Below link tells you how to add it
https://futurestud.io/tutorials/retrofit-2-adding-customizing-the-gson-converter
Create Pojo class and implementsSerializable. Use this class to get a response.
try this,
class pojo implements Serializable{
#SerializedName("name")
private String name;
public long getName() {
return name;
}
public void setId(String name) {
this.name = name;
}
}

how to extract value from the json response which is under array and array is under object

enter image description here JSON Response:-
{
"hotelogix": {
"version": "1.0",
"datetime": "2017-01-17T11:37:58",
"response": {
"status": {
"code": 0,
"message": "success"
},
"nightAuditDate": "2015-04-15",
"lastUpdatedOn": "2017-01-17 11:05:40",
"isUpdated": true,
"totalRecords": "2",
"totalPages": 1,
"bookings": [
{
"isGroup": false,
"group": null,
"id": "gbighAuuug||",
"mainId": "gbighAuuug||",
"checkInDate": "2015-04-15",
"checkOutDate": "2015-04-16",
"adult": 2,
"child": 0,
"infant": 0,
"code": "12281852",
"reservationStatus": "RESERVE",
"businessSourcesId": null,
"source": "PMS",
"preference": "",
"roomStays": [
{
"date": "2015-04-15",
"roomTypeId": "gb_BTEs|",
"roomTypeName": "old age rooms",
"roomTypeCode": "CLSccc",
"roomId": "0",
"roomName": "",
"rateId": "gw||",
"rateName": "Seasonal Rate",
"amount": "600.000000",
"tax": "0.000000",
"discountAmount": "0.000000"
}
],
"guestStays": [
{
"id": "h2H4TkT2Bw||",
"mainId": "h2H4TkT2Bw||",
"checkInDate": "2015-04-15",
"checkOutDate": "2015-04-16",
"status": "RESERVE",
"isPrimary": true,
"isChargeSharer": true,
"type": "Adult",
"guestDetails": {
"id": "gsgPzJITYA||",
"code": "P949",
"salutation": null,
"fName": "Neha",
"lName": "Sharma",
"email": "neha#hotelogix.com",
"phoneNo": "55887799",
"mobileNo": "",
"gender": null,
"nationality": null,
"identityTypeId": null,
"identityNo": null,
"isBlackList": false,
"isVip": false,
"dob": "-0001-11-30",
"organization": null,
"designation": null,
"spouseSalutation": null,
"spouseFName": null,
"spouseLName": null,
"spouseDob": "0000-00-00 00:00:00",
"anniversary": "0000-00-00 00:00:00",
"addresses": {
"home": {
"address": "",
"country": null,
"state": null,
"city": "",
"zip": "",
"fax": null
},
"work": {
"address": "",
"country": null,
"state": null,
"city": "",
"zip": "",
"fax": null,
"phone": null,
"mobile": null
}
},
"files": []
}
},
{
"id": "h2H4TkT2Ag||",
"mainId": "h2H4TkT2Ag||",
"checkInDate": "2015-04-15",
"checkOutDate": "2015-04-16",
"status": "RESERVE",
"isPrimary": false,
"isChargeSharer": false,
"type": "Adult",
"guestDetails": {
"id": "gsgPzJITYg||",
"code": "P951",
"salutation": null,
"fName": "Mayajhanti",
"lName": "Jha",
"email": "mayanti#hotelogix.com",
"phoneNo": "01158988888",
"mobileNo": "99680480558",
"gender": "Male",
"nationality": "US",
"identityTypeId": null,
"identityNo": null,
"isBlackList": false,
"isVip": false,
"dob": "-0001-11-30",
"organization": null,
"designation": null,
"spouseSalutation": null,
"spouseFName": null,
"spouseLName": null,
"spouseDob": "0000-00-00 00:00:00",
"anniversary": "0000-00-00 00:00:00",
"addresses": {
"home": {
"address": "D-996, Cross Road",
"country": "US",
"state": "CA",
"city": "Los Angeles",
"zip": "325215",
"fax": null
},
"work": {
"address": "",
"country": null,
"state": null,
"city": "",
"zip": "",
"fax": null,
"phone": null,
"mobile": null
}
},
"files": [
{
"name": "Id Proof",
"url": "c://Users//mukesh//Desktop//abc.png"
},
{
"name": "Id Proof",
"url": "c://Users//mukesh//Desktop//abc.png"
}
]
}
}
],
"payments": [],
"otherCharges": [],
"addons": [],
"isHoldTill": false,
"releaseDate": "0000-00-00 00:00:00"
},
{
"isGroup": true,
"group": {
"id": "h2AIqHf1",
"mainId": "h2AIqHf1",
"checkInDate": "2015-04-15",
"checkOutDate": "2015-04-16",
"code": "G 0117433",
"groupStatus": "RESERVE",
"businessSourcesId": null,
"source": "",
"preference": "",
"ownerType": "Guest",
"owner": {
"id": "gsgPzf2HQw||",
"code": "P957",
"salutation": null,
"fName": "juna",
"lName": "mishra",
"email": "juna#hotelogix.com",
"phoneNo": "",
"mobileNo": "8802640811",
"gender": null,
"nationality": null,
"identityTypeId": null,
"identityNo": null,
"isBlackList": false,
"isVip": false,
"dob": "-0001-11-30",
"organization": null,
"designation": null,
"spouseSalutation": null,
"spouseFName": null,
"spouseLName": null,
"spouseDob": "0000-00-00 00:00:00",
"anniversary": "0000-00-00 00:00:00",
"addresses": {
"home": {
"address": "",
"country": null,
"state": null,
"city": "",
"zip": "",
"fax": null
},
"work": {
"address": "",
"country": null,
"state": null,
"city": "",
"zip": "",
"fax": null,
"phone": null,
"mobile": null
}
},
"files": []
},
"payTerm": 2,
"payments": [],
"otherCharges": [],
"groupLeader": []
},
"id": "gbihNuZBbA||",
"mainId": "gbihNuZBbA||",
"checkInDate": "2015-04-15",
"checkOutDate": "2015-04-16",
"adult": 1,
"child": 0,
"infant": 0,
"code": "01171859",
"reservationStatus": "RESERVE",
"businessSourcesId": null,
"source": "PMS",
"preference": "",
"roomStays": [
{
"date": "2015-04-15",
"roomTypeId": "gb_BTEs|",
"roomTypeName": "old age rooms",
"roomTypeCode": "CLSccc",
"roomId": "0",
"roomName": "",
"rateId": "gw||",
"rateName": "Seasonal Rate",
"amount": "500.000000",
"tax": "0.000000",
"discountAmount": "0.000000"
}
],
"guestStays": [
{
"id": "h2ANlxcGFg||",
"mainId": "h2ANlxcGFg||",
"checkInDate": "2015-04-15",
"checkOutDate": "2015-04-16",
"status": "RESERVE",
"isPrimary": true,
"isChargeSharer": true,
"type": "Adult",
"guestDetails": {
"id": "gsgPzf2HQg||",
"code": "P958",
"salutation": null,
"fName": "mina",
"lName": "sharma",
"email": "",
"phoneNo": "545487875454",
"mobileNo": "",
"gender": "Male",
"nationality": null,
"identityTypeId": null,
"identityNo": null,
"isBlackList": false,
"isVip": false,
"dob": "-0001-11-30",
"organization": null,
"designation": null,
"spouseSalutation": null,
"spouseFName": null,
"spouseLName": null,
"spouseDob": "0000-00-00 00:00:00",
"anniversary": "0000-00-00 00:00:00",
"addresses": {
"home": {
"address": "",
"country": null,
"state": null,
"city": "",
"zip": "",
"fax": null
},
"work": {
"address": "",
"country": null,
"state": null,
"city": "",
"zip": "",
"fax": null,
"phone": null,
"mobile": null
}
},
"files": []
}
}
],
"payments": [],
"otherCharges": [],
"addons": [],
"isHoldTill": false,
"releaseDate": "0000-00-00 00:00:00"
}
]
},
{
"xyx": {
"version": "1.0",
"datetime": "2016-12-13T05:27:08",
"response": {
"status": {
"code": 0,
"message": "success"
},
"hotels": [
{
"id": 6209,
"userTypes": [
{
"id": "UXjk0A||",
"title": "manager",
"status": "Active"
},
{
"id": "UJlhsA||",
"title": "test23",
"status": "Active"
},
{
"id": "UJll-Q||",
"title": "march",
"status": "Active"
},
{
"id": "UJlnNA||",
"title": "ajay mishra",
"status": "Active"
},
{
"id": "UJlnMw||",
"title": "prime",
"status": "Active"
},
{
"id": "UJlnPg||",
"title": "rr",
"status": "Active"
},
{
"id": "UJlnPw||",
"title": "xman",
"status": "Active"
},
{
"id": "UJloKg||",
"title": "sdd",
"status": "Active"
},
{
"id": "UJloKQ||",
"title": "tst1",
"status": "Active"
},
{
"id": "UJloLg||",
"title": "test2",
"status": "Active"
},
{
"id": "UJloLw||",
"title": "test3",
"status": "Active"
},
{
"id": "UJlpoA||",
"title": "kk",
"status": "Active"
},
{
"id": "UJlpqQ||",
"title": "shantanu manager",
"status": "Active"
},
{
"id": "UJhGcA||",
"title": "elbo",
"status": "Active"
}
]
}
]
},
"request": {
"method": "getusertypes",
"key": "02w7TK3e-0Ccoyo",
"data": {
"hotels": [
{
"id": 6209
}
]
}
}
}
}
My code:-
String getusertypestitlestring = jsonResult.getJSONObject("xyz").getJSONObject("response").getJSONArray("hotels").getJSONArray(0).getString(1);
:: i want to get userTypes>> title
:: i want to get bookings >> group >> code
Try this:
JSONArray userTypesArray = jsonResult.getJSONObject("xyx").getJSONObject("response").getJSONArray("hotels").getJSONObject(0).getJSONArray("userTypes");
for(int i =0 ; i< userTypesArray.length(); i++){
JSONObject userType = userTypesArray.getJSONObject(i);
System.out.println(userType.get("title"));
}
String getusertypestitlestring = jsonResult.getJSONObject("xyz").getJSONObject("response").getJSONArray("hotels").getJSONArray(0).getString(1);
Here it is not xyz, that is xyx
Make pojo of each Json Object in your Json String.
For Ex:
Make class POJO:
public class POJO {
XYX xyx;
public XYX getXyx() {
return xyx;
}
public void setXyx(XYX xyx) {
this.xyx = xyx;
}
}
Make class XYX:
public class XYX implements Serializable{
String version;
String datetime;
Response response;
Request request;
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getDatetime() {
return datetime;
}
public void setDatetime(String datetime) {
this.datetime = datetime;
}
public Response getResponse() {
return response;
}
public void setResponse(Response response) {
this.response = response;
}
public Request getRequest() {
return request;
}
public void setRequest(Request request) {
this.request = request;
}
}
Make class Response:
class Response {
Status status;
List<Hotels> hotels;
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public List<Hotels> getHotels() {
return hotels;
}
public void setHotels(List<Hotels> hotels) {
this.hotels = hotels;
}
}
Make Class Request :
class Request {
String method;
String key;
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}
and for List
Make class Hotels:
class Hotels {
String id;
List<UserTypes> userTypes;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<UserTypes> getUserTypes() {
return userTypes;
}
public void setUserTypes(List<UserTypes> userTypes) {
this.userTypes = userTypes;
}
}
and make all other pojo classes and use Gson library to set data in your pojos :
POJO pojo = new Gson().fromJson(jsonResponse,POJO.class);
And read Data from pojo :
System.out.println(pojo.getXyx().getDatetime());
System.out.println(pojo.getXyx().getRequest().getKey());
System.out.println(pojo.getXyx().getResponse().getHotels().get(0).getUserTypes().get(0).getTitle());
You can easily use GSON instead of JSON. It's more comfortable to work with the GSON objects. More input is available at:
https://google.github.io/gson/apidocs/com/google/gson/Gson.html

Java jackson cannot deserialise array of objects in JSON

I have a problem with Java Jackson when I try to deserialise.
public class Mapper {
public static Map<String, Object> JsonToMap(String json)
throws JsonParseException, JsonMappingException, IOException {
// Convert the JSON recieved through POST request to a HasMap
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = new HashMap<String, Object>();
map = mapper.readValue(json, new TypeReference<Map<String, Object>>() {
});
return map;
}
Here is the JSON
{[
"UserMealDay": {
"date": "2016-04-04",
"slot_num": "1"
},
"Recipe": {
"id": "82",
"name": "Porridge with Banana & Sunflower Seeds",
"RecipeIngredient": [{
"id": "110263",
"multiplier": null,
"quantity": "50.00",
"ingredient_id": "486",
"ingredient_unit_id": "8",
"recipe_id": "82",
"Ingredient": {
"id": "486",
"name": "jumbo oats",
"plural": "jumbo oats",
"IngredientGroup": [{
"id": "19",
"name": "Oats",
"IngredientGroupsIngredient": {
"id": "160",
"ingredient_group_id": "19",
"ingredient_id": "486",
"created": "0000-00-00 00:00:00",
"modified": "0000-00-00 00:00:00",
"deleted": "0",
"deleted_date": "0000-00-00 00:00:00"
}
}]
},
"IngredientUnit": {
"id": "8",
"short": "g",
"name": "g",
"plural": "g",
"created": "2015-07-14 20:10:33",
"modified": "2015-07-24 09:25:22",
"deleted": "0",
"deleted_date": "0000-00-00 00:00:00"
}
}, {
"id": "110264",
"multiplier": null,
"quantity": "100.00",
"ingredient_id": "182",
"ingredient_unit_id": "18",
"recipe_id": "82",
"Ingredient": {
"id": "182",
"name": "cold water",
"plural": "cold water",
"IngredientGroup": [{
"id": "59",
"name": "Misc",
"IngredientGroupsIngredient": {
"id": "465",
"ingredient_group_id": "59",
"ingredient_id": "182",
"created": "0000-00-00 00:00:00",
"modified": "0000-00-00 00:00:00",
"deleted": "0",
"deleted_date": "0000-00-00 00:00:00"
}
}, {
"id": "121",
"name": "water",
"IngredientGroupsIngredient": {
"id": "1104",
"ingredient_group_id": "121",
"ingredient_id": "182",
"created": "0000-00-00 00:00:00",
"modified": "0000-00-00 00:00:00",
"deleted": "0",
"deleted_date": "0000-00-00 00:00:00"
}
}]
},
"IngredientUnit": {
"id": "18",
"short": "ml",
"name": "ml",
"plural": "ml",
"created": "2015-07-14 20:11:43",
"modified": "2015-07-24 09:42:29",
"deleted": "0",
"deleted_date": "0000-00-00 00:00:00"
}
}, {
"id": "110265",
"multiplier": null,
"quantity": "100.00",
"ingredient_id": "815",
"ingredient_unit_id": "18",
"recipe_id": "82",
"Ingredient": {
"id": "815",
"name": "milk",
"plural": "milk",
"IngredientGroup": [{
"id": "1",
"name": "Dairy",
"IngredientGroupsIngredient": {
"id": "1158",
"ingredient_group_id": "1",
"ingredient_id": "815",
"created": "0000-00-00 00:00:00",
"modified": "0000-00-00 00:00:00",
"deleted": "0",
"deleted_date": "0000-00-00 00:00:00"
}
}, {
"id": "3",
"name": "Cows Milk",
"IngredientGroupsIngredient": {
"id": "1159",
"ingredient_group_id": "3",
"ingredient_id": "815",
"created": "0000-00-00 00:00:00",
"modified": "0000-00-00 00:00:00",
"deleted": "0",
"deleted_date": "0000-00-00 00:00:00"
}
}]
},
"IngredientUnit": {
"id": "18",
"short": "ml",
"name": "ml",
"plural": "ml",
"created": "2015-07-14 20:11:43",
"modified": "2015-07-24 09:42:29",
"deleted": "0",
"deleted_date": "0000-00-00 00:00:00"
}
}, {
"id": "110266",
"multiplier": null,
"quantity": "1.00",
"ingredient_id": "707",
"ingredient_unit_id": "23",
"recipe_id": "82",
"Ingredient": {
"id": "707",
"name": "banana",
"plural": "bananas ",
"IngredientGroup": [{
"id": "45",
"name": "Tropical fruit",
"IngredientGroupsIngredient": {
"id": "340",
"ingredient_group_id": "45",
"ingredient_id": "707",
"created": "0000-00-00 00:00:00",
"modified": "0000-00-00 00:00:00",
"deleted": "0",
"deleted_date": "0000-00-00 00:00:00"
}
}]
},
"IngredientUnit": {
"id": "23",
"short": "qty",
"name": "qty",
"plural": "qty",
"created": "2015-07-14 20:12:19",
"modified": "2015-07-24 09:43:13",
"deleted": "0",
"deleted_date": "0000-00-00 00:00:00"
}
}, {
"id": "110267",
"multiplier": null,
"quantity": "20.00",
"ingredient_id": "280",
"ingredient_unit_id": "8",
"recipe_id": "82",
"Ingredient": {
"id": "280",
"name": "sunflower seeds",
"plural": "sunflower seeds",
"IngredientGroup": [{
"id": "79",
"name": "Seeds",
"IngredientGroupsIngredient": {
"id": "584",
"ingredient_group_id": "79",
"ingredient_id": "280",
"created": "0000-00-00 00:00:00",
"modified": "0000-00-00 00:00:00",
"deleted": "0",
"deleted_date": "0000-00-00 00:00:00"
}
}]
},
"IngredientUnit": {
"id": "8",
"short": "g",
"name": "g",
"plural": "g",
"created": "2015-07-14 20:10:33",
"modified": "2015-07-24 09:25:22",
"deleted": "0",
"deleted_date": "0000-00-00 00:00:00"
}
}, {
"id": "110268",
"multiplier": null,
"quantity": "1.00",
"ingredient_id": "493",
"ingredient_unit_id": "31",
"recipe_id": "82",
"Ingredient": {
"id": "493",
"name": "honey",
"plural": "honey",
"IngredientGroup": [{
"id": "87",
"name": "Non-refined\/ Natural sugar",
"IngredientGroupsIngredient": {
"id": "630",
"ingredient_group_id": "87",
"ingredient_id": "493",
"created": "0000-00-00 00:00:00",
"modified": "0000-00-00 00:00:00",
"deleted": "0",
"deleted_date": "0000-00-00 00:00:00"
}
}, .......
The TypeReferencing can only use only primitive variables, and I must use String[] as my first parameter instead of String.
What should I do?
Thanks!

Categories