Querying JSON
The JSON datatype supports querying with the JSONPath query language.
Querying an Element
import static org.cibseven.spin.Spin.JSON;
String json = "{\"child\": [{\"id\": 1,\"name\": \"Lucy\",\"sex\": \"female\"},{\"id\": 2,\"name\": \"Tracy\",\"sex\": \"female\"}],\"number\": 1,\"boolean\": true}";
SpinJsonNode child = JSON(json).jsonPath("$.child[0]").element();
Querying an Element List
import static org.cibseven.spin.Spin.JSON;
String json = "{\"child\": [{\"id\": 1,\"name\": \"Lucy\",\"sex\": \"female\"},{\"id\": 2,\"name\": \"Tracy\",\"sex\": \"female\"}],\"number\": 1,\"boolean\": true}";
SpinList<SpinJsonNode> childs = JSON(json).jsonPath("$.child").elementList();
Querying a String
import static org.cibseven.spin.Spin.JSON;
String json = "{\"child\": [{\"id\": 1,\"name\": \"Lucy\",\"sex\": \"female\"},{\"id\": 2,\"name\": \"Tracy\",\"sex\": \"female\"}],\"number\": 1,\"boolean\": true}";
String value = JSON(json).jsonPath("$.child[0].name").stringValue();
Querying a Number
import static org.cibseven.spin.Spin.JSON;
String json = "{\"child\": [{\"id\": 1,\"name\": \"Lucy\",\"sex\": \"female\"},{\"id\": 2,\"name\": \"Tracy\",\"sex\": \"female\"}],\"number\": 1,\"boolean\": true}";
Double count = JSON(json).jsonPath("$.number").numberValue();
Querying a Boolean
import static org.cibseven.spin.Spin.JSON;
String json = "{\"child\": [{\"id\": 1,\"name\": \"Lucy\",\"sex\": \"female\"},{\"id\": 2,\"name\": \"Tracy\",\"sex\": \"female\"}],\"number\": 1,\"boolean\": true}";
Boolean exists = JSON(json).jsonPath("$.boolean").boolValue();
Filtering a Query
Be aware that a filtering expression - the expression in the ()
- is not allowed to contain double quotes!
import static org.cibseven.spin.Spin.JSON;
String json = "{\"child\": [{\"id\": 1,\"name\": \"Lucy\",\"sex\": \"female\"},{\"id\": 2,\"name\": \"Tracy\",\"sex\": \"female\"}],\"number\": 1,\"boolean\": true}";
SpinList<SpinJsonNode> girls = JSON(json).jsonPath("$.child[?(@.sex == 'female')]").elementList();