spyzhov / ajson

Abstract JSON for Golang with JSONPath support
MIT License
246 stars 23 forks source link

Enhanced support for updating json using jsonpath #35

Open karlkfi opened 3 years ago

karlkfi commented 3 years ago

This is an interesitng project! It seems like it's the only jsonpath library that supports mutation and not just retrieval. That said, the mutation API/behavior could be improved.

I'd like to be able to mutate json using jsonpath. This leads to two asks:

  1. I'd also like to be able to pass an interface{} to a Node.Set function and have it handle conversion to the right type, erroring if the type is invalid. I think this just means exporting the internal update method: https://github.com/spyzhov/ajson/blob/master/node_mutations.go#L147
  2. If the node exists, ajson makes mutation easy by supporting Set fields on Node. However, if there's no match, because the node or it's parents don't exist, I'd like to be able to easily auto-generate the node and any parent maps & arrays to match the jsonpath expression.
spyzhov commented 3 years ago

Thanks a lot for your comment, it's really nice to hear.

About your suggestions:

  1. Yes, you're right. It's quite a small and helpful fix, I'll do it in the nearest future.
  2. I thought about it. And you know, at the very beginning I thought that it is not impossible, because you can use something like $..leaf.*.new_field. But if I'd add some limitations, like: "only direct names (or 0 to add the first item in the array) after the leaf element", then it would be enough to generate a new subtree. Like $..leaf[0].new.element["0"] So, I suppose, I can implement it with some limitations. But not sure about the due date.
spyzhov commented 3 years ago

I implemented methods Set and SetNode int the v0.6.0.

I prefer to implement the second part of the request in the v1.0.0 of the library, as soon as it causes backward-incompatible changes. The next I'm going to develop will be exactly v1.0.0.