0

I would want to POST JSON request using Apache HttpClient. But the Json data is little complex that I would want to send to target system. Below is the json that I would send

{
  "name":"xyz",
  "id":"428",
"mailId":
  [
   "mailme@mail.com"
  ],
  "bundle1":
  {
      "opwarden":
      {
         "number":"132344345",
         "title":"title"
      }     
  }
}

What is the best and easiest way to contract above json data in Java?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
ezhil
  • 977
  • 6
  • 15
  • 36

2 Answers2

1

I would recommend using JACKSON object mapper. If you dont want to recreate the above model structure writing pojos.

A nice tutorial can be found here enter link description here

You basically use it like this:

ObjectMapper objectMapper = new ObjectMapper();
String jsonRepresentation = objectMapper.writeValueAsString( anyObject );

Like stated above anyObject could also be a Map key/values and the values can also be maps again.

Your specific use case would be like this:

    ObjectMapper m = new ObjectMapper();

    Map<String, Object> input = new HashMap<String, Object>();
    input.put( "name", "xyz" );
    input.put( "id", "428" );
    input.put( "mailId", new String[] { "mailme@mail.com" } );

    Map<String, Object> opwarden = new HashMap<String, Object>();
    opwarden.put( "number", "132344345" );
    opwarden.put( "title", "title" );

    Map<String, Object> bundle1 = new HashMap<String, Object>();
    bundle1.put( "opwarden", opwarden );

    input.put( "bundle1" , bundle1 );

    String json = m.writeValueAsString( input );
arnonuem
  • 1,317
  • 6
  • 19
  • It talks about simple json data. Can you share any code snippet for complex json data? – ezhil Jun 28 '19 at 10:40
  • Data gets complex as you start nesting it. So by using HashMaps for example its easy to nest objects and you get a complex data structure written as json String. – arnonuem Jun 28 '19 at 10:55
1

With POJOs and ObjectMapper for Jackson:

public class Data {

    private final String name;
    private final String id;
    private final List<String> mailId;
    private final List<Opwarden> bundle1;

    public Data(final String name, final String id, final List<String> mailId, final List<Opwarden>     bundle1) {
        this.name = name;
        this.id = id;
        this.mailId = mailId;
        this.bundle1 = bundle1;
    }

    public String getName() {
        return name;
    }

    public String getId() {
        return id;
    }

    public List<String> getMailId() {
        return mailId;
    }

    public List<Opwarden> getBundle1() {
        return bundle1;
    }
}

and Opwarden:

public class Opwarden {

    private final String number;
    private final String title;

    public Opwarden(final String number, final String title) {
        this.number = number;
        this.title = title;
    }

    public String getNumber() {
        return number;
    }

    public String getTitle() {
        return title;
    }
}

You can create a JSON with:

ObjectMapper objectMapper = new ObjectMapper();
Data data = new Data("xyz", "428", List.of("mailme@mail.com"), List.of(new Opwarden("132344345", "title")));
System.out.println(objectMapper.writeValueAsString(data));

The output:

{
    "name": "xyz",
    "id": "428",
    "mailId": [
        "mailme@mail.com"
    ],
    "bundle1": [
        {
            "number": "132344345",
            "title": "title"
        }
    ]
}
Thibaud Ledent
  • 1,446
  • 1
  • 7
  • 6
  • @Ledent - The method of(Test.Opwarden) is undefined for the type List. Is there any method called "of" – ezhil Jun 28 '19 at 14:05
  • The method `List.of()` is available in Java 9, see https://docs.oracle.com/javase/9/docs/api/java/util/List.html#of-- – Thibaud Ledent Jun 30 '19 at 16:42