
How to deserialize nested JSON into flat, Map-like structure?
Couple of days back I got a questions on how to flatten JSON Object which may be simple of Complex in structure?
JsonFlattener is a very powerful maven utility exactly for the same. Let’s take a look at example.
You need to import below Maven Dependency to your project. Add below to your project’s pom.xml file.
<dependency>
<groupId>com.github.wnameless</groupId>
<artifactId>json-flattener</artifactId>
<version>0.2.2</version>
</dependency>
<dependency>
<groupId>com.github.wnameless.json</groupId>
<artifactId>json-flattener</artifactId>
<version>0.12.0</version>
</dependency>
If you don’t see pom.xml file into your Eclipse workspace as you don’t have maven project then you could simply covert your project maven project.
On
side note: It’s good to see Crunchify article featured in Google Search Result page 🙂

Let’s get started
Step-1
- Create java class
CrunchifyJSONFlattenerTutorial.java - Hope you have added above
json-flattenerdependency to yourpom.xmlfile
Step-2
Create file crunchify.txt and put it under ~/Document folder if you are on Macbook. Modify path as per your need.
{
"Name": "crunchify.com",
"Author": "App Shah",
"Address": "New York",
"Company Services": [{
"Service": "Site SEO Review",
"Link": "https://crunchify.com/wordpress-consulting/#seo"
}, {
"Service": "Full Website Design Service",
"Link": "https://crunchify.com/wordpress-consulting/#full-design"
}, {
"Service": "WordPress Optimization & Consultation",
"Link": "https://crunchify.com/wordpress-consulting/#consultant"
}]
}
We will read crunchify.txt JSON file in java.
Step-3
Copy java code to your Eclipse:
package crunchify.com.tutorials;
import com.github.wnameless.json.flattener.JsonFlattener;
import com.github.wnameless.json.unflattener.JsonUnflattener;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.io.FileReader;
import java.util.Map;
/**
* @author Crunchify.com
* How to Flatten or Unflatten Complex JSON objects into Flat & Map-Like Structure in Java?
*
*/
public class CrunchifyJSONFlattenerTutorial {
public static void main(String[] args) {
JSONParser parser = new JSONParser();
try {
/* crunchify.txt file content
{
"Name": "crunchify.com",
"Author": "App Shah",
"Address": "New York",
"Company Services": [{
"Service": "Site SEO Review",
"Link": "https://crunchify.com/services/site-seo-review-service/"
}, {
"Service": "Full Website Design Service",
"Link": "https://crunchify.com/services/full-website-design-service/"
}, {
"Service": "WordPress Optimization & Consultation",
"Link": "https://crunchify.com/services/wordpress-optimization-service/"
}]
}
*/
// Put above JSON content to crunchify.txt file and change path location
Object obj = parser.parse(new FileReader("/Users/appshah/Documents/crunchify.txt"));
JSONObject jsonObject = (JSONObject) obj;
// JsonFlattener: A Java utility used to FLATTEN nested JSON objects
// The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.
String flattenedJson = JsonFlattener.flatten(jsonObject.toString());
log("\n========== Simple Flatten ========== \n" + flattenedJson);
Map<String, Object> flattenedJsonMap = JsonFlattener.flattenAsMap(jsonObject.toString());
log("\n=====Flatten As Map=====\n" + flattenedJson);
// We are using Java8 forEach loop. More info: https://crunchify.com/in-java-8-how-to-iterate-through-java-util-map-and-java-util-list-example-attached-with-total-5-different-ways/
flattenedJsonMap.forEach((k, v) -> log(k + " : " + v));
// Unflatten it back to original JSON
String nestedJson = JsonUnflattener.unflatten(flattenedJson);
System.out.println("\n===== Unflatten it back to original JSON ===== \n" + nestedJson);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void log(String flattenedJson) {
System.out.println(flattenedJson);
}
}
And that’s it. Just run program and you will see Flatten and Unflatten format of your JSONObject.
Eclipse Console output:
=====Simple Flatten=====
{"Address":"New York","Author":"App Shah","Company Services[0].Service":"Site SEO Review","Company Services[0].Link":"https:\/\/crunchify.com\/services\/site-seo-review-service\/","Company Services[1].Service":"Full Website Design Service","Company Services[1].Link":"https:\/\/crunchify.com\/services\/full-website-design-service\/","Company Services[2].Service":"WordPress Optimization & Consultation","Company Services[2].Link":"https:\/\/crunchify.com\/services\/wordpress-optimization-service\/","Name":"crunchify.com"}
=====Flatten As Map=====
{"Address":"New York","Author":"App Shah","Company Services[0].Service":"Site SEO Review","Company Services[0].Link":"https:\/\/crunchify.com\/services\/site-seo-review-service\/","Company Services[1].Service":"Full Website Design Service","Company Services[1].Link":"https:\/\/crunchify.com\/services\/full-website-design-service\/","Company Services[2].Service":"WordPress Optimization & Consultation","Company Services[2].Link":"https:\/\/crunchify.com\/services\/wordpress-optimization-service\/","Name":"crunchify.com"}
Address : New York
Author : App Shah
Company Services[0].Service : Site SEO Review
Company Services[0].Link : https://crunchify.com/wordpress-consulting/#seo
Company Services[1].Service : Full Website Design Service
Company Services[1].Link : https://crunchify.com/wordpress-consulting/#full-design
Company Services[2].Service : WordPress Optimization & Consultation
Company Services[2].Link : https://crunchify.com/wordpress-consulting/#consultant
Name : crunchify.com
=====Unflatten it back to original JSON=====
{
"Name": "crunchify.com",
"Author": "App Shah",
"Address": "New York",
"Company Services": [{
"Service": "Site SEO Review",
"Link": "https://crunchify.com/services/site-seo-review-service/"
}, {
"Service": "Full Website Design Service",
"Link": "https://crunchify.com/services/full-website-design-service/"
}, {
"Service": "WordPress Optimization & Consultation",
"Link": "https://crunchify.com/services/wordpress-optimization-service/"
}]
}
