Class JsonSlurper
Example usage:
JsonSlurper can use several types of JSON parsers. Please read the documentation for
JsonParserType. There are relaxed mode parsers, large file parser, and index overlay parsers.
Don't worry, it is all groovy. JsonSlurper will just work, but understanding the different parser
types may allow you to drastically improve the performance of your JSON parsing.
def slurper = new groovy.json.JsonSlurper()
def result = slurper.parseText('{"person":{"name":"Guillaume","age":33,"pets":["dog","cat"]}}')
assert result.person.name == "Guillaume"
assert result.person.age == 33
assert result.person.pets.size() == 2
assert result.person.pets[0] == "dog"
assert result.person.pets[1] == "cat"
parser = new JsonSlurper().setType(JsonParserType.INDEX_OVERLAY);
JsonSlurper reads the whole document into memory and does not bound total input size (only
nesting depth, via setMaxNestingDepth(int)). For very large documents, bound the input size
before parsing, or use a streaming / Jackson-based parser instead; Groovy interoperates cleanly with
Jackson (the groovy-yaml/groovy-toml/groovy-csv slurpers are already
Jackson-backed).
- Since:
- 1.8.0
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionWhat a string in a full ISO-8601 or JSON-date form is turned into.intThe maximum nesting depth of arrays/objects the parser will accept before throwing aJsonException.intThe maximum length, in characters, of a single number token the parser will accept before throwing aJsonException.intThe threshold (in characters) that selects which parser strategy theFile-based parse methods use: documents smaller than this are parsed in memory, larger ones use a windowing buffer parser.getType()Parser type.booleanDeprecated.booleanisChop()Turns on buffer chopping for index overlay.booleanTurns on buffer lazy chopping for index overlay.parse(byte[] bytes) Parse a JSON data structure from content from a byte array.Parse a JSON data structure from content from a byte array.parse(char[] chars) Parse a JSON data structure from content from a char array.Parse a JSON data structure from content within a given File.Parse a JSON data structure from content within a given File.parse(InputStream inputStream) Parse a JSON data structure from content from an inputStreamparse(InputStream inputStream, String charset) Parse a JSON data structure from content from an inputStreamParse a JSON data structure from content from a readerParse a JSON data structure from content at a given URL.Parse a JSON data structure from content at a given URL.Parse a JSON data structure from content at a given URL.Parse a JSON data structure from content at a given URL.Parse a JSON data structure from content within a given Path.Parse a JSON data structure from content within a given Path.Parse a JSON data structure from content at a given URL.Parse a JSON data structure from content at a given URL.Parse a text representation of a JSON data structuresetCheckDates(boolean checkDates) Deprecated.a boolean cannot say which type is wanted; usesetDateHandling(JsonDateHandling)setChop(boolean chop) Turns on buffer chopping for index overlay.setDateHandling(JsonDateHandling dateHandling) Sets what a string in a full ISO-8601 or JSON-date form is turned into.setLazyChop(boolean lazyChop) Turns on buffer lazy chopping for index overlay.setMaxNestingDepth(int maxNestingDepth) Sets the maximum nesting depth of arrays/objects the parser will accept before throwing aJsonException.setMaxNumberLength(int maxNumberLength) Sets the maximum length, in characters, of a single number token the parser will accept before throwing aJsonException.setMaxSizeForInMemory(int maxSizeForInMemory) Sets the threshold that selects which parser strategy theFile-based parse methods use (seegetMaxSizeForInMemory()).setType(JsonParserType type) Parser type.
-
Constructor Details
-
JsonSlurper
public JsonSlurper()
-
-
Method Details
-
getMaxSizeForInMemory
public int getMaxSizeForInMemory()The threshold (in characters) that selects which parser strategy theFile-based parse methods use: documents smaller than this are parsed in memory, larger ones use a windowing buffer parser.This is a parser-strategy selector, not a size limit. It does not reject or cap input — an oversized document is still parsed in full (by the windowing parser). It is consulted only by the
File-based parse methods; theparse(Reader),parse(InputStream)andparse(URL)entry points ignore it and buffer their entire input into memory regardless of this value. To bound untrusted input, limit its size yourself before parsing (and cap nesting viasetMaxNestingDepth(int)or thegroovy.json.maxNestingDepthsystem property).- Returns:
- the in-memory vs windowing-parser selection threshold
- Since:
- 2.3
-
setMaxSizeForInMemory
Sets the threshold that selects which parser strategy theFile-based parse methods use (seegetMaxSizeForInMemory()).This selects a parser strategy; it does not limit or reject input. Setting it gives no DoS protection: it is not consulted by
parse(Reader),parse(InputStream)orparse(URL), and even theFile-based methods still parse documents larger than the threshold. Bound untrusted input by its size before parsing, and cap nesting viasetMaxNestingDepth(int)orgroovy.json.maxNestingDepth.- Parameters:
maxSizeForInMemory- the in-memory vs windowing-parser selection threshold- Returns:
- this
JsonSlurperinstance - Since:
- 2.3
-
getType
Parser type.- Returns:
- type
- Since:
- 2.3
- See Also:
-
setType
Parser type.- Returns:
- JsonSlurper
- Since:
- 2.3
- See Also:
-
isChop
public boolean isChop()Turns on buffer chopping for index overlay.- Returns:
- chop on or off
- Since:
- 2.3
- See Also:
-
setChop
Turns on buffer chopping for index overlay.- Returns:
- JsonSlurper
- Since:
- 2.3
- See Also:
-
isLazyChop
public boolean isLazyChop()Turns on buffer lazy chopping for index overlay.- Returns:
- on or off
- Since:
- 2.3
- See Also:
-
setLazyChop
Turns on buffer lazy chopping for index overlay.- Returns:
- JsonSlurper
- Since:
- 2.3
- See Also:
-
isCheckDates
Deprecated.a boolean cannot say which type is wanted; usegetDateHandling()Whether a string in a full ISO-8601 or JSON-date form is returned as aDaterather than as aString. On by default.This applies to
JsonParserType.INDEX_OVERLAYandJsonParserType.LAX; the other two parser types return the string either way, so setting this has no effect on them. A date without a time, such as"2026-09-04", is left as aStringby all four.- Returns:
- whether date-like strings are converted
- Since:
- 2.3
-
setCheckDates
Deprecated.a boolean cannot say which type is wanted; usesetDateHandling(JsonDateHandling)Sets whether a string in a full ISO-8601 or JSON-date form is returned as aDaterather than as aString. On by default.This applies to
JsonParserType.INDEX_OVERLAYandJsonParserType.LAX; the other two parser types return the string either way, so setting this has no effect on them. Note that aDateis an instant, so an offset in the source is not preserved by the conversion. Has no effect oncesetDateHandling(JsonDateHandling)has been called on this slurper, so that a choice made through the wider option is not undone by this one.- Parameters:
checkDates- whether date-like strings should be converted- Returns:
- this slurper, for chaining
- Since:
- 2.3
-
getDateHandling
What a string in a full ISO-8601 or JSON-date form is turned into.- Returns:
- the handling in effect
- Since:
- 6.0.0
-
setDateHandling
Sets what a string in a full ISO-8601 or JSON-date form is turned into. Defaults toJsonDateHandling.UTIL_DATE, which is what a slurper has always produced.This applies to
JsonParserType.INDEX_OVERLAYandJsonParserType.LAX; the other two parser types return the string whatever is set here. A date carrying no time, such as"2026-09-04", is left as aStringby all four. Calling this also makessetCheckDates(boolean)a no-op on this slurper, whichever order the two are called in.- Parameters:
dateHandling- what a date-like string should become- Returns:
- this slurper, for chaining
- Since:
- 6.0.0
-
getMaxNestingDepth
public int getMaxNestingDepth()The maximum nesting depth of arrays/objects the parser will accept before throwing aJsonException. This guards the recursive-descent parsers against a small but deeply-nested document driving aStackOverflowError. A value of0or less disables the check (restoring the previous, unbounded behaviour). Defaults toBaseJsonParser.DEFAULT_MAX_NESTING_DEPTH, and can be overridden globally with thegroovy.json.maxNestingDepthsystem property.- Returns:
- the maximum nesting depth
- Since:
- 6.0.0
-
setMaxNestingDepth
Sets the maximum nesting depth of arrays/objects the parser will accept before throwing aJsonException. A value of0or less disables the check.- Parameters:
maxNestingDepth- maximum number of nested arrays/objects to allow- Returns:
- this
JsonSlurper - Since:
- 6.0.0
-
getMaxNumberLength
public int getMaxNumberLength()The maximum length, in characters, of a single number token the parser will accept before throwing aJsonException. Converting a digit run past thelongrange builds aBigIntegerorBigDecimal, and decimal conversion is superlinear in the digit count, so an uncapped token lets a small document cost quadratic CPU. A value of0or less disables the check (restoring the previous, unbounded behaviour). Defaults toBaseJsonParser.DEFAULT_MAX_NUMBER_LENGTH, and can be overridden globally with thegroovy.json.maxNumberLengthsystem property.- Returns:
- the maximum number token length
- Since:
- 6.0.0
-
setMaxNumberLength
Sets the maximum length, in characters, of a single number token the parser will accept before throwing aJsonException. A value of0or less disables the check.- Parameters:
maxNumberLength- maximum number of characters in a number token- Returns:
- this
JsonSlurper - Since:
- 6.0.0
-
parseText
Parse a text representation of a JSON data structure- Parameters:
text- JSON text to parse- Returns:
- a data structure of lists and maps
-
parse
Parse a JSON data structure from content from a reader- Parameters:
reader- reader over a JSON content- Returns:
- a data structure of lists and maps
-
parse
Parse a JSON data structure from content from an inputStream- Parameters:
inputStream- stream over a JSON content- Returns:
- a data structure of lists and maps
- Since:
- 2.3
-
parse
Parse a JSON data structure from content from an inputStream- Parameters:
inputStream- stream over a JSON contentcharset- charset- Returns:
- a data structure of lists and maps
- Since:
- 2.3
-
parse
Parse a JSON data structure from content from a byte array.- Parameters:
bytes- buffer of JSON contentcharset- charset- Returns:
- a data structure of lists and maps
- Since:
- 2.3
-
parse
Parse a JSON data structure from content from a byte array.- Parameters:
bytes- buffer of JSON content- Returns:
- a data structure of lists and maps
- Since:
- 2.3
-
parse
Parse a JSON data structure from content from a char array.- Parameters:
chars- buffer of JSON content- Returns:
- a data structure of lists and maps
- Since:
- 2.3
-
parse
Parse a JSON data structure from content within a given Path.- Parameters:
path-Pathcontaining JSON content- Returns:
- a data structure of lists and maps
- Throws:
IOException
-
parse
Parse a JSON data structure from content within a given Path.- Parameters:
path-Pathcontaining JSON contentcharset- the charset for this File- Returns:
- a data structure of lists and maps
- Throws:
IOException
-
parse
Parse a JSON data structure from content within a given File.- Parameters:
file- File containing JSON content- Returns:
- a data structure of lists and maps
- Since:
- 2.2.0
-
parse
Parse a JSON data structure from content within a given File.- Parameters:
file- File containing JSON contentcharset- the charset for this File- Returns:
- a data structure of lists and maps
- Since:
- 2.2.0
-
parse
Parse a JSON data structure from content at a given URL.- Parameters:
url- URL containing JSON content- Returns:
- a data structure of lists and maps
- Since:
- 2.2.0
-
parse
Parse a JSON data structure from content at a given URL.- Parameters:
url- URL containing JSON contentparams- connection parameters- Returns:
- a data structure of lists and maps
- Since:
- 2.2.0
-
parse
public Object parse(@NamedParam(value="connectTimeout",type=java.lang.Integer.class) @NamedParam(value="readTimeout",type=java.lang.Integer.class) @NamedParam(value="useCaches",type=java.lang.Boolean.class) @NamedParam(value="allowUserInteraction",type=java.lang.Boolean.class) @NamedParam(value="requestProperties",type=java.util.Map.class) Map<String, ?> params, URL url) Parse a JSON data structure from content at a given URL. Convenience variant when using Groovy named parameters for the connection params.- Parameters:
params- connection parametersurl- URL containing JSON content- Returns:
- a data structure of lists and maps
- Since:
- 2.2.0
-
parse
Parse a JSON data structure from content at a given URL.- Parameters:
url- URL containing JSON contentcharset- the charset for this File- Returns:
- a data structure of lists and maps
- Since:
- 2.2.0
-
parse
Parse a JSON data structure from content at a given URL.- Parameters:
url- URL containing JSON contentparams- connection parameterscharset- the charset for this File- Returns:
- a data structure of lists and maps
- Since:
- 2.2.0
-
parse
Parse a JSON data structure from content at a given URL. Convenience variant when using Groovy named parameters for the connection params.- Parameters:
params- connection parametersurl- URL containing JSON contentcharset- the charset for this File- Returns:
- a data structure of lists and maps
- Since:
- 2.2.0
-
getDateHandling()