import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Iterator;
import java.util.List;
import com.jayway.jsonpath.JsonPath;
public class TestJsonPath {
public static void main(String[] args) {
String sjson = readtxt();
print("--------------------------------------getJsonValue--------------------------------------");
getJsonValue(sjson);
private static String readtxt() {
StringBuilder sb = new StringBuilder();
try {
FileReader fr = new FileReader("D:/books.txt");
BufferedReader bfd = new BufferedReader(fr);
String s = "";
while((s=bfd.readLine())!=null) {
sb.append(s);
} catch (Exception e) {
e.printStackTrace();
System.out.println(sb.toString());
return sb.toString();
private static void getJsonValue(String json) {
List<String> authors1 = JsonPath.read(json, "$.store.book[*].author");
List<String> authors2 = JsonPath.read(json, "$..author");
List<Object> authors3 = JsonPath.read(json, "$.store.*");
List<Object> authors4 = JsonPath.read(json, "$.store..price");
List<Object> authors5 = JsonPath.read(json, "$..book[2]");
List<Object> authors6 = JsonPath.read(json, "$..book[0,1]");
List<Object> authors7 = JsonPath.read(json, "$..book[:2]");
List<Object> authors8 = JsonPath.read(json, "$..book[1:2]");
List<Object> authors9 = JsonPath.read(json, "$..book[-2:]");
List<Object> authors10 = JsonPath.read(json, "$..book[2:]");
List<Object> authors11 = JsonPath.read(json, "$..book[?(@.isbn)]");
List<Object> authors12 = JsonPath.read(json, "$.store.book[?(@.price < 10)]");
List<Object> authors13 = JsonPath.read(json, "$..book[?(@.price <= $['expensive'])]");
List<Object> authors14 = JsonPath.read(json, "$..book[?(@.author =~ /.*REES/i)]");
List<Object> authors15 = JsonPath.read(json, "$..*");
List<Object> authors16 = JsonPath.read(json, "$..book.length()");
print("**********authors1**********");
print(authors1);
print("**********authors2**********");
print(authors2);
print("**********authors3**********");
printOb(authors3);
print("**********authors4**********");
printOb(authors4);
print("**********authors5**********");
printOb(authors5);
print("**********authors6**********");
printOb(authors6);
print("**********authors7**********");
printOb(authors7);
print("**********authors8**********");
printOb(authors8);
print("**********authors9**********");
printOb(authors9);
print("**********authors10**********");
printOb(authors10);
print("**********authors11**********");
printOb(authors11);
print("**********authors12**********");
printOb(authors12);
print("**********authors13**********");
printOb(authors13);
print("**********authors14**********");
printOb(authors14);
print("**********authors15**********");
printOb(authors15);
print("**********authors16**********");
printOb(authors16);
private static void print(List<String> list) {
for(Iterator<String> it = list.iterator();it.hasNext();) {
System.out.println(it.next());
private static void printOb(List<Object> list) {
for(Iterator<Object> it = list.iterator();it.hasNext();) {
System.out.println(it.next());
private static void print(String s) {
System.out.println("\n"+s);
{"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":"JavaWeb","author":"X-rapido","title":"Top-link","isbn":"0-553-211231-3","price":32.68},{"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}
--------------------------------------getJsonValue--------------------------------------
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/workSpaces/SupportPackge/MavenRepository/org/apache/logging/log4j/log4j-slf4j-impl/2.0.2/log4j-slf4j-impl-2.0.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/workSpaces/SupportPackge/MavenRepository/org/slf4j/slf4j-log4j12/1.7.10/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http:
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
**********authors1**********
Nigel Rees
Evelyn Waugh
Herman Melville
X-rapido
J. R. R. Tolkien
**********authors2**********
Nigel Rees
Evelyn Waugh
Herman Melville
X-rapido
J. R. R. Tolkien
**********authors3**********
[{"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":"JavaWeb","author":"X-rapido","title":"Top-link","isbn":"0-553-211231-3","price":32.68},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}]
{color=red, price=19.95}
**********authors4**********
12.99
32.68
22.99
19.95
**********authors5**********
{category=fiction, author=Herman Melville, title=Moby Dick, isbn=0-553-21311-3, price=8.99}
**********authors6**********
{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}
**********authors7**********
{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}
**********authors8**********
{category=fiction, author=Evelyn Waugh, title=Sword of Honour, price=12.99}
**********authors9**********
{category=JavaWeb, author=X-rapido, title=Top-link, isbn=0-553-211231-3, price=32.68}
{category=fiction, author=J. R. R. Tolkien, title=The Lord of the Rings, isbn=0-395-19395-8, price=22.99}
**********authors10**********
{category=fiction, author=Herman Melville, title=Moby Dick, isbn=0-553-21311-3, price=8.99}
{category=JavaWeb, author=X-rapido, title=Top-link, isbn=0-553-211231-3, price=32.68}
{category=fiction, author=J. R. R. Tolkien, title=The Lord of the Rings, isbn=0-395-19395-8, price=22.99}
**********authors11**********
{category=fiction, author=Herman Melville, title=Moby Dick, isbn=0-553-21311-3, price=8.99}
{category=JavaWeb, author=X-rapido, title=Top-link, isbn=0-553-211231-3, price=32.68}
{category=fiction, author=J. R. R. Tolkien, title=The Lord of the Rings, isbn=0-395-19395-8, price=22.99}
**********authors12**********
{category=reference, author=Nigel Rees, title=Sayings of the Century, price=8.95}
{category=fiction, author=Herman Melville, title=Moby Dick, isbn=0-553-21311-3, price=8.99}
**********authors13**********
{category=reference, author=Nigel Rees, title=Sayings of the Century, price=8.95}
{category=fiction, author=Herman Melville, title=Moby Dick, isbn=0-553-21311-3, price=8.99}
**********authors14**********
{category=reference, author=Nigel Rees, title=Sayings of the Century, price=8.95}
**********authors15**********
{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":"JavaWeb","author":"X-rapido","title":"Top-link","isbn":"0-553-211231-3","price":32.68},{"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}}
[{"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":"JavaWeb","author":"X-rapido","title":"Top-link","isbn":"0-553-211231-3","price":32.68},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}]
{color=red, price=19.95}
{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=JavaWeb, author=X-rapido, title=Top-link, isbn=0-553-211231-3, price=32.68}
{category=fiction, author=J. R. R. Tolkien, title=The Lord of the Rings, isbn=0-395-19395-8, price=22.99}
reference
Nigel Rees
Sayings of the Century
fiction
Evelyn Waugh
Sword of Honour
12.99
fiction
Herman Melville
Moby Dick
0-553-21311-3
JavaWeb
X-rapido
Top-link
0-553-211231-3
32.68
fiction
J. R. R. Tolkien
The Lord of the Rings
0-395-19395-8
22.99
19.95
**********authors16**********
5