0

I have the following JSON, generated in the Android application:

{
  "Details": {
    "ClaimDate": "08/10/2019",
    "HFCode": "55555"
  },
  "Items": [
    {
      "Item": {
        "ItemCode": "Y203",
        "ItemPrice": "20",
        "ItemQuantity": "1"
      }
    }
  ],
  "Services": [
    {
      "Service": {
        "ServiceCode": "X105",
        "ServicePrice": "200",
        "ServiceQuantity": "1"
      }
    }
  ]
}

On the server side, I need this structure

{
  "details": {
    "ClaimDate": "08/10/2019",
    "HFCode": "55555"
  },
  "items": [
    {
      "itemCode": "Y200",
      "itemPrice": 0,
      "itemQuantity": 0
    }
  ],
  "services": [
    {
      "serviceCode": "X100",
      "servicePrice": 0,
      "serviceQuantity": 0
    }
  ]
}

Is there a way to customize this on the Android application side?

I try to do it manually, but I can't get a satisfactory result

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
startNet
  • 274
  • 1
  • 10
  • Could explain a bit about what you are working with here? If you want to do this on the application side you will need to modify the code that generates this JSON to use integers (or floats) rather than Strings. Could you post the code that is creating this JSON? – StealthSpoder Oct 08 '19 at 16:12
  • Do you want to deserialise `JSON` payload to `POJO` model or you want to just transform it to another form and do not deserialise? If you want to deserialise and your `POJO` model does not fit take a look at these questions: [Deserialize inner list of objects to list of one higher level](https://stackoverflow.com/questions/54424576/jackson-deserialize-inner-list-of-objects-to-list-of-one-higher-level), [How do I unwrap a list of list wrapped items in Jackson?](https://stackoverflow.com/questions/57270327/how-do-i-unwrap-a-list-of-list-wrapped-items-in-jackson). – Michał Ziober Oct 08 '19 at 21:24

2 Answers2

0

You can use a transformer function which will take the first json/object as input and returns the second json/object as output. Unfortunately, since your keys and data types are different, standard libraries will not able to do this. If you want to use Jackson or Gson, you will have to play with Custom (De) Serializers.

aksappy
  • 3,400
  • 3
  • 23
  • 49
0

If you are using Jackson (One of the most popular JSON libraries) and you just want to transform the given JSON string into another one, then you can achieve this by following way:

ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(jsonStr);

ObjectNode rootNew = mapper.createObjectNode();
rootNew.put("details", root.get("Details"));

JsonNode itemNode = root.get("Items").get(0).get("Item");
ObjectNode itemsNodeNew = mapper.createObjectNode();
itemsNodeNew.put("itemCode", itemNode.get("ItemCode"));
itemsNodeNew.put("itemPrice", itemNode.get("ItemPrice"));
itemsNodeNew.put("itemQuantity", itemNode.get("ItemQuantity"));
rootNew.put("items", mapper.createArrayNode().add(itemsNodeNew));

JsonNode serviceNode = root.get("Services").get(0).get("Service");
ObjectNode serviceNodeNew = mapper.createObjectNode();
serviceNodeNew.put("serviceCode", serviceNode.get("ServiceCode"));
serviceNodeNew.put("servicePrice", serviceNode.get("ServicePrice"));
serviceNodeNew.put("serviceQuantity", serviceNode.get("ServiceQuantity"));
rootNew.put("services", mapper.createArrayNode().add(serviceNodeNew));

System.out.println(rootNew.toString());

But if you want to convert the JSON string to POJO for further manipulation, you can directly deserialize and serialize it.

LHCHIN
  • 3,679
  • 2
  • 16
  • 34