简体   繁体   中英

How to unflatten a JSON object?

How to convert the flattened JSON like following

{
  "foo.bar1": "1",
  "foo.bar2": "2",
  "foo.bar3.doo1": "3",
  "foo.bar3.doo2" : "4"
}

to be nested like

{
  "foo": {
    "bar1": "1",
    "bar2": "2",
    "bar3": {
      "doo1": "3",
      "doo2": "4"
    }
  }
}

? I could find many posts about flattening a nested JSON, but not vice versa.

Convert keys to paths, and put the values in their original places using setpath .

. as $in | reduce keys_unsorted[] as $k ({}; setpath($k / "."; $in[$k]))

Online demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM