Package org.kissweb

Class JsonPath

java.lang.Object
org.kissweb.JsonPath

public class JsonPath extends Object
This class provides a variety of features on top of the functionality provided by org.json. In addition to being able to parse a string into a json object, the json object can also be obtained from a file. Once the json object is obtained, embedded elements may be indexed similar to XPath thus providing an easy method of destructuring a complex json object. Lastly, rather than throw an exception when a data item is missing, these methods return null.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static Boolean
    getBoolean(String delimiter, org.json.JSONObject jobj, String path)
    Traverse an object returning the Boolean referenced by path.
    static Boolean
    getBoolean(org.json.JSONObject jobj, String path)
    Traverse an object returning the Boolean referenced by path.
    static Character
    getCharacter(String delimiter, org.json.JSONObject jobj, String path)
    Traverse an object returning the Character referenced by path.
    static Character
    getCharacter(org.json.JSONObject jobj, String path)
    Traverse an object returning the Character referenced by path.
    static Integer
    getDate(String delimiter, org.json.JSONObject jobj, String path)
    Traverse an object returning the Integer date referenced by path.
    static Integer
    getDate(org.json.JSONObject jobj, String path)
    Traverse an object returning the Integer date referenced by path.
    static Double
    getDouble(String delimiter, org.json.JSONObject jobj, String path)
    Traverse an object returning the Double referenced by path.
    static Double
    getDouble(org.json.JSONObject jobj, String path)
    Traverse an object returning the Double referenced by path.
    static Integer
    getInteger(String delimiter, org.json.JSONObject jobj, String path)
    Traverse an object returning the Integer referenced by path.
    static Integer
    getInteger(org.json.JSONObject jobj, String path)
    Traverse an object returning the Integer referenced by path.
    static org.json.JSONArray
    getJSONArray(String delimiter, org.json.JSONObject jobj, String path)
    Traverse an object returning the JSONArray referenced by path.
    static org.json.JSONArray
    getJSONArray(org.json.JSONObject jobj, String path)
    Traverse an object returning the JSONArray referenced by path.
    static Long
    getLong(String delimiter, org.json.JSONObject jobj, String path)
    Traverse an object returning the Long referenced by path.
    static Long
    getLong(org.json.JSONObject jobj, String path)
    Traverse an object returning the Long referenced by path.
    static org.json.JSONObject
    getObject(String delimiter, org.json.JSONObject jobj, String path)
    Traverse an object returning the sub-object referenced by path.
    static org.json.JSONObject
    getObject(org.json.JSONObject jobj, String path)
    Traverse an object returning the sub-object referenced by path.
    static String
    getString(String delimiter, org.json.JSONObject jobj, String path)
    Traverse an object returning the String referenced by path.
    static String
    getString(org.json.JSONObject jobj, String path)
    Traverse an object returning the String referenced by path.
    static void
    main(String[] argv)
     
    static org.json.JSONObject
    This takes a JSON string or a file name (containing JSON) and returns a JSON object.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • JsonPath

      public JsonPath()
  • Method Details

    • toJson

      public static org.json.JSONObject toJson(String str) throws IOException
      This takes a JSON string or a file name (containing JSON) and returns a JSON object.

      If in a string, the string must start with "{" or space character
      Parameters:
      str - the json string or file name containing a json string
      Returns:
      Throws:
      IOException
    • getObject

      public static org.json.JSONObject getObject(String delimiter, org.json.JSONObject jobj, String path)
      Traverse an object returning the sub-object referenced by path.
      Parameters:
      delimiter - the delimiter used in path to delimit the each path sub-part
      jobj - the initial json object
      path - a string indicating the path to the desired element
      Returns:
      the json object being referenced by path or null if not found
    • getObject

      public static org.json.JSONObject getObject(org.json.JSONObject jobj, String path)
      Traverse an object returning the sub-object referenced by path. The delimiter used in path is a period.
      Parameters:
      jobj - the initial json object
      path - a string indicating the path to the desired element
      Returns:
      the json object being referenced by path or null if not found
    • getString

      public static String getString(String delimiter, org.json.JSONObject jobj, String path)
      Traverse an object returning the String referenced by path.
      Parameters:
      delimiter - the delimiter used in path to delimit the each path sub-part
      jobj - the initial json object
      path - a string indicating the path to the desired element
      Returns:
      the String being referenced by path or null if not found
    • getString

      public static String getString(org.json.JSONObject jobj, String path)
      Traverse an object returning the String referenced by path. The delimiter used in path is a period.
      Parameters:
      jobj - the initial json object
      path - a string indicating the path to the desired element
      Returns:
      the String being referenced by path or null if not found
    • getInteger

      public static Integer getInteger(String delimiter, org.json.JSONObject jobj, String path)
      Traverse an object returning the Integer referenced by path.
      Parameters:
      delimiter - the delimiter used in path to delimit the each path sub-part
      jobj - the initial json object
      path - a string indicating the path to the desired element
      Returns:
      the Integer being referenced by path or null if not found
    • getInteger

      public static Integer getInteger(org.json.JSONObject jobj, String path)
      Traverse an object returning the Integer referenced by path. The delimiter used in path is a period.
      Parameters:
      jobj - the initial json object
      path - a string indicating the path to the desired element
      Returns:
      the Integer being referenced by path or null if not found
    • getDouble

      public static Double getDouble(String delimiter, org.json.JSONObject jobj, String path)
      Traverse an object returning the Double referenced by path.
      Parameters:
      delimiter - the delimiter used in path to delimit the each path sub-part
      jobj - the initial json object
      path - a string indicating the path to the desired element
      Returns:
      the Double being referenced by path or null if not found
    • getDouble

      public static Double getDouble(org.json.JSONObject jobj, String path)
      Traverse an object returning the Double referenced by path. The delimiter used in path is a period.
      Parameters:
      jobj - the initial json object
      path - a string indicating the path to the desired element
      Returns:
      the Double being referenced by path or null if not found
    • getJSONArray

      public static org.json.JSONArray getJSONArray(String delimiter, org.json.JSONObject jobj, String path)
      Traverse an object returning the JSONArray referenced by path.
      Parameters:
      delimiter - the delimiter used in path to delimit the each path sub-part
      jobj - the initial json object
      path - a string indicating the path to the desired element
      Returns:
      the JSONArray being referenced by path or null if not found
    • getJSONArray

      public static org.json.JSONArray getJSONArray(org.json.JSONObject jobj, String path)
      Traverse an object returning the JSONArray referenced by path. The delimiter used in path is a period.
      Parameters:
      jobj - the initial json object
      path - a string indicating the path to the desired element
      Returns:
      the JSONArray being referenced by path or null if not found
    • getBoolean

      public static Boolean getBoolean(String delimiter, org.json.JSONObject jobj, String path)
      Traverse an object returning the Boolean referenced by path.
      Parameters:
      delimiter - the delimiter used in path to delimit the each path sub-part
      jobj - the initial json object
      path - a string indicating the path to the desired element
      Returns:
      the Boolean being referenced by path or null if not found
    • getBoolean

      public static Boolean getBoolean(org.json.JSONObject jobj, String path)
      Traverse an object returning the Boolean referenced by path. The delimiter used in path is a period.
      Parameters:
      jobj - the initial json object
      path - a string indicating the path to the desired element
      Returns:
      the Boolean being referenced by path or null if not found
    • getLong

      public static Long getLong(String delimiter, org.json.JSONObject jobj, String path)
      Traverse an object returning the Long referenced by path.
      Parameters:
      delimiter - the delimiter used in path to delimit the each path sub-part
      jobj - the initial json object
      path - a string indicating the path to the desired element
      Returns:
      the Long being referenced by path or null if not found
    • getLong

      public static Long getLong(org.json.JSONObject jobj, String path)
      Traverse an object returning the Long referenced by path. The delimiter used in path is a period.
      Parameters:
      jobj - the initial json object
      path - a string indicating the path to the desired element
      Returns:
      the Long being referenced by path or null if not found
    • getCharacter

      public static Character getCharacter(String delimiter, org.json.JSONObject jobj, String path)
      Traverse an object returning the Character referenced by path.

      If the field contains mor than one character, the first is returned.
      Parameters:
      delimiter - the delimiter used in path to delimit the each path sub-part
      jobj - the initial json object
      path - a string indicating the path to the desired element
      Returns:
      the Character being referenced by path or null if not found
    • getCharacter

      public static Character getCharacter(org.json.JSONObject jobj, String path)
      Traverse an object returning the Character referenced by path. The delimiter used in path is a period.

      If the field contains mor than one character, the first is returned.
      Parameters:
      jobj - the initial json object
      path - a string indicating the path to the desired element
      Returns:
      the Character being referenced by path or null if not found
    • getDate

      public static Integer getDate(String delimiter, org.json.JSONObject jobj, String path)
      Traverse an object returning the Integer date referenced by path.

      The Integer date returned is of the form YYYYMMDD. So, 6/8/2018 would be returned as 20180608. A null is returned if the data cannot be obtained.
      Parameters:
      delimiter - the delimiter used in path to delimit the each path sub-part
      jobj - the initial json object
      path - a string indicating the path to the desired element
      Returns:
      the Integer date being referenced by path or null if not found
    • getDate

      public static Integer getDate(org.json.JSONObject jobj, String path)
      Traverse an object returning the Integer date referenced by path. The delimiter used in path is a period.

      The Integer date returned is of the form YYYYMMDD. So, 6/8/2018 would be returned as 20180608. A null is returned if the data cannot be obtained.
      Parameters:
      jobj - the initial json object
      path - a string indicating the path to the desired element
      Returns:
      the Integer date being referenced by path or null if not found
    • main

      public static void main(String[] argv) throws IOException
      Throws:
      IOException