Convert JSON to List of map java

It is impossible explicitly, however, you can deserialize you String into List< Object> and then cast your Object in for-loop to Map< String, Object>:

String jsonString = '[{"id":2,"name":"Abc_SS","description":"Abc"},{"id":100,"name":"sales","description":"sales"}]'; List items = (List) JSON.deserializeUntyped(jsonString); for (Object itemObj : items) { Map item = (Map) itemObj; //now you can work with it like you always work with Map }

In this article, we'll convert a JSON array into a Java Array and Java List using Jackson.

Since we're using Jackson, you'll have to add it to your project. If you're using Maven, it's as easy as adding the dependency:

<dependency> <groupId>com.fasterxml.jackson.coregroupId> <artifactId>jackson-databindartifactId> <version>2.11.2version> dependency>

Or, if you're using Gradle:

compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.11.2'

Since we're mapping from JSON to our own objects, let's go ahead and define a POJO:

public class Language { private String name; private String description;

Reading JSON from a String

Let's start out with reading JSON from a String. The String contains an array of programming languages, with brief descriptions:

String json = "[{\"name\": \"Java\", \"description\": \"Java is a class-based, object-oriented programming language.\"},{\"name\": \"Python\", \"description\": \"Python is an interpreted, high-level and general-purpose programming language.\"}, {\"name\": \"JS\", \"description\": \"JS is a programming language that conforms to the ECMAScript specification.\"}]";

Using Jackson's ObjectMapper class, it's easy to read values and map them to an object, or an array of objects. We just use the readValue() method, passing the JSON contents and the class we'd like to map to. Since we're mapping to an array of Language, we'll also specify this in the readValue() method:

final ObjectMapper objectMapper = new ObjectMapper(); Language[] langs = objectMapper.readValue(json, Language[].class);

Alternatively, you can extract the values directly into a list, using Jackson's TypeReference:

List langList = objectMapper.readValue(json, new TypeReference>(){});

Without using TypeReference<>, which is advised, you can convert the array into a list with any other approach at your disposal, such as:

List langList = new ArrayList(Arrays.asList(langs));

And then print out the values:

langList.forEach(x -> System.out.println(x.toString()));

This results in:

Language{name='Java', description='Java is a class-based, object-oriented programming language.'} Language{name='Python', description='Python is an interpreted, high-level and general-purpose programming language.'} Language{name='JS', description='JS is a programming language that conforms to the ECMAScript specification.'}

Reading JSON from a File

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

We don't always deal with JSON in String format. Oftentimes, the contents come from a File. Thankfully, Jackson makes this task as easy as the last one, we just provide the File to the readValue() method:

final ObjectMapper objectMapper = new ObjectMapper(); List langList = objectMapper.readValue( new File("langs.json"), new TypeReference>(){}); langList.forEach(x -> System.out.println(x.toString()));

The langs.json file contains:

[ { "name": "Java", "description": "Java is a class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible." }, { "name": "Python", "description": "Python is an interpreted, high-level and general-purpose programming language. Created by Guido van Rossum and first released in 1991." }, { "name": "JS", "description": "JS is a programming language that conforms to the ECMAScript specification. JavaScript is high-level, often just-in-time compiled, and multi-paradigm." } ]

Running this code results in:

Language{name='Java', description='Java is a class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible.'} Language{name='Python', description='Python is an interpreted, high-level and general-purpose programming language. Created by Guido van Rossum and first released in 1991.'} Language{name='JS', description='JS is a programming language that conforms to the ECMAScript specification. JavaScript is high-level, often just-in-time compiled, and multi-paradigm.'}

Conclusion

In this article, we've used Jackson to parse and map the values from a JSON String and file into a Java array and list.

This is done via the readValue() method, by specifying the JSON contents (String or file) and by specifying the POJO we'd like to map to.

private List> map_string;

Json you need to pass:-

{ "map_formula" : [ { "A+" : "if(price<400),\"40000\",0", "B" : "", "c" : "", "d" : "", "e" : "" }, { "poor" : "value for poor", "good" : "300", "average" : "300", "excellent" : "300" } ] }