// https://mvnrepository.com/artifact/com.jayway.jsonpath/json-path
compile group: 'com.jayway.jsonpath', name: 'json-path', version: '2.4.0'
XPath | JSONPath | Description |
---|
/ | $ | the root object/element |
. | @ | the current object/element |
/ | . or [] | child operator |
.. | n/a | parent operator |
// | .. | recursive descent. JSONPath borrows this syntax from E4X. |
* | * | wildcard. All objects/elements regardless their names. |
@ | n/a | attribute access. JSON structures don’t have attributes. |
[] | [] | subscript operator. XPath uses it to iterate over element collections and for predicates. In Javascript and JSON it is the native array operator. |
| | [,] | Union operator in XPath results in a combination of node sets. JSONPath allows alternate names or array indices as a set. |
n/a | [start: end:step] | array slice operator borrowed from ES4. |
[] | ?() | applies a filter (script) expression. |
n/a | () | script expression, using the underlying script engine. |
() | n/a | grouping in Xpath |
XPath | JSONPath | Description |
---|
/store/book/author | $.store.book[*].author | the authors of all books in the store |
//author | $..author | all authors |
/store/* | $.store.* | all things in store, which are some books and a red bicycle. |
/store//price | $.store..price | the price of everything in the store. |
//book[3] | $..book[2] | the third book |
//book[last()] | $…book[(@.length-1)] $…book[-1:] | the last book in order. |
//book[position() < 3] | $…book[0,1] $…book[:2] | the first two books |
//book[isbn] | $..book[?(@.isbn)] | filter all books with isbn number |
//book[price<10] | $…book[?(@.price<10)] | filter all books cheapier than 10 |
//* | $..* | all Elements in XML document. All members of JSON structure. |
使用JsonPath修改Json数据
"apis":[
"ctime":"2018-11-08 16:01:16",
"mtime":"2019-02-20 22:11:42",
"modifierId":1,
"creatorId":1,
"apiId":"1",
"authType":"none",
"version":"v3",
"path": "/test",
"httpMethod":"POST",
"serviceCode":"resource",
"cateCode":"server-task",
"mappingProtocol":"DUBBO",
"modifyVersion":1,
"onlineVersion":1,
"isOnline":"Y",
"isCached":"N",
"cachedTime":10000,
"requestMode":"MAPPING",
"servCreatorId":1134,
"requestMappingParam":{
"requestParams":[
"name":"tenantId",
"type":"Long",
"paramLocation":"Query",
"isRequired":true,
"defaultValue":""
"name":"userId",
"type":"Long",
"paramLocation":"Query",
"isRequired":true,
"defaultValue":""
"name":"taskId",
"type":"Long",
"paramLocation":"Query",
"isRequired":false,
"description":"ID"
"name":"taskName",
"type":"String",
"paramLocation":"Query",
"isRequired":true,
"defaultValue":"",
"description":"名称"
"dubboParamMappings": {
"leafParamMappings": [
"paramName":"tenantId",
"paramType":"java.lang.Long",
"paramMapping":"tenantId"
apis是个列表,列表包含很多map集合。
每个map代表一个api,api中包含requestParams。
其中需要将requestParams中tenantId和userId参数加上headerMapping。
"requestParams":[
"name":"tenantId",
"type":"Long",
"paramLocation":"Query",
"isRequired":true,
"defaultValue":"",
"headerMapping":"X-Access-TenantId"
"name":"userId",
"type":"Long",
"paramLocation":"Query",
"isRequired":true,
"defaultValue":"",
"headerMapping":"X-Access-UserId"
首先,读取json文件:
InputStream inputStream = Files.newInputStream(Paths.get("/Users/admin/Downloads/resource.json"));
构造整个文档
DocumentContext document = JsonPath.parse(inputStream);
在指定位置添加key value
- $.apis[*]是获取所有apis,这里其实只有一个。。。
- $.apis[*].requestMappingParam是获取apis列表中每个map的requestMappingParam
- $.apis[*].requestMappingParam.requestParams是获取上一步requestMappingParam中的requestParams
- [?(@.name==“tenantId”&&[email protected])]这个是过滤条件,指的是requestParams中包含name且等于tenantId并且requestParams没有headerMapping这个key
document.put("$.apis[*].requestMappingParam.requestParams[?(@.name==\"tenantId\"&&[email protected])]", "headerMapping", "X-Access-TenantId");
document.put("$.apis[*].requestMappingParam.requestParams[?(@.name==\"userId\"&&[email protected])]", "headerMapping", "X-Access-UserId");
- 后面两个参数分别是key和value
写入修改后的json
Files.write(Paths.get("/tmp/resource.json")), context.jsonString().getBytes());
没几行代码便完成了
JsonPath的用法今天有个零时需求,需要将很多json数据添加一个key value。当听到这个需求,第一眼想到的是XPath,然鹅XPath是基于xml,就百度一下看看有没有json path。居然还真有个JsonPath。。。突然发现自己很孤陋寡闻。既然有了,那就学一下,发现还挺好使。下面就说说今天的使用心得一、项目地址https://github.com/json-path/J...
以下是利用JSONPath对JSONObject的操作示例代码:
public static void main(String[] args) {
JSONObject json=new JSONObject();
json.put("name","张三");
json.put("sex","女");
json.put("address","浙江杭州西湖转塘");
JSONObject son=new JSONObject();
Getting Started
JsonPath is available at the Central Maven Repository. Maven users add this to your POM.
dependency>
groupId>com.jayway.jsonpathgroupId>
artifactId>json-pathartifactId>