Quantcast
Channel: Crunchify
Viewing all articles
Browse latest Browse all 1037

In Java How to Flatten or Unflatten Complex JSON objects into Flat & Map-Like Structure

$
0
0

How to deserialize JSON into flat, Map-like structure?

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>

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 🙂

Good to see Crunchify article featured on Google Search page

Let’s get started

Step-1

  • Create java class CrunchifyJSONFlattenerTutorial.java
  • Hope you have added above json-flattener dependency to your pom.xml file

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/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/"
	}]
}

We will read crunchify.txt JSON file in java.

Step-3

Copy java code to your Eclipse:

package crunchify.com.tutorials;

import java.io.FileReader;
import java.util.Map;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

import com.github.wnameless.json.flattener.JsonFlattener;
import com.github.wnameless.json.unflattener.JsonUnflattener;

/**
 * @author Crunchify.com
 * 
 */

public class CrunchifyJSONFlattenerTutorial {
	public static void main(String[] args) {
		JSONParser parser = new JSONParser();
		try {

			// 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
			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: http://crunchify.me/1VIwm0l
			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/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

=====Unflatten it back to original JSON===== 
{"Address":"New York","Author":"App Shah","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/"}],"Name":"crunchify.com"}

Ask a question in Forum for faster update OR have a suggestion on article? Share it as a comment.

The post In Java How to Flatten or Unflatten Complex JSON objects into Flat & Map-Like Structure appeared first on Crunchify.

Author: App Shah

Crunchify, LLC Logo


Viewing all articles
Browse latest Browse all 1037

Trending Articles