I am using jayway library as I have to use JSONPath Expressions.
{
"fruit":"Apple",
"size":"Large",
"price":"40"
}
Above is my json, now from this JSON suppose I want to read from a specific path. For Ex: - $.fruit So the code snippet will be like Assuming I have read the json file and and it is stored here after converting it to string.
String sb ="input json";
DocumentContext context = JsonPath.parse(sb);
Object rawData = context.read(path);
Considering this will give me String as it is definte path. I will have "Apple" stored in "sb"
Now what if I want to add this String value to a different JSON which will have only this element in it. Using jayway library classes.
{
"fruit":"Apple"
}
- I tried context.set method to add but it will give me all elements.
- Also tried context.remove method but for that I will have to specifically provide the remaining path. (if there is any negate condition like context.remove(!"$.fruit") which will remove every element aprat from fruit), I think I can still achieve but couldn't find that as well.