Reading JSON from a String:
json() hints Spin to use the JSON data format for parsing the JSON.
Alternatively, you can directly use the JSON(...) function:
JSON("true") returns a SpinJsonNode that represents the boolean value true.
Reading JSON from a Reader:
Spin also supports reading JSON from an instance ofjava.io.Reader:
JSON(...) method also supports readers. The following example shows how to read JSON from a file (error handling ommitted):
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: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:
The Different Value Types
The methodSpinJsonNode#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 JavaStringrepresentation of the value or throws an exception if the value is not a String#numberValue()- gets a JavaNumberrepresentation of the value or throws an exception if the value is not a Number#boolValue()- gets a JavaBooleanrepresentation of the value or throws an exception if the value is not a Boolean
Value type checks of the property
isObject()returns booleanhasProp()returns booleanisBoolean()returns booleanisNumber()returns booleanisString()returns booleanisNull()returns booleanisValue()returns booleanisArray()returns boolean
Fetch Array of Data
You can also fetch a list of items if your property is an array of data. in Java: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:
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)
-
Array - Could contain a number of simple and container types
Example in Java:
-
Object - Could contain a number of simple and container types
Example in Java:
Remove JSON Properties
There are 2 ways to remove properties from a JSON object..deleteProp("name")- Removes a property with given name..deleteProp(<List of names>)- Removes one or more properties with given names.
Work with JSON Arrays
JSON arrays represent a list of objects. Spin offers the following methods to manipulate this list:.indexOf(<Object>)- Fetches the index of the FIRST occurrence of the searched object..lastIndexOf(<Object>)- Fetches the index of the LAST occurrence of the searched object..append(<Object>)- Appends an object to the end of the list..insertAt(<Index>, <Object>)- Appends an object at the specific index of the list..insertBefore(<Search object>, <Object>)- Inserts an object before the FIRST occurrence of another object..insertAfter(<Search object>, <Object>)- Inserts an object after the FIRST occurrence of another object..remove(<Object>)- Removes the FIRST occurrence of the object..removeLast(<Object>)- Removes the LAST occurrence of the object..removeAt(Index)- Removes the list entry at the specified index.