1

I am trying to understand what exactly is it to flatten a Json and Unflatten a Json. Any links or blogs would be helpful.

Also w.r.t the additional question, I am trying to deserialize a flattened json attribute into objectmapper object. The Json I am trying is of following format.

{
  "MyUserID": "45345dfsf4545",
  "attributes": {
    "ArrayAttribute1[0].alertMessage": "You have consumed all of your data allowance",
    "ArrayAttribute1[0].promoName": "MyPromoTest",
    "ArrayAttribute2[0].showmorepromosbutton": "true",
    "ArrayAttribute1[0].promoPrice": "P 149.00",
    "userType": 1,
    "Attribute1": "Jan 28 2016 . 3:09PM",
    "Attribute1": "true",
    "Attribute2.validityColor": "RED",
    "Attribute2.subscriberBal": "P 29.5",
    }
}
Leon
  • 2,926
  • 1
  • 25
  • 34
Tushar Jadhav
  • 15
  • 1
  • 5
  • I think there was some explanation here https://stackoverflow.com/questions/19098797/fastest-way-to-flatten-un-flatten-nested-json-objects – Pradeep Krishna Govindaraju Aug 24 '18 at 12:31
  • @PradeepKrishnaGovindaraju thank you! But is there any link you or website where I could get to read what is the difference between flattening and unflattening, it would more helpful. I went through the link shared but it holds solution to how to flatten and unflatten a json (not something very relevant to my query). I want some solution related to deserialize an already flattened attribute in a json. – Tushar Jadhav Aug 24 '18 at 13:17

2 Answers2

1
  • Flattening means to put the JSON Object into a single-Hierarchy-level like structure also called flat structure i.e there are no child or parent objects, everything is a key-value pair. Nested Objects will be accessible by dot operator. On the contrary Un-flattening means to put the flattened structure in to Mutli-level structure where there exists Parent-child structure.

  • You need to pass a un-flattened structure to Object-mapper to de-serialize it.

Following examples will help in understanding it better

Nested JSON Object

{ "a":
  { "b": 1,
    "c": null,
    "d": [false, true]
  },
  "e": "f",
  "g": 2.3
}

Flattened JSON

{ "a.b": 1,
  "a.c": null,
  "a.d[0]": false,
  "a.d[1]": true,
  "e": "f",
  "g": 2.3
}

Java Map like flattened JSON

  {a.b=1, a.c=null, a.d[0]=false, a.d[1]=true, e=f, g=2.3}

Why flattening is required? If you don't internal structure of JSON Object, it will be quite cumbersome to access all the elements inside hence simple solution is to put everything on the same level and deepness of Objects is depicted by dot operator. So flattening makes it easier to access the internal elements.

Serialization and de-serialization can only happen on un-flattened JSONs.

Rishi
  • 1,646
  • 2
  • 15
  • 34
0

Found this repository(https://github.com/wnameless/json-flattener) to flatten and unflatten a json. Make sure you unflatten it to a String first before feeding it to ObjectMapper. Unfortunately did not find a specification that defined the differences.