How to parse JSON value using User Defined Function

In the previous post, we have seen implementing User defined functions using login API.

Let’s check how to parse JSON value using User Defined Function.

Example

  • Use parseJSON function as a Groovy function to parse a JSON and store the values in an object.
  • The object returned from the parseJSON is a JsonNode. (from JsonNode (jackson-databind 2.7.0 API))
  • Use any of the methods in the above docs on the object to extract or manipulate the JSON before returning it.
  • parseStudentData function below parses the student data in a JSON format.
type Query {
parseStudentData(value: String): Json @tan(type:Groovy, inline: """return parseJSON(value)""")
}

Let’s execute the function!

{
  parseStudentData(
    value: "{\"Student\":{\"Name\": \"Hypi\",\"url\": \"https://hypi.io\"}}"
  )
}
#result
{
  "data": {
    "parseStudentData": {
      "Student": {
        "Name": "Hypi",
        "url": "https://hypi.io"
      }
    }
  }
}