Home > Back-end >  Simplest way to convert this array in to the specified one using Java
Simplest way to convert this array in to the specified one using Java

Time:11-06

I have this String Json Payload

[
   "key1":{
      "atr1":"key1",
      "atr2":"value1",
      "atr3":"value2",
      "atr4":"value3,
      "atr5":"value4"
   },
   "key2":{
      "atr1":"key2",
      "atr2":"value5",
      "atr3":"value6",
      "atr4":value7,
      "atr5":"value8"
   }
]

and I want it to be converted in to the following format using Java

[
  {
      "atr2":"value1",
      "atr3":"value2",
      "atr4":"value3,
      "atr5":"value4"
   },
  {
      "atr2":"value5",
      "atr3":"value6",
      "atr4": "value7",
      "atr5":"value8"
   }
]

What would be the simplest way of transforming this ?

CodePudding user response:

You cannot, because the example below is not valid json. Check it out using this JSON validator.

If you paste this in (I've fixed some basic errors with lack of quotes)

{
  {
      "atr2":"value1",
      "atr3":"value2",
      "atr4":"value3",
      "atr5":"value4"
   },
  {
      "atr2":"value5",
      "atr3":"value6",
      "atr4":"value7",
      "atr5":"value8"
   }
}

You will get these errors ...

enter image description here

It can work if you change the target schema to something like this by using a json-array to contain your data.

[
  {
      "atr2":"value1",
      "atr3":"value2",
      "atr4":"value3",
      "atr5":"value4"
   },
  {
      "atr2":"value5",
      "atr3":"value6",
      "atr4":"value7",
      "atr5":"value8"
   }
]

If this works for you, then this problem can easily be solved by using the ObjectMapper class.

  1. You use it to deserealize the original JSON into a class, which has two fields "key1" and "key2"
  2. Extract the values of these fields and then just store them in an array ...
  3. Serialize the array using the ObjectMapper.

Here a link, which explains how to use the ObjectMapper class to achieve the goals above.

EDIT:

So you'll need the following classes to solve the problem ...

Stores the object data

class MyClass {
   String atr2;
   String art3;
}

Then you have a container class, which is used to store the initial json.

class MyClassContainer {
  MyClass key1;
  MyClass key2;
}

Here's how you do the parse from the original json to MyClassContainer

var mapper = new ObjectMapper()
var json = //Get the json String somehow
var myClassContainer =  mapper.readValue(json,MyClassContainer.class)
var mc1 = myClassContainer.getKey1();
var mc2 = myClassContainer.getKey2();
var myArray = {key1, key2}
var resultJson = mapper.writeValueAsString(myArray)

CodePudding user response:

Assuming that you will correct the JSON into a valid one (which involves replacing the surrounding square braces with curly ones, and correct enclosure of attribute values within quotes), here's a simpler way which involves only a few lines of core logic.

try{
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false );
    HashMap<String, Data> map = mapper.readValue( jsonString, new TypeReference<HashMap<String, Data>>(){} );
        
    String json = mapper.writeValueAsString( map.values() );
    System.out.println( json );
}
catch( JsonProcessingException e ){
    e.printStackTrace();
}
  • jsonString above is your original JSON corrected and valid JSON input.
  • Also notice the setting of FAIL_ON_UNKNOWN_PROPERTIES to false to allow atr1 to be ignored while deserializing into Data.

Since we are completely throwing away attr1 and its value, the Data class will represent all fields apart from that.

private static class Data{
    private String atr2;
    private String atr3;
    private String atr4;
    private String atr5;
}
  • Related