[Tips] JSONPath 사용법
JSONPath란?
JSON 포맷의 데이터 구조를 쉽게 처리할 수 있도록 고안된 표현식이다.
JSONPath 사용법
기본 문법 요소
JSON의 데이터는 key-value 쌍으로 이루어진 요소들을 object{}
또는 array[]
로 묶어 놓은 형태이다.
$
루트 노드, JSONPath의 모든 표현식은 여기서 시작@
현재 노드, 아래의 조건부 필터 표현식에서 사용.
하위 노드..
중첩된 전체 하위 요소들[]
배열 인덱스*
모든 요소와 매칭되는 와일드 카드? (boolean expression)
조건부 필터 표현식
객체(object)를 다루는 문법
1
2
3
4
5
6
{
"bicycle": {
"color": "red"
"price": 19.95
}
}
표현식 | 출력 |
---|---|
$.bicycle | [{“color”: “red”, “price”: 19.95}] |
$.bicycle.color | [“red”] |
cf. JSONPath에서 모든 Query의 결과물은 반드시 배열 형태로 반환한다.
배열(array)를 다루는 문법
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"book": [
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
]
}
표현식 | 출력 | |
---|---|---|
$.book[0:1].isbn | [“0-553-21311-3”] | 0부터 0까지, 0만 |
$.book[-1:].title | [“The Lord of the Rings”] | [-1:0] [-1:] 배열의 마지막 원소 |
$.book[*].category | [“fiction”, “fiction”] | 모든 원소들 지정 |
조건부 필터 다루기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
표현식 | 출력 |
---|---|
$..book[?(@.price < 10)].title | [“Sayings of the Century”, “Moby Dick”] |
$.store..[?(@.category == “reference”)].author | [“Nigel Rees”] |
$.store.[?(@.category == “reference”)][title,author] | [“Sayings of the Century”, “Nigel Rees”] |
$.store.[?(@.color == “red”)].price | [19.95] |
This post is licensed under CC BY 4.0 by the author.