The JSON datatype supports reading JSON from Strings or Readers.

Reading JSON from a String:

  1. import static org.camunda.spin.Spin.*;
  2. import static org.camunda.spin.DataFormats.*;
  3. SpinJsonNode json = S("{\"customer\": \"Kermit\"}", json());

The second paramter json() hints Spin to use the JSON data format for parsing the JSON.

Alternatively, you can directly use the JSON(…) function:

  1. import static org.camunda.spin.Spin.*;
  2. SpinJsonNode json = JSON("{\"customer\": \"Kermit\"}");

String values that represent JSON primitive values can also be read. For example, JSON("true") returns a SpinJsonNode that represents the boolean value true.

Reading JSON from a Reader:

Spin also supports reading JSON from an instance of java.io.Reader:

  1. import static org.camunda.spin.Spin.*;
  2. import static org.camunda.spin.DataFormats.*;
  3. SpinJsonNode json = S(reader, json());

The JSON(…) method also supports readers. The following example shows how to read JSON from a file (error handling ommitted):

  1. import static org.camunda.spin.Spin.*;
  2. FileInputStream fis = new FileInputStream("/tmp/customer.json");
  3. InputStreamReader reader = new InputStreamReader(fis, "utf-8");
  4. SpinJsonNode json = JSON(reader);

Reading JSON using a Script Language

JSON can be read from script languages in the same way as from Java. Since script languages use dynamic typing, you do not need to hint the data format but you can use autodetection. The following example demonstrates how to read JSON in Javascript:

  1. var customer = S('{"customer": "Kermit"}');

Reading JSON Properties

To fetch properties from the JSON tree you can use .prop("name"). This will return the property as SpinJsonNode and you can use this to get the value of the property as the following examples will demonstrate:

in Java:

  1. import static org.camunda.spin.Spin.*;
  2. SpinJsonNode json = JSON("{\"customer\": \"Kermit\"}");
  3. SpinJsonNode customerProperty = json.prop("customer");
  4. String customerName = customerProperty.stringValue();

in Javascript:

  1. var json = S('{"customer": "Kermit"}');
  2. var customerProperty = json.prop("customer");
  3. var customerName = customerProperty.value();

The Different Value Types

The method SpinJsonNode#value() can be used to get the Java equivalent of a String/Boolean/Number or null JSON property. It throws an exception for Object and Array properties. There are also:

  • stringValue() - gets a Java String representation of the value or throws an exception if the value is not a String

  • numberValue() - gets a Java Number representation of the value or throws an exception if the value is not a Number

  • boolValue() - gets a Java Boolean representation of the value or throws an exception if the value is not a Boolean

    Value type checks of the property

  • isObject() returns boolean

  • hasProp() returns boolean
  • isBoolean() returns boolean
  • isNumber() returns boolean
  • isString() returns boolean
  • isNull() returns boolean
  • isValue() returns boolean
  • isArray() returns boolean
    Script example (JavaScript):
  1. var json = S('{"over18":false}');
  2. json.prop('over18').isBoolean() //returns true

Fetch Array of Data

You can also fetch a list of items if your property is an array of data.

in Java:

  1. import static org.camunda.spin.Spin.*;
  2. SpinJsonNode json = JSON("{\"customer\": \[\"Kermit\", \"Waldo\"\]}");
  3. SpinJsonNode customerProperty = json.prop("customer");
  4. SpinList customers = customerProperty.elements();
  5. SpinJsonNode customer = customers.get(0);
  6. String customerName = customer.stringValue();

in Javascript:

  1. var json = S('{"customer": ["Kermit", "Waldo"]}');
  2. var customerProperty = json.prop("customer");
  3. var customers = customerProperty.elements();
  4. var customer = customers.get(0)
  5. var customerName = customer.value();

Fetch Field Names

Spin allows us to use the .fieldNames() method to fetch the names of all child nodes and properties in a node. The following example shows you how to use .fieldNames() in Java and Javascript.

in Java:

  1. import static org.camunda.spin.Spin.*;
  2. SpinJsonNode json = JSON("{\"customer\": \[\"Kermit\", \"Waldo\"\]}");
  3. ArrayList fieldNames = json.fieldNames();
  4. String fieldName1 = fieldNames.get(0)

in Javascript:

  1. var json = S('{"customer": ["Kermit", "Waldo"]}');
  2. var fieldNames = json.fieldNames();
  3. var fieldName1 = fieldNames.get(0)

Set JSON Properties

To set a property you can use the .prop("name", object) method. This allows you to set one of the following 5 simple types:

  • String - Example: .prop("name", "Waldo")
  • Integer - Example: .prop("age", 42)
  • Long - Example: .prop("period", 4200000000)
  • Float - Example: .prop("price", 42.00)
  • Boolean - Example: .prop("active", true)
    or one of the 2 following container types:

  • Array - Could contain a number of simple and container types

Example in Java:

import static org.camunda.spin.Spin.*;

SpinJsonNode json = JSON("{\"customer\": [\"Kermit\", \"Waldo\"]}");
ArrayList list = new ArrayList();
list.add("new entry");
list.add("new entry2");
json.prop("new_array", list);

Example in Javascript:

var json = S('{"customer": ["Kermit", "Waldo"]}');
var list = ["new entry", "new entry2"];
json.prop("new_array", list);

  • Object - Could contain a number of simple and container types

Example in Java:

import static org.camunda.spin.Spin.*;

SpinJsonNode json = JSON("{\"customer\": [\"Kermit\", \"Waldo\"]}");
Map object = new HashMap();
object.put("new_entry", 42);
object.put("new_entry2", "Yeah!");
json.prop("new_object", object);

Example in Javascript:

var json = S('{"customer": ["Kermit", "Waldo"]}');
var object = {
"new_entry": 1,
"new_entry2": "Yeah!"
};
json.prop("new_array", object);

Remove JSON Properties

There are 2 ways to remove properties from a JSON object.

  • .deleteProp("name") - Removes a property with given name.
  • .deleteProp() - Removes one or more properties with given names.
    For more details see the following examples for Javascript and Java.

Java:

  1. import static org.camunda.spin.Spin.*;
  2. SpinJsonNode json = JSON("{\"customer\": \"Kermit\", \"language\": \"en\"}");
  3. List<String> listOfNames = new ArrayList<String>();
  4. listOfNames.add("customer");
  5. listOfNames.add("language");
  6. // removes only the customer property
  7. json.deleteProp("customer");
  8. // removes customer and language
  9. json.deleteProp(list);

Javascript:

  1. var json = S('{"customer": ["Kermit", "Waldo"], "language": "en"}');
  2. var list = ["customer", "en"];
  3. // removes only the customer property
  4. json.deleteProp("customer");
  5. // removes customer and language
  6. json.deleteProp(list);

Work with JSON Arrays

JSON arrays represent a list of objects. Spin offers the following methods to manipulate this list:

  • .indexOf() - Fetches the index of the FIRST occurrence of the searched object.
  • .lastIndexOf() - Fetches the index of the LAST occurrence of the searched object.
  • .append() - Appends an object to the end of the list.
  • .insertAt(, ) - Appends an object at the specific index of the list.
  • .insertBefore(, ) - Inserts an object before the FIRST occurrence of another object.
  • .insertAfter(, ) - Inserts an object after the FIRST occurrence of another object.
  • .remove() - Removes the FIRST occurrence of the object.
  • .removeLast() - Removes the LAST occurrence of the object.
  • .removeAt(Index) - Removes the list entry at the specified index.

  • These methods allow us to work with JSON arrays in a fast way. To show this, we will use the following JSON Object as an example:
  1. {
  2. "test-array" : [
  3. "testdata1",
  4. "testdata2",
  5. 1,
  6. 2,
  7. true,
  8. 1,
  9. false,
  10. 1
  11. ]
  12. }

So let’s see how we can manipulate this list in some examples.

Example 1 - Get the Index of ‘testdata2’ and the Last Occurrence of ‘1’:

  1. import static org.camunda.spin.Spin.*;
  2. SpinJsonNode json = JSON("{\"test-array\" : [\"testdata1\",\"testdata2\",1,2,true,1,false,1]}");
  3. SpinJsonNode list = json.prop("test-array");
  4. Integer i = list.indexOf("testdata2"); // Should be '1'
  5. Integer j = list.lastIndexOf(1); // Should be '7'
  1. var json = S('{"test-array" : ["testdata1","testdata2",1,2,true,1,false,1]}');
  2. var list = json.prop("test-array");
  3. var i = list.indexOf("testdata2"); // should be 1
  4. var j = list.lastIndexOf(1); // Should be '7'

Example 2 - Add and Remove Data to/from the list:

  1. import static org.camunda.spin.Spin.*;
  2. SpinJsonNode json = JSON("{\"test-array\" : [\"testdata1\",\"testdata2\",1,2,true,1,false,1]}");
  3. SpinJsonNode list = json.prop("test-array");
  4. list.append("test2"); // at the end of the list there should now be "test2"
  5. list.remove("test2"); // Aaaand now, it is gone ;)
  6. list.insertAt(1, "test3"); // test3 should now be inserted before testdata2
  7. list.removeAt(1, "test3"); // Aaaand now, it is gone ;)
  8. list.insertBefore(true, "test4"); // now there should be test4 on index 4
  9. list.insertAfter(true, 5); // So now 5 is on index 6
  1. var json = S('{"test-array" : ["testdata1","testdata2",1,2,true,1,false,1]}');
  2. var list = json.prop("test-array");
  3. list.append("test2"); // at the end of the list there should now be "test2"
  4. list.remove("test2"); // Aaaand now, it is gone ;)
  5. list.insertAt(1, "test3"); // test3 should now be inserted before testdata2
  6. list.removeAt(1, "test3"); // Aaaand now, it is gone ;)
  7. list.insertBefore(true, "test4"); // now there should be test4 on index 4
  8. list.insertAfter(true, 5); // So now 5 is on index 6

原文: https://docs.camunda.org/manual/7.9/reference/spin/json/01-reading-json/