Groovy JsonSlurper

Posted on by By Somen Sarkar, in Databases, ETL, Javascript | 0

JSON Slurper in Groovy

What is JSON slurper?
JSON slurper is a class in groovy that can be used to parse text or read content into a data structure of lists and maps.
In other words we can say that it converts the json string into list and maps, which can be used effectivly in the code.

Example :

import groovy.json.JsonSlurper;

def jsonSlurper = new JsonSlurper()
def output = jsonSlurper.parseText('{"employee":{"id":"hdev021","dob":23/1/1987,"skillset":["c","java","oracle","design patterns"]}}')

Here we can see that the String is being parsed by jsonSlurper object and the output is now a json object.
The fields can be accessed by dot (.) notation directly in the code
print output.employee.id // Prints the following hdev021

JsonSlurper can use several types of JSON parsers.

1. INDEX_OVERLAY (Used in REST calls)
2. CHAR_BUFFER (parses the datatypes like integer, date time, long etc)
3. CHARACTER_SOURCE (This is used when json string is large).
4. LAX (Relax json parsing comments, no quote strings, etc are allowed)
5. CHAR_BUFFER (is a super fast parser)

The following types can be set by using the method setType of JsonSlurper

 

Source: internet.

logo

Free Open Source BI Platform

logo
  • AI-Powered Analytics
  • Embedded Analytics
  • Single Sign-On (SSO)
  • Multi-Tenancy
  • Paginated Canned Reporting
  • Interactive Dashboard
  • White Labeling
0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments