Convert Java pojo to json String

convert java object to json using gson
convert java object to json using jsonobject
convert json to pojo in java using jackson
java object to jsonobject
how to convert json string to custom object in java
convert json file to string java
deserialize string to json java
convert nested json to java object using jackson

I have the following java class

public  class TabularDescriptor extends ReportDescriptor {

    private String generatorClass;
    private String targetClass;
    private String name;
    private String sublabel;
    private String reportName;
    private List<MappingMetadata> mappings = null;
    private List<TabularColumnGroup> columnGroups = null;
    private List<TabularStates> states = null;
     and its getters and settere

I have entity classes for each of those List like MappingMetadata,TabularColumnGroup,TabularStates. I want to get a json data for this pojo classes. What can I do for it.

And what is the use of

    public JSONObject toJSON() {
        JSONObject ret = new JSONObject();
        ret.put("generatorClass", this.generatorClass);
        ret.put("targetClass", this.targetClass);
        return ret;

And is there anyway I can display my json content on browser if yes how can I? Thanks.

Convert Java pojo to json String, You can use ObjectMapper or Gson for Class to JSON conversion and vice-versa . (I would recommend ObjectMapper). Object Mapper. Jackson 2 – Convert Java Object to / from JSONjQuery loop over JSON string – $.each exampleHow to access JSON object in JavaScriptJackson : was expecting double-quote to start field name Java JSON Tutorial […]

There are several libraries that deal with JSON serialization/deserialization in the Java world(for details refer to the documentation):

  1. GSON - Google's library for a Java serialization/deserialization(docs).



        dependencies { implementation ''}



    Java Serialization Snippet:

        TabularDescriptor tabularDescriptor = new TabularDescriptor();
        Gson gson = new Gson();
        String json = gson.toJson(obj);
  2. Jackson - another library for a Java serialization/deserialization(docs), comes completely embedded with all dependencies in Spring Boot - popular Java IOC/DI framework.


    Maven(basic, for annotations and additional features you will need more Jackson dependencies):


    Java Serialization Snippet:

        TabularDescriptor tabularDescriptor = new TabularDescriptor();
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(tabularDescriptor);

    Details worth noting: you have to have all public getters/setters for the serialized object and its object fields for the correct outcome in both cases(in its simplest form).

Reference articles:


Jackson 2 - Convert Java Object to / from JSON, Jackson 2 - Convert Java Object to / from JSON. Staff.class); //JSON string to Java Object Staff obj = mapper. POJO. A simple Java object for testing. Staff. java. public class Staff { private String name; private int age; private� Use objectMapper.writeValueAsString() method to convert pojo to JSON //Simple POJO Employee emp = new Employee(1, "Lokesh Gupta", 34, "India"); //Object mapper instance ObjectMapper mapper = new ObjectMapper(); //Convert POJO to JSON String json = mapper.writeValueAsString(emp); 2.1. Java program to convert Java object to JSON

I would recommend you to add Jackson to your project, it's rather easy to use.


And in Java Code can be used as so:

ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(tabularDescriptor);
TabularDescriptor newTabularDescriptor = objectMapper.readValue(json, TabularDescriptor.class);

How to convert Java object to / from JSON (Jackson), 1.2 Convert JSON to Java object, readValue() POJO (Plain Old Java Object) Read JSON string and convert it back to a Java object. Convert Java Object to Json String using Jackson API Create a POJO (Plain Old Java Object) to be converted into JSON. Java Class. filter_none. edit close. play_arrow.

Jackson API, Here is an example of creating JSON from Java object using the setAddress( address); // Convert object to JSON string String json = mapper. In this tutorial, we will learn how to convert POJO into a JSON & JSON back into POJO using java based Jackson-databind API.The ObjectMapper class provided by Jackson API provides functionality for converting between Java Objects and JSON.

How to Convert a Java Object into a JSON String, Common Uses for Converting Java Obj. to JSON String. The example below demonstrates a client server scenario where the RESTful Web� In this tutorial, we will show you how to use Gson to convert Java object to / from JSON.. P.S All examples are tested by Gson 2.8.5

How to Convert Java Object to JSON, In this tutorial we show how to convert a Java object to JSON using two libraries, We will use a Person class which is just a standard POJO. Map; public class Person { String name; Integer age; List<String> hobbies;� document.toJson() is not giving standard json for long, timestamp etc and gson is complaining while deserialising to POJO. So I need a way to convert org.bson.Document to standard json. NOTE: I want to avoid using mongo java driver or morphia as this work does not relate to mongo in anyway.

  • Have a look at It seems to be exactly what you're looking for. You can easily serialize and deserialize Java Objects using it.
  • Thank you. What is value in tabularDescriptor. Is it the value I set using its setter? If yes how can I set value to pojo classes like MappingMetadata.
  • @Lilac 1) tabularDescriptor is your object e.g. TabularDescriptor tabularDescriptor = new TabularDescriptor(...); 2) Not sure that I get what you mean here. Are you refering to how to create objects? Easiest way is to use a Builder Pattern, especially in your case since you have a lot of inner objects in the tabular descriptor.
  • Thank you. I wasn't sure how to give values for the inner objects.