The JSON datatype supports querying with the JSONPath query language.

Querying an Element

  1. import static org.camunda.spin.Spin.JSON;
  2. String json = "{\"child\": [{\"id\": 1,\"name\": \"Lucy\",\"sex\": \"female\"},{\"id\": 2,\"name\": \"Tracy\",\"sex\": \"female\"}],\"number\": 1,\"boolean\": true}";
  3. SpinJsonNode child = JSON(json).jsonPath("$.child[0]").element();

Querying an Element List

  1. import static org.camunda.spin.Spin.JSON;
  2. String json = "{\"child\": [{\"id\": 1,\"name\": \"Lucy\",\"sex\": \"female\"},{\"id\": 2,\"name\": \"Tracy\",\"sex\": \"female\"}],\"number\": 1,\"boolean\": true}";
  3. SpinList<SpinJsonNode> childs = JSON(json).jsonPath("$.child").elementList();

Querying a String

  1. import static org.camunda.spin.Spin.JSON;
  2. String json = "{\"child\": [{\"id\": 1,\"name\": \"Lucy\",\"sex\": \"female\"},{\"id\": 2,\"name\": \"Tracy\",\"sex\": \"female\"}],\"number\": 1,\"boolean\": true}";
  3. String value = JSON(json).jsonPath("$.child[0].name").stringValue();

Querying a Number

  1. import static org.camunda.spin.Spin.JSON;
  2. String json = "{\"child\": [{\"id\": 1,\"name\": \"Lucy\",\"sex\": \"female\"},{\"id\": 2,\"name\": \"Tracy\",\"sex\": \"female\"}],\"number\": 1,\"boolean\": true}";
  3. Double count = JSON(json).jsonPath("$.number").numberValue();

Querying a Boolean

  1. import static org.camunda.spin.Spin.JSON;
  2. String json = "{\"child\": [{\"id\": 1,\"name\": \"Lucy\",\"sex\": \"female\"},{\"id\": 2,\"name\": \"Tracy\",\"sex\": \"female\"}],\"number\": 1,\"boolean\": true}";
  3. 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!

  1. import static org.camunda.spin.Spin.JSON;
  2. String json = "{\"child\": [{\"id\": 1,\"name\": \"Lucy\",\"sex\": \"female\"},{\"id\": 2,\"name\": \"Tracy\",\"sex\": \"female\"}],\"number\": 1,\"boolean\": true}";
  3. SpinList<SpinJsonNode> girls = JSON(json).jsonPath("$.child[?(@.sex == 'female')]").elementList();

原文: https://docs.camunda.org/manual/7.9/reference/spin/json/03-querying-json/