carlosingles / json-patch-query Goto Github PK
View Code? Open in Web Editor NEWImplementation of JSON Patch Query as proposed by the TM Forum
Implementation of JSON Patch Query as proposed by the TM Forum
I have used JSONPatchQuery to remove multiple nodes at once.
Input JSON:
{
"swagger": "2.0",
"tags": [{
"name": "catalog"
},
{
"name": "category"
},
{
"name": "productOffering"
},
{
"name": "productOfferingPrice"
},
{
"name": "productSpecification"
},
{
"name": "importJob"
},
{
"name": "exportJob"
},
{
"name": "notification listeners (client side)"
},
{
"name": "events subscription"
}
]
}
Operations:
[
{
"op": "remove",
"path": "$.tags[?(@.name!='catalog' && @.name!='productOffering' && @.name!='productSpecification')]"
}
]
My expected result is:
{
"swagger": "2.0",
"tags": [{
"name": "catalog"
},
{
"name": "productOffering"
},
{
"name": "productSpecification"
}
]
}
However, I don't get this result because there is a bug in the apply function.
Most likely the problem is that you are modifying the same document object, that is being checked for the different paths. This is particularly problematic for the remove operation.
A potential fix would be to create a clone of the document object, make the modifications there and then return that object.
Hi @carlosingles,
I was happy to find your repository on the TMForum JSON Patch Query + JSON Path. Congratulations on the work done.
I'm one of the authors/contributors to the TMForum Design Guidelines.
I was intriqued by the edge case you highlighted, good catch.
I was wondering if the following solution will work.
As far the limitation is focused on the JSON Path inability to go up the tree, I'm thinking that maybe it can be addressed by a logical operator condition in the json patch query it self.
Something along the way:
filter=orderItem[?(@.productOffering.id=="1513")]&filter=orderItem[*].product.relatedParty[?(@.id==7894)]
(note the & acting as a logical AND between the two conditions)
Now, filter
is a selector defined in TMF DG Part 6 - for JSON Path and i've omitted the "$"
However, if we allow this, the JSON Patch Query, it should look like this
{
op: 'replace',
path: '/orderItem/quantity?filter=$.orderItem[?(@.productOffering.id=="1513")]&filter=$.orderItem[*].product.relatedParty[?(@.id==7894)]',
value: '3',
}
Which should be reflected in: "update the quantity where the filter condition A and filter condition B are sadisfied".
As per JSON Patch Query:
Operation in a JSON Patch Query document are handled in the same way as
for a JSON Patch document but the query parameter in the βpathβ member
is used to identify the element within an array that is impacted by the
operation
So, the "query" part will come after ?
where the condition of the query is specified.
I do believe this might require a small update in the TMF DG Part 5, to allow for the "filter" selector as right now it is implicitly passed in the path value.
What do you think ?
And again congrats on the great job on this repo.
Thanks,
Florin
Test patch operation hasn't seem to be supported
Steps to reproduce:
import patch from 'json-patch-query'
const item = { id: 342 }
console.log(patch.apply(item, [{ op: 'test', path: '$.id', value: 2 }]))
Expected behavior:
Error, since id with such value is missing
Actual behavior:
Ignored operation
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.