Convert JSON to HashMap and HashMap to JSON

After reading the previous article on ‘Introduction to JSON and Javascript Objects‘, you might now be familiar with JSON. We might often be dealing with JSON in our projects, where we want some data to be returned from Java as an AJAX response. The data can be an HTML fragment/template or simply a JSON.

Here I have two scenarios to deal with right now. First,

  1. If I want to convert a Map to JSON – Such as in AJAX calls
  2. If I want to convert a JSON to a MapIf any third party API returns JSON to my Java method

1. Converting Map to JSON

or Using Spring Annotation @ResponseBody

2. Converting JSON to Map

Above are the overloaded methods where you can pass either the JSONObject or a JSON string.


Parsing the JSON response in Javascript

Here is how you can parse the JSON response in Javascript:

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.