Mastering JsonPath: Extract Ordered Objects and Slices from JSON in Java

This tutorial demonstrates how to use JsonPath in Java to retrieve specific items and slices from JSON arrays, covering zero‑based indexing, negative indices for reverse lookup, Python‑style slice syntax, and provides complete code examples with console output.

FunTester
FunTester
FunTester
Mastering JsonPath: Extract Ordered Objects and Slices from JSON in Java

JSON Data

The article starts with a JSON example representing a store that contains a list of books and a bicycle, then shows how to create a JSONObject from the JSON string in Java.

JSONObject json = JSON.parseObject("{\"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}},\"expensive\":10,\"ss\":[32,32,4,23]}");

Getting an ordered object from the array

Using JsonPath $.store.book[2] retrieves the third book (index starts at 0). The expression $.store.book[-1] returns the last book because a negative index counts from the end.

Indexing begins at 0.

Negative indices select elements from the end of the array.

Object read = JsonPath.read(json, "$.store.book[2]");
output(JSON.parseObject(read.toString()));

An equivalent form using JSONObject directly:

JSONObject read = JsonPath.read(json, "$.store.book[2]");
output(read);

The console output shows the selected book object:

{
  "author":"Herman Melville",
  "price":8.99,
  "isbn":"0-553-21311-3",
  "category":"fiction",
  "title":"Moby Dick"
}

Getting a slice of the array

JsonPath slice syntax mirrors Python's array slicing. $.store.book[:2] returns the first two books, while $.store.book[-2:] returns the last two books.

"[:2]" selects elements from the start up to (but not including) index 2.

"[-2:]" selects the last two elements.

Object slice1 = JsonPath.read(json, "$.store.book[:2]");
output(slice1);
Object slice2 = JsonPath.read(json, "$.store.book[-2:]");
output(slice2);

The console output displays the sliced results, each as a JSON object representing the corresponding books.

{
  "author":"Nigel Rees",
  "price":8.95,
  "category":"reference",
  "title":"Sayings of the Century"
}
...
{
  "author":"J. R. R. Tolkien",
  "price":22.99,
  "isbn":"0-395-19395-8",
  "category":"fiction",
  "title":"The Lord of the Rings"
}
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Data Extraction
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.