{
"store": {
"book": [
{ "category": "fiction", "author": "Author A", "title": "Book 1" },
{ "category": "reference", "author": "Author B", "title": "Book 2" },
{ "category": "fiction", "author": "Author C", "title": "Book 3" }
]
}
}
title dari book pertama dalam array, gunakan ekspresi JSONPath ini: $.store.book[0].title$.store.book[0].title:$: Merujuk ke node root dokumen JSON — pada dasarnya seluruh struktur.store: Menunjuk ke properti store, yang merupakan objek di bawah node root.book: Mengakses properti book di bawah objek store, yang merupakan array.[0]: Memilih elemen pertama dalam array book (indeks dimulai dari 0).title: Mengambil title dari buku pertama dalam array.store, mengakses array book, memilih item pertama, dan mengekstrak title dari item tersebut.0.$.store.book[?(@.category=='reference')].| Sintaks | Deskripsi |
|---|---|
$ | Node root |
@ | Node saat ini |
.node atau ['node'] | Mengakses node turunan |
[index] | Pengindeksan array, mendukung penghitungan dari 0 |
[start:end:step] | Pemotongan array |
* | Wildcard, mencocokkan semua node turunan |
.. | Wildcard rekursif, mencocokkan semua turunan |
(<expr>) | Ekspresi dinamis |
?(<boolean expr>) | Kondisi filter |
| Sintaks | Deskripsi |
|---|---|
^ | Mengakses parent dari item yang cocok |
~ | Mendapatkan nama atribut dari item yang cocok (sebagai array) |
@null(), @boolean(), @number(), @string(), @array(), @object() | Mengambil tipe JSON dasar |
@integer() | Mengambil tipe integer |
@scalar() | Mengambil tipe kompleks, menerima undefined dan angka non-terbatas (saat mengueri objek JavaScript) |
@other() | Dapat digunakan dengan otherTypeCallback yang ditentukan pengguna |
@undefined(), @function(), @nonFinite() | Tipe non-JSON yang digunakan saat mengueri objek JavaScript non-JSON |
@path, @parent, @property, @parentProperty, @root | Selektor singkat dalam filter |
` | Meng-escape urutan yang tersisa |
@['...'], ?@['...'] | Meng-escape karakter khusus dalam nama atribut di dalam filter |
$.. | Mengambil semua komponen parent |
{
"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
}
}
}| XPath | JSONPath | Hasil |
|---|---|---|
/store/book/author | $.store.book[*].author | Penulis semua buku |
//author | $..author | Semua penulis |
/store/* | $.store.* | Semua node turunan di bawah store |
/store//price | $.store..price | Semua bidang harga |
//book[3] | $..book[2] | Buku ketiga (indeks berbasis 0) |
//book[last()] | $..book[(@.length-1)] atau $..book[-1:] | Buku terakhir |
//book[position()<3] | $..book[:2] atau $..book[0,1] | Dua buku pertama |
//book[isbn] | $..book[?(@.isbn)] | Buku dengan ISBN |
//book[price<10] | $..book[?(@.price<10)] | Buku dengan harga di bawah 10 |
//* | $..* | Mencocokkan semua node turunan secara rekursif |