parser.asStream
parser.asStream(filename)
parser.asStream(filename, start)
Description
Reads a large Json file as a stream (JsonObjectStream).
If there is a start then the stream start at the object with that name.
Call next() on the stream to get the next object.
Parameters
| Parameter | Type | Explanation |
|---|---|---|
| filename | String | name of the file |
| start | String | name of the object to start the stream from |
Example
def stream = parser.asStream("large-data.json")
def obj = stream.next()
while (obj != null) {
// Process each JSON object
println "Name: ${obj.name}"
println "Age: ${obj.age}"
println "Active: ${obj.active}"
obj = stream.next()
}
// You can also start from a specific field
def stream2 = parser.asStream("data.json", "results")
def result = stream2.next()
warning
asStream() handles null values differently than asMap(). When a JSON field contains null, asStream() returns the string "null" while asMap() returns actual null. Always check for string "null" when using streaming:
// Correct way to check for null values in streaming
if (obj.someField == "null") {
// Handle null case
}
// NOT this (won't work with asStream):
if (obj.someField == null) {
// This won't catch null values from asStream()
}