tidwall / gjson

Get JSON values quickly - JSON parser for Go
MIT License
14.2k stars 846 forks source link

Flatten/Expand Array #335

Closed adaml881 closed 1 year ago

adaml881 commented 1 year ago
[
  {
    "age":37,
    "friends": [
      {"age": 44, "name": "Dale Murphy"},
      {"age": 68, "name": "Roger Craig"},
      {"age": 47, "name": "Jane Murphy"}
    ],
    "name": "Tom Anderson"
  }
]

Using this data sample is there a way to transform the whole document to a long array, flatten the children objects like this:

[
  {
    "age":37,
    "friendAge": 44,
    "friendName": "Dale Murphy"
    "name": "Tom Anderson"
  },
  {
    "age":37,
    "friendAge": 68,
    "friendName": "Roger Craig"
    "name": "Tom Anderson"
  },
  {
    "age":37,
    "friendAge": 47,
    "friendName": "Jane Murphy"
    "name": "Tom Anderson"
  }
]

This might seem like a strange thing to do, but I'm hoping to use it as a way to denormalize multiple different datasets into the exact same structure to make it easier to search through

volans- commented 1 year ago

@adaml881 I think this is similar to what was asked in #169, see the replies there along with the suggested modifier.

adaml881 commented 1 year ago

Good call! thank you.

Using the fill modifier from #169 this worked.

#.[{myAge:age,myName:name},friends].@fill|@flatten