-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Creation of deep nodes #83
Comments
Also $.array[10] could create an array with anough size to assign the 10th item |
Is there any more thought to bringing this functionality to JsonPath? |
Yes this would be nice to have in the API. I have done this for my own. I have wrapped the API and in my class there is a method getNode(node, jsonpath) which creates all missing nodes. But I have done the easy way and support only the dot-notation and not the bracket-notation. |
@jlolling I took a look through your forked repo and couldn't find the class you were referring to. Did you push that class up to a public repo somewhere? I am doing something similar and was hoping for inspiration on how to handle arrays. |
I made it in my own application. I should get an idea where to put that in the existing Jayways API. If you have an idea where we can integrate this in the Jayway API - that would be great. |
I see that now, look like you are using the Jackson providers. I started down the path of using the default jsonsmart providers instead. I am wrapping the set, add, and put methods. Possibly wrap them all together since I am looking for a generic way to add anything to the document, without explicitly knowing what the path is. Also only supporting dot notation. |
I guess it should be possible to build such functionality for all json providers. But for my project goal I needed that very quick - so I have done this only for my own. |
Functional workaround for limited syntax with limited performance. I do hope this library comes back to life or something yaml-based supercedes it. /**
* Sets a value, creating any missing parents
* @param context
* @param path supports only "definite" paths in the simple format {@code $.a.b.c}.
* @param value value to set
*/
private void create(DocumentContext context, String path, Object value) {
int pos = path.lastIndexOf('.');
String parent = path.substring(0, pos);
String child = path.substring(pos + 1);
try {
context.read(parent); // EX if parent missing
} catch (PathNotFoundException e) {
create(context, parent, new LinkedHashMap<>()); // (recursively) Create missing parent
}
context.put(parent, child, value);
} |
Any update on this feature? |
Thanks for the workaround. I just found out about JsonPath, and I wish I knew about it before. We had before a requirement from a client to convert a list of full path key-value pairs to a JSON object (the scenario is more complicated related to a dynamic input form, but this is the end result for brevity and to stay on point),
to
We also had to take into consideration arrays for that case. |
Another potential (limited, map-only, no arrays) workaround is to use a custom JsonProvider which returns an empty map from the method Something similar to this.
Unsure how this will behave when used for reading json so make sure this is used only for update operations and use the default jsonProvider for read operations. |
A little lame extension for one by one array elements adding:
|
Any update on this feature? |
A related feature is available (through |
It would be useful if JsonPath had the capability to create "deep nodes" rather than just nodes of already existing parents.
For example, provide a method that, given a path e.g. $.x.y.z, a value 10, and a DocumentContext, create the node z with a value 10 allowing for the possibility that x, y do not yet exist. The logic would be
It's possible to do this with the existing APIs using an iterative loop to work down the path, DocumentContext.read to test for existence and DocumentContext.put to create, but would be really cool if the API provided such a capability generally.
The text was updated successfully, but these errors were encountered: