ajson
ajson copied to clipboard
Enhanced support for updating json using jsonpath
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:
- 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
- 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.
Thanks a lot for your comment, it's really nice to hear.
About your suggestions:
- Yes, you're right. It's quite a small and helpful fix, I'll do it in the nearest future.
- 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 (or0
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.
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
.