BisnodeInformatics/json-flattener

Name: json-flattener

Owner: Bisnode Informatics Deutschland

Description: A Java utility used to FLATTEN nested JSON objects and even more to UNFLATTEN it back

Forked from: wnameless/json-flattener

Created: 2017-03-23 11:04:59.0

Updated: 2017-03-23 11:05:00.0

Pushed: 2017-03-24 14:10:58.0

Homepage:

Size: 155

Language: Java

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Maven Central codecov

json-flattener

A Java utility used to FLATTEN nested JSON objects and even more to UNFLATTEN it back

Purpose

Converts a nested JSON
  { “a”: { “b”: 1, “c”: null, “d”: [false, true] }, “e”: “f”, “g”: 2.3 }
into a flattened JSON
  { “a.b”: 1, “a.c”: null, “a.d[0]“: false, “a.d[1]“: true, “e”: f, “g”: 2.3 }
or a Java Map
  {a.b=1, a.c=null, a.d[0]=false, a.d[1]=true, e=f, g=2.3}

Maven Repo
endency>
<groupId>com.github.wnameless</groupId>
<artifactId>json-flattener</artifactId>
<version>0.3.0</version>
pendency>
Quick Start
ng json = "{ \"a\" : { \"b\" : 1, \"c\": null, \"d\": [false, true] }, \"e\": \"f\", \"g\":2.3 }";
String, Object> flattenJson = JsonFlattener.flattenAsMap(json);

em.out.println(flattenJson);
utput: {a.b=1, a.c=null, a.d[0]=false, a.d[1]=true, e=f, g=2.3}

ng jsonStr = JsonFlattener.flatten(json);
em.out.println(jsonStr);
utput: {"a.b":1,"a.c":null,"a.d[0]":false,"a.d[1]":true,"e":"f","g":2.3}

ng nestedJson = JsonUnflattener.unflatten(jsonStr);
em.out.println(nestedJson);
"a":{"b":1,"c":null,"d":[false,true]},"e":"f","g":2.3}

upport JSON keys which contain dots or square brackets
ng flattendJsonWithDotKey = JsonFlattener.flatten("[{\"a.a.[\":1},2,{\"c\":[3,4]}]");
em.out.println(flattendJsonWithDotKey);
utput: {"[0][\"a.a.[\"]":12,"[1]":2,"[2].c[0]":3,"[2].c[1]":4}

ng nestedJsonWithDotKey = JsonUnflattener.unflatten(
    "{\"[1][0];\":2,\"[0]\":1,\"[1][1]\":3,\"[2]\":4,\"[3][\\\"ab.c.[\\\"]\":5}");
em.out.println(nestedJsonWithDotKey);
utput: [1,[2,3],4,{"ab.c.[":5}]
New Features (since v0.3.0)
LeftAndRightBrackets
sonFlattener - Brackets can be changed from square brackets([]) to any 2 arbitrary characters
ng json = "{\"abc\":{\"def\":[123]}}";
em.out.println(new JsonFlattener(json).withLeftAndRightBrackets('(', ')').flatten());
"abc.def(0)":123}

sonUnflattener - if special brackets are using, it should be set into the unflattener as well
 = "{"abc.def(0)":123}";
em.out.println(new JsonUnflattener(json).withLeftAndRightBrackets('(', ')').unflatten());
"abc":{"def":[123]}}
Reader
tStream inputStream = new FileInputStream("simple.json");
er reader = new InputStreamReader(inputStream);

upport Reader as input 
Flattener jf = new JsonFlattener(reader);
Unflattener ju = new JsonUnflattener(reader);
New Features (since v0.2.0)
FlattenMode
ng json = "{\"abc\":{\"def\":[1,2,{\"g\":{\"h\":[3]}}]}}";

lattenMode.NORMAL(default) - flatten everything
em.out.println(new JsonFlattener(json).withFlattenMode(FlattenMode.NORMAL).flatten());
"abc.def[0]":1,"abc.def[1]":2,"abc.def[2].g.h[0]":3}

lattenMode.KEEP_ARRAYS - flatten all except arrays
em.out.println(new JsonFlattener(json).withFlattenMode(FlattenMode.KEEP_ARRAYS).flatten());
"abc.def":[1,2,{"g.h":[3]}]}

hen the flattened outcome can NOT suit in a Java Map, it will still be put in the Map with "root" as its key. 
String, Object> map = new JsonFlattener("[[123]]").withFlattenMode(FlattenMode.KEEP_ARRAYS).flattenAsMap();
em.out.println(map.get("root"));
[123]]
StringEscapePolicy
ng json = "{\"abc\":{\"def\":\"??\\t\"}}";

tringEscapePolicy.NORMAL(default) - escape only speacial characters
em.out.println(new JsonFlattener(json).withStringEscapePolicy(StringEscapePolicy.NORMAL).flatten());
"abc.def":"??\t"}

tringEscapePolicy.ALL_UNICODES - escape speacial characters and unicodes
em.out.println(new JsonFlattener(json).withStringEscapePolicy(StringEscapePolicy.ALL_UNICODES).flatten());
"abc.def":"\u592A\u6975\t"}
Separator
sonFlattener - separator can be changed from dot(.) to an arbitrary character
ng json = "{\"abc\":{\"def\":123}}";
em.out.println(new JsonFlattener(json).withSeparator('*').flatten());
"abc*def":123}

sonUnflattener - if a special separator is using, it should be set into the unflattener as well
 = "{\"abc*def\":123}";
em.out.println(new JsonUnflattener(json).withSeparator('*').unflatten());
"abc":{"def":123}}
PrintMode
ng json = "{\"abc\":{\"def\":123}}";

sonFlattener
rintMode.MINIMAL(default)
em.out.println(new JsonFlattener(json).withPrintMode(PrintMode.MINIMAL).flatten());
"abc.def":123}

rintMode.REGULAR
em.out.println(new JsonFlattener(json).withPrintMode(PrintMode.REGULAR).flatten());
 "abc.def": 123 }

rintMode.PRETTY
em.out.println(new JsonFlattener(json).withPrintMode(PrintMode.PRETTY).flatten());

 "abc.def": 123


sonUnflattener
rintMode.MINIMAL(default)
 = "{\"abc.def\":123}";
em.out.println(new JsonUnflattener(json).withPrintMode(PrintMode.MINIMAL).unflatten());
"abc":{"def":123}}

rintMode.REGULAR
em.out.println(new JsonUnflattener(json).withPrintMode(PrintMode.REGULAR).unflatten());
"abc": {"def": 123}}

rintMode.PRETTY
em.out.println(new JsonUnflattener(json).withPrintMode(PrintMode.PRETTY).unflatten());

 "abc": {
   "def": 123
 }


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.