Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Map to JSON using Jackson

How can I convert a Map to a valid JSON using Jackson?

I am doing it using Google's GSON via a Spring Boot REST Post method...

Here's the RESTful Web Service:

import java.util.Map;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.google.gson.Gson;

@RestController
@RequestMapping("/myservice")
public class ValidationService {    

    @RequestMapping(value="/validate", method = RequestMethod.POST)
    public void validate(@RequestBody Map<String, Object> payload) throws Exception {
        Gson gson = new Gson();
        String json = gson.toJson(payload); 
        System.out.println(json);
    }
}

So, when I invoke it using this:

curl -H "Accept: application/json" -H "Content-type: application/json" \
-X POST -d '{"name":"value"}' http://localhost:8080/myservice/validate

Receive the following to stdout (this is exactly what I want):

{"name":"value"}

Is there a better way to do this using Jackson instead of Google's Gson and / or am I going about it the wrong way altogether?

like image 735
PacificNW_Lover Avatar asked Mar 30 '15 07:03

PacificNW_Lover


People also ask

Can we convert Map to JSON?

We can convert a Map to JSON object using the toJSONString() method(static) of org. json. simple. JSONValue.

How does Jackson convert object to JSON?

Converting Java object to JSON In it, create an object of the POJO class, set required values to it using the setter methods. Instantiate the ObjectMapper class. Invoke the writeValueAsString() method by passing the above created POJO object. Retrieve and print the obtained JSON.

What is use of ObjectMapper in Java?

ObjectMapper provides functionality for reading and writing JSON, either to and from basic POJOs (Plain Old Java Objects), or to and from a general-purpose JSON Tree Model ( JsonNode ), as well as related functionality for performing conversions.


3 Answers

You can convert Map to JSON using Jackson as follows:

Map<String,String> payload = new HashMap<>();
payload.put("key1","value1");
payload.put("key2","value2");

String json = new ObjectMapper().writeValueAsString(payload);
System.out.println(json);
like image 127
Mithun Avatar answered Oct 11 '22 06:10

Mithun


Using jackson, you can do it as follows:

    ObjectMapper mapper = new ObjectMapper();
    String clientFilterJson = "";
    try {
        clientFilterJson = mapper.writeValueAsString(filterSaveModel);
    } catch (IOException e) {
        e.printStackTrace();
    }
like image 29
Uzair Avatar answered Oct 11 '22 05:10

Uzair


You should prefer Object Mapper instead. Here is the link for the same : Object Mapper - Spring MVC way of Obect to JSON

like image 32
Arpit Aggarwal Avatar answered Oct 11 '22 04:10

Arpit Aggarwal