json-path / JsonPath

Java JsonPath implementation
Apache License 2.0
8.94k stars 1.65k forks source link

Q: Is there a way to set a new value if match the node name #504

Open GitHubForTroy opened 5 years ago

GitHubForTroy commented 5 years ago

ex : { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }
==> { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-****-3", "price": 8.99 } if i match "isbn" node

hf-kklein commented 5 years ago

Yes, there is. This example replaces all nodes named "isbn" with "0-****-3".

DocumentContext dc = JsonPath.parse("insert your json string here")
dc.put("$..isbn", "0-****-3");

If you'd like to replace only some nodes depending on their value you could use e.g. regular expressions inside the path:

$..[?(@.isbn=~/0-\d{3}-\d{5}-3/)].isbn
akanshSirohi commented 10 months ago

@GitHubForTroy @hf-kklein Can you please check my issue..?? https://github.com/json-path/JsonPath/issues/982#issue-2082624079