Class JsonSlurper

java.lang.Object
de.stefanfrings.utils.JsonSlurper

public class JsonSlurper extends Object
Parse JSON documents of any structure into a flat map key/value pairs.

Example input:

 {
     "name":"stefan",
     "hobbies":["electronics","swimming"],
     "info":{
         "job":"developer",
         "age":40
     }
 }
 

Example result:

  • name = "stefan"
  • hobbies = "electronics"
  • hobbies0 = "electronics"
  • hobbies1 = "swimming"
  • info.job = "developer"
  • info.age = "40"

The elements of arrays are numbered (e.g. hobbies0, hobbies1, hobbies2, ...). In addition, the first element is also available without number suffix (e.g. hobbies).

Author:
Stefan Frings, http://stefanfrings.de/javautils
  • Constructor Details

    • JsonSlurper

      public JsonSlurper()
  • Method Details

    • parse

      public static ConvertingHashMap parse(String jsonDocument) throws IOException
      Parse an JSON document into a simple flat HashMap where the keys reflect the JSON structure.
      Parameters:
      jsonDocument - JSON document
      Returns:
      Map with all collected data.
      Throws:
      IOException - Should never happen