Interface JsonUtils


public interface JsonUtils

This interface enables you to access convenient methods to read and manipulate JSON. All the methods accept a JSON String and a location. Some methods accept an additional JSON argument that you can use to modify the supplied JSON String.

Location syntax:

  • .
    The location elements are delimited by a period character
  • [n]
    Specifies the nth item in a JSON array
  • []
    Specifies the last element in a JSON array
  • key
    Identifies keyed entry in a JSON object

Note: Indices are zero based.

For example, when applied to the JSON below:

account.[0].user.name would select "Peter Wiener"

account.[].user.name would select "Carlos Montoya"

account.[1].user.addresses.[] would select "Address 6"

     {
         "account": [
              {
                  "user": {
                      "name": "Peter Wiener",
                      "addresses": [
                          "Address 1",
                          "Address 2",
                          "Address 3"
                      ]
                  }
              },
              {
                  "user": {
                      "name": "Carlos Montoya",
                      "addresses": [
                          "Address 4",
                          "Address 5",
                          "Address 6"
                      ]
                  }
              }
         ]
     }
 

Remarks

Methods that take in JSON accept single quotes in place of double quotes.

If your use case is more complex than this interface allows, see JsonNode and its inheritors.