0

If I have a map like this

{"media.video.one"="hello", "media.video.two"="world", "media.audio.one"="hi"}

or 

{"/media/video/one"="hello", "/media/video/two"="world", "/media/audio/one"="hi"}

Is it possible to convert this into JSON using Jackson and ObjectMapper APIs like this?

{
  "media": {
    "video": {
      "one": "hello",
      "two": "hello"
    },
    "auido": {
      "one": "hi"
    }
  }
}

Or how I can do this in code?

I found something similar in JS here: https://stackoverflow.com/a/37438096/6024354

Sunil Kumar
  • 151
  • 1
  • 8
  • What does your map actually look like? Is it a `Map`? If so, why does it have such a flat structure when the json you want seems to require some deeper nesting? And finally, if it is a `Map` I'd suggest you split the keys at the dots and build a bunch of nested maps with the appropriate key part at each level. – Thomas Mar 26 '21 at 05:38
  • The map is of type Map. I'm getting this map from a different source and I basically need to convert it to JSON. The objectmapper.readTree() api works similarly in the other directtion. I was curious to see if this way was possible. – Sunil Kumar Mar 26 '21 at 05:42
  • Well, `readTree()` returns a bunch of nested maps, i.e. every JSON object is represented as a map and since you have 4 objects (the main one, "media", "video" and "audio") you'd get 4 maps. Serializing those maps or a similarly built structure would be supported by Jackson but I'm not sure a flat `Map` would be produced or consumed in the way you want - at least not out-of-the-box. – Thomas Mar 26 '21 at 05:54
  • I found something similar in JS here: https://stackoverflow.com/a/37438096/6024354 Is there an easier way to do this? – Sunil Kumar Mar 26 '21 at 07:13
  • That basically looks like what I was suggesting - and it wouldn't be that complex. Any more generic approach would probably be more complex. – Thomas Mar 26 '21 at 07:23

0 Answers0