qntfy / kazaam

Arbitrary transformations of JSON in Golang
MIT License
283 stars 54 forks source link

Ability to grab values from outside `over` context. #109

Open jmcnevin opened 3 years ago

jmcnevin commented 3 years ago

I'm attempting to transform a document like this:

{
  "first_name": "John",
  "last_name": "Doe",
  "children": [{ "first_name": "Jack" }, { "first_name": "Jane" }]
}

Into something like this:

[
  {
    "first_name": "Jack",
    "last_name": "Doe"
  },
  {
    "first_name": "Jane",
    "last_name": "Doe"
  }
]

This would take the children's first names and combine them with the parent's last name. I'm not sure if this can be done currently without some way of escaping the context created by using over, like this (borrowing from syntax I've seen in JSONata):

{
  "operation": "shift",
  "spec": {
    "first_name": "first_name",
    "last_name": "$$.last_name"
  },
  "over": "children"
}

This would be a nice feature to have, unless some other solution is possible.

JoshuaC215 commented 3 years ago

Hey there, yeah I don't know of a good way to do this currently. Agreed that it would be useful. I am not sure how to do a clean implementation of this but would welcome ideas. I think the relevant code section is here, and at first glance it seems like passing around the whole object context in that loop would be clunky - I think it would need to be a new function parameter in all the transforms, or something like that.