blob: 466a28d03a5b1dd21917ef2250f8571b44b7408f [file] [log] [blame]
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]--><head>
<meta charset='utf-8'/><meta http-equiv='X-UA-Compatible' content='IE=edge'/><meta name='viewport' content='width=device-width, initial-scale=1'/><meta name='keywords' content='groovy, json, scripting, ginq, gquery, gradle, graalvm, native'/><meta name='description' content='This post looks at parsing JSON using Groovy&apos;s in-built capabilities as well as some other libraries.'/><title>The Apache Groovy programming language - Blogs - Parsing JSON with Groovy</title><link href='../img/favicon.ico' type='image/x-ico' rel='icon'/><link rel='stylesheet' type='text/css' href='../css/bootstrap.css'/><link rel='stylesheet' type='text/css' href='../css/font-awesome.min.css'/><link rel='stylesheet' type='text/css' href='../css/style.css'/><link rel='stylesheet' type='text/css' href='https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.css'/>
</head><body>
<div id='fork-me'>
<a href='https://github.com/apache/groovy'>
<img style='position: fixed; top: 20px; right: -58px; border: 0; z-index: 100; transform: rotate(45deg);' src='/img/horizontal-github-ribbon.png'/>
</a>
</div><div id='st-container' class='st-container st-effect-9'>
<nav class='st-menu st-effect-9' id='menu-12'>
<h2 class='icon icon-lab'>Socialize</h2><ul>
<li>
<a href='https://groovy-lang.org/mailing-lists.html' class='icon'><span class='fa fa-envelope'></span> Discuss on the mailing-list</a>
</li><li>
<a href='https://twitter.com/ApacheGroovy' class='icon'><span class='fa fa-twitter'></span> Groovy on Twitter</a>
</li><li>
<a href='https://groovy-lang.org/events.html' class='icon'><span class='fa fa-calendar'></span> Events and conferences</a>
</li><li>
<a href='https://github.com/apache/groovy' class='icon'><span class='fa fa-github'></span> Source code on GitHub</a>
</li><li>
<a href='https://groovy-lang.org/reporting-issues.html' class='icon'><span class='fa fa-bug'></span> Report issues in Jira</a>
</li><li>
<a href='http://stackoverflow.com/questions/tagged/groovy' class='icon'><span class='fa fa-stack-overflow'></span> Stack Overflow questions</a>
</li><li>
<a href='http://groovycommunity.com/' class='icon'><span class='fa fa-slack'></span> Slack Community</a>
</li>
</ul>
</nav><div class='st-pusher'>
<div class='st-content'>
<div class='st-content-inner'>
<!--[if lt IE 7]>
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
<![endif]--><div><div class='navbar navbar-default navbar-static-top' role='navigation'>
<div class='container'>
<div class='navbar-header'>
<button type='button' class='navbar-toggle' data-toggle='collapse' data-target='.navbar-collapse'>
<span class='sr-only'></span><span class='icon-bar'></span><span class='icon-bar'></span><span class='icon-bar'></span>
</button><a class='navbar-brand' href='../index.html'>
<i class='fa fa-star'></i> Apache Groovy
</a>
</div><div class='navbar-collapse collapse'>
<ul class='nav navbar-nav navbar-right'>
<li class=''><a href='https://groovy-lang.org/learn.html'>Learn</a></li><li class=''><a href='https://groovy-lang.org/documentation.html'>Documentation</a></li><li class=''><a href='/download.html'>Download</a></li><li class=''><a href='https://groovy-lang.org/support.html'>Support</a></li><li class=''><a href='/'>Contribute</a></li><li class=''><a href='https://groovy-lang.org/ecosystem.html'>Ecosystem</a></li><li class=''><a href='/blog'>Blog posts</a></li><li class=''><a href='https://groovy.apache.org/events.html'></a></li><li>
<a data-effect='st-effect-9' class='st-trigger' href='#'>Socialize</a>
</li><li class=''>
<a href='../search.html'>
<i class='fa fa-search'></i>
</a>
</li>
</ul>
</div>
</div>
</div><div id='content' class='page-1'><div class='row'><div class='row-fluid'><div class='col-lg-3'><ul class='nav-sidebar'><li><a href='./'>Blog index</a></li><li class='active'><a href='#doc'>Parsing JSON with Groovy</a></li><li><a href='#_batteries_included_experience' class='anchor-link'>Batteries included experience</a></li><li><a href='#_gradle' class='anchor-link'>Gradle</a></li><li><a href='#_maven' class='anchor-link'>Maven</a></li><li><a href='#_advanced_features' class='anchor-link'>Advanced features</a></li><li><a href='#_other_json_libraries' class='anchor-link'>Other JSON libraries</a></li><li><a href='#_integrated_query' class='anchor-link'>Integrated query</a></li><li><a href='#_quick_performance_comparison' class='anchor-link'>Quick performance comparison</a></li><li><a href='#_summary' class='anchor-link'>Summary</a></li></ul><br/><ul class='nav-sidebar'><li style='padding: 0.35em 0.625em; background-color: #eee'><span>Related posts</span></li><li><a href='./working-with-sql-databases-with'>Working with SQL databases with Groovy and GraalVM</a></li><li><a href='./comparators-and-sorting-in-groovy'>Comparators and Sorting in Groovy</a></li><li><a href='./groovy-list-processing-cheat-sheet'>Groovy List Processing Cheat Sheet</a></li><li><a href='./classifying-iris-flowers-with-deep'>Classifying Iris Flowers with Deep Learning, Groovy and GraalVM</a></li><li><a href='./zipping-collections-with-groovy'>Zipping Collections with Groovy</a></li><li><a href='./detecting-objects-with-groovy-the'>Detecting objects with Groovy, the Deep Java Library (DJL), and Apache MXNet</a></li></ul></div><div class='col-lg-8 col-lg-pull-0'><a name='doc'></a><h1>Parsing JSON with Groovy</h1><p><span>Author: <i>Paul King</i></span><br/><span>Published: 2022-07-10 02:00PM</span></p><hr/><div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p><span class="image right"><img src="img/json_logo.gif" alt="json logo" width="120"></span>
Groovy has excellent support for processing a range of structured
data formats like JSON, TOML, YAML, etc. This blog post looks at
<a href="https://www.json.org/json-en.html">JSON</a>.</p>
</div>
<div class="paragraph">
<p>There is quite good documentation on this topic as part of the
<a href="https://groovy-lang.org/processing-json.html">Groovy documentation</a>.
There are also numerous online sources for more details including
<a href="https://www.tutorialspoint.com/groovy/groovy_json.htm">Groovy - JSON tutorial</a>, <a href="https://www.baeldung.com/groovy-json">Working with JSON in Groovy</a>, and <a href="https://blog.mrhaki.com/2014/08/groovy-goodness-relax-groovy-will-parse.html">Groovy Goodness: Relax… Groovy Will Parse Your Wicked JSON</a>
to name just a few. This post does a quick summary and provides
more setup information and details about various options.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_batteries_included_experience">Batteries included experience</h2>
<div class="sectionbody">
<div class="paragraph">
<p>If you have installed the Groovy installation zip (or .msi on
windows), you will have the <code>groovy-json</code> module which
includes <code>JsonSlurper</code>, so the bulk of the examples shown here
and in the other mentioned links should work out of the box.</p>
</div>
<div class="paragraph">
<p><code>JsonSlurper</code> is the main class for parsing JSON.</p>
</div>
<div class="paragraph">
<p><span class="image"><img src="img/JsonSlurper_in_GroovyConsole.png" alt="JsonSlurper in GroovyConsole"></span></p>
</div>
<div class="paragraph">
<p>This example shows parsing JSON embedded in a string but there
are other methods for parsing files, URLs and other streams.</p>
</div>
<div class="paragraph">
<p>Another example using <code>groovysh</code>:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="console">paulk@pop-os:~$ groovysh
Groovy Shell (4.0.3, JVM: 18.0.1)
Type ':help' or ':h' for help.
----------------------------------------------------------------------------------------
groovy:000&gt; new groovy.json.JsonSlurper().parseText('{ "myList": [1, 3, 5] }').myList
===&gt; [1, 3, 5]</code></pre>
</div>
</div>
<div class="paragraph">
<p>Or using a Jupyter/BeakerX notebook:</p>
</div>
<div class="paragraph">
<p><span class="image"><img src="img/JsonSlurper_in_Jupyter_notebook.png" alt="JsonSlurper in Jupyter notebook"></span></p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_gradle">Gradle</h2>
<div class="sectionbody">
<div class="paragraph">
<p>If you are using a build tool like <a href="https://gradle.org/">Gradle</a>,
you may prefer to reference your dependencies from a dependency
repository rather than having a locally installed distribution.</p>
</div>
<div class="paragraph">
<p>Suppose you have the following test using <code>JsonSlurper</code> in
the file <code>src/test/groovy/JsonTest.groovy</code>:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="groovy">import groovy.json.JsonSlurper
import org.junit.Test
class JsonTest {
@Test
void testJson() {
def text = '{"person":{"name":"Guillaume","age":33,"pets":["dog","cat"]}}'
def json = new JsonSlurper().parseText(text)
assert json.person.pets.size() == 2
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>You can reference the relevant Groovy dependencies, in our case
<code>groovy-json</code> and <code>groovy-test</code>, in a <code>build.gradle</code> file like
below:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="groovy">apply plugin: 'groovy'
repositories {
mavenCentral()
}
dependencies {
testImplementation "org.apache.groovy:groovy-json:4.0.3" // for JsonSlurper
testImplementation "org.apache.groovy:groovy-test:4.0.3" // for tests
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Both these artifacts bring in the core <code>groovy</code> artifact
transitively, so there&#8217;s no need to reference that explicitly.</p>
</div>
<div class="paragraph">
<p>Running <code>gradle test</code> should run the tests and produce a report:</p>
</div>
<div class="paragraph">
<p><span class="image"><img src="img/Test_results_for_Class_JsonTest.png" alt="Test results for Class JsonTest"></span></p>
</div>
<div class="paragraph">
<p>You can if you prefer, use the <code>groovy-all</code> artifact like this:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="groovy">apply plugin: 'groovy'
repositories {
mavenCentral()
}
dependencies {
testImplementation "org.apache.groovy:groovy-all:4.0.3"
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>This artifact contains no jars but has all of the common
Groovy modules as transitive dependencies.</p>
</div>
<div class="paragraph">
<p>[<strong>Note</strong>: In early Groovy 4 versions you may have needed to reference Groovy as a platform, e.g.:
<code>testImplementation platform("org.apache.groovy:groovy-all:4.0.1")</code>. This is now only required when using the <code>groovy-bom</code> artifact.]</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_maven">Maven</h2>
<div class="sectionbody">
<div class="paragraph">
<p>When using the <a href="https://maven.apache.org/">Maven</a> build tool,
you would instead create a <code>pom.xml</code> like below and make
use of two plugins:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="xml">&lt;?xml version="1.0"?&gt;
&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;groupId&gt;myGroupId&lt;/groupId&gt;
&lt;artifactId&gt;groovy-json-maven&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;packaging&gt;jar&lt;/packaging&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.codehaus.gmavenplus&lt;/groupId&gt;
&lt;artifactId&gt;gmavenplus-plugin&lt;/artifactId&gt;
&lt;version&gt;1.13.0&lt;/version&gt;
&lt;executions&gt;
&lt;execution&gt;
&lt;goals&gt;
&lt;goal&gt;compileTests&lt;/goal&gt;
&lt;/goals&gt;
&lt;/execution&gt;
&lt;/executions&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
&lt;version&gt;3.0.0-M7&lt;/version&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.groovy&lt;/groupId&gt;
&lt;artifactId&gt;groovy-json&lt;/artifactId&gt;
&lt;version&gt;4.0.3&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.groovy&lt;/groupId&gt;
&lt;artifactId&gt;groovy-test&lt;/artifactId&gt;
&lt;version&gt;4.0.3&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;/project&gt;</code></pre>
</div>
</div>
<div class="paragraph">
<p>Alternatively, you could once again reference the <code>groovy-all</code>
artifact as per this alternate build file:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="xml">&lt;?xml version="1.0"?&gt;
&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;groupId&gt;myGroupId&lt;/groupId&gt;
&lt;artifactId&gt;groovy-json-maven&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;packaging&gt;jar&lt;/packaging&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.codehaus.gmavenplus&lt;/groupId&gt;
&lt;artifactId&gt;gmavenplus-plugin&lt;/artifactId&gt;
&lt;version&gt;1.13.0&lt;/version&gt;
&lt;executions&gt;
&lt;execution&gt;
&lt;goals&gt;
&lt;goal&gt;compileTests&lt;/goal&gt;
&lt;/goals&gt;
&lt;/execution&gt;
&lt;/executions&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
&lt;version&gt;3.0.0-M7&lt;/version&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.maven.surefire&lt;/groupId&gt;
&lt;artifactId&gt;surefire-junit47&lt;/artifactId&gt;
&lt;version&gt;3.0.0-M7&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.groovy&lt;/groupId&gt;
&lt;artifactId&gt;groovy-all&lt;/artifactId&gt;
&lt;type&gt;pom&lt;/type&gt;
&lt;version&gt;4.0.3&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;/project&gt;</code></pre>
</div>
</div>
<div class="paragraph">
<p>When referencing the <code>groovy-all</code> artifact, we specify that it
is a pom artifact using <code>&lt;type&gt;pom&lt;/type&gt;</code>.
We also needed to configure the surefire plugin to use JUnit4.
The <code>groovy-all</code> artifact also brings in JUnit5 support and the
surefire plugin will use that by default and not find our test.</p>
</div>
<div class="paragraph">
<p>Running the test should yield:</p>
</div>
<div class="listingblock">
<div class="content">
<pre>[<span class="blue">INFO</span>] ------------------------------------------------------
[<span class="blue">INFO</span>] T E S T S
[<span class="blue">INFO</span>] ------------------------------------------------------
[<span class="blue">INFO</span>] Running JsonTest
[<span class="blue">INFO</span>] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.36 s - in JsonTest</pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_advanced_features">Advanced features</h2>
<div class="sectionbody">
<div class="paragraph">
<p>To cater for different scenarios, <code>JsonSlurper</code> is powered by
several internal implementation classes. You don&#8217;t access these
classes directly but rather set a parser <em>type</em> when
instantiating your slurper.</p>
</div>
<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 22.2222%;">
<col style="width: 77.7778%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Type</th>
<th class="tableblock halign-left valign-top">When to use</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">CHAR_BUFFER</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Default, least-surprise parser with eager parsing of ints, dates, etc.</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">INDEX_OVERLAY</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">For REST calls, WebSocket messages, AJAX, inter process communication. Fastest parser which uses indexes to some existing char buffer.</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">CHARACTER_SOURCE</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">For handling larger JSON files.</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">LAX</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Allows comments and no quotes or single quotes in numerous situations.</p></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>Here is an example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="groovy">import groovy.json.JsonSlurper
import static groovy.json.JsonParserType.*
def slurper = new JsonSlurper(type: LAX)
def json = slurper.parseText('''{person:{'name':"Guillaume","age":33,"pets":["dog" /* ,"cat" */]}}''')
assert json.person.pets == ['dog']</code></pre>
</div>
</div>
<div class="paragraph">
<p>Note the missing quotes for the <em>person</em> key, the single quotes
for the <em>name</em> key and <em>"cat"</em> has been commented out.
These changes wouldn&#8217;t be allowed by a strict JSON parser.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_other_json_libraries">Other JSON libraries</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Groovy doesn&#8217;t require you to use the <code>groovy-json</code> classes.
You can use your favourite Java library with Groovy.
You&#8217;ll still benefit from many of Groovy&#8217;s short-hand notations.</p>
</div>
<div class="paragraph">
<p>Here&#8217;s an example using Gson:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="groovy">@Grab('com.google.code.gson:gson:2.9.0')
import com.google.gson.JsonParser
def parser = new JsonParser()
def json = parser.parse('{"person":{"name":"Guillaume","age":33,"pets":["dog","cat"]}}')
assert json.person.pets*.asString == ['dog', 'cat']</code></pre>
</div>
</div>
<div class="paragraph">
<p>Here&#8217;s an example using the Jackson JSON support:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="groovy">@Grab('com.fasterxml.jackson.core:jackson-databind:2.13.3')
import com.fasterxml.jackson.databind.ObjectMapper
def text = '{"person":{"name":"Guillaume","age":33,"pets":["dog","cat"]}}'
def json = new ObjectMapper().readTree(text)
assert json.person.pets*.asText() == ['dog', 'cat']</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_integrated_query">Integrated query</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Groovy 4 also supports language integrated query syntax, known as
GINQ or GQuery. We can use that with JSON too.</p>
</div>
<div class="paragraph">
<p>Suppose we have information in JSON format about fruits, their
prices (per 100g) and the concentration of vitamin C (per 100g):</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="json">{
"prices": [
{"name": "Kakuda plum", "price": 13},
{"name": "Camu camu", "price": 25},
{"name": "Acerola cherries", "price": 39},
{"name": "Guava", "price": 2.5},
{"name": "Kiwifruit", "price": 0.4},
{"name": "Orange", "price": 0.4}
],
"vitC": [
{"name": "Kakuda plum", "conc": 5300},
{"name": "Camu camu", "conc": 2800},
{"name": "Acerola cherries", "conc": 1677},
{"name": "Guava", "conc": 228},
{"name": "Kiwifruit", "conc": 144},
{"name": "Orange", "conc": 53}
]
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Now, suppose we are on a budget and want to select the most
cost-effective fruits to buy to help us achieve our daily
vitamin C requirements. We <em>join</em> the <em>prices</em> and <em>vitC</em> information and order by most cost-effective fruit.
We’ll select the top 2 in case our first choice isn’t in stock
when we go shopping. Our GQuery processing looks like this:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="groovy">def jsonFile = new File('fruit.json')
def json = new JsonSlurper().parse(jsonFile)
assert GQL {
from p in json.prices
join c in json.vitC on c.name == p.name
orderby c.conc / p.price in desc
limit 2
select p.name
} == ['Kakuda plum', 'Kiwifruit']</code></pre>
</div>
</div>
<div class="paragraph">
<p>We can see, for this data, Kakadu plums followed by Kiwifruit
are our best choices.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_quick_performance_comparison">Quick performance comparison</h2>
<div class="sectionbody">
<div class="paragraph">
<p>As a very crude measure of performance, <code>JsonSlurper</code> with all 4 parser types as well as Gson and Jackson were used to parse
the timezone values from
<a href="https://github.com/flowcommerce/json-reference" class="bare">https://github.com/flowcommerce/json-reference</a> and check that
the current timezone in Brisbane is the same as the timezone
in Sydney. The json file is by no means huge. It has just under
3000 lines and is under 60K in size. The best time taken
(including compilation time) after 4 runs was taken - definitely
a micro-benchmark which shouldn&#8217;t be taken too seriously, but
might be a rough guide. Just for fun, a native version of the
Groovy JsonSlurper script with the type set to INDEX_OVERLAY
was made using GraalVM. It&#8217;s timings are included too.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="shell">$ time groovy GroovyJsonIndexOverlay.groovy
real 0m1.365s
user 0m4.157s
sys 0m0.145s
$ time groovy GroovyJsonCharacterSource.groovy
real 0m1.447s
user 0m4.472s
sys 0m0.174s
$ time groovy GroovyJsonLax.groovy
real 0m1.452s
user 0m4.338s
sys 0m0.171s
$ time groovy GroovyJson.groovy
real 0m1.383s
user 0m4.050s
sys 0m0.165s
$ time groovy Gson.groovy
real 0m1.814s
user 0m5.543s
sys 0m0.209s
$ time groovy Jackson.groovy
real 0m2.007s
user 0m6.332s
sys 0m0.208s
$ time ./groovyjsonindexoverlay
real 0m0.015s
user 0m0.011s
sys 0m0.004s</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_summary">Summary</h2>
<div class="sectionbody">
<div class="paragraph">
<p>We have seen the basics of setting up our projects to parse JSON
using Groovy and some of the numerous options available to use
depending on the scenario. We also saw how to use other JSON
libraries, utilize GQuery syntax during our processing, and
looked at some very crude performance figures.</p>
</div>
</div>
</div></div></div></div></div><footer id='footer'>
<div class='row'>
<div class='colset-3-footer'>
<div class='col-1'>
<h1>Groovy</h1><ul>
<li><a href='https://groovy-lang.org/learn.html'>Learn</a></li><li><a href='https://groovy-lang.org/documentation.html'>Documentation</a></li><li><a href='/download.html'>Download</a></li><li><a href='https://groovy-lang.org/support.html'>Support</a></li><li><a href='/'>Contribute</a></li><li><a href='https://groovy-lang.org/ecosystem.html'>Ecosystem</a></li><li><a href='/blog'>Blog posts</a></li><li><a href='https://groovy.apache.org/events.html'></a></li>
</ul>
</div><div class='col-2'>
<h1>About</h1><ul>
<li><a href='https://github.com/apache/groovy'>Source code</a></li><li><a href='https://groovy-lang.org/security.html'>Security</a></li><li><a href='https://groovy-lang.org/learn.html#books'>Books</a></li><li><a href='https://groovy-lang.org/thanks.html'>Thanks</a></li><li><a href='http://www.apache.org/foundation/sponsorship.html'>Sponsorship</a></li><li><a href='https://groovy-lang.org/faq.html'>FAQ</a></li><li><a href='https://groovy-lang.org/search.html'>Search</a></li>
</ul>
</div><div class='col-3'>
<h1>Socialize</h1><ul>
<li><a href='https://groovy-lang.org/mailing-lists.html'>Discuss on the mailing-list</a></li><li><a href='https://twitter.com/ApacheGroovy'>Groovy on Twitter</a></li><li><a href='https://groovy-lang.org/events.html'>Events and conferences</a></li><li><a href='https://github.com/apache/groovy'>Source code on GitHub</a></li><li><a href='https://groovy-lang.org/reporting-issues.html'>Report issues in Jira</a></li><li><a href='http://stackoverflow.com/questions/tagged/groovy'>Stack Overflow questions</a></li><li><a href='http://groovycommunity.com/'>Slack Community</a></li>
</ul>
</div><div class='col-right'>
<p>
The Groovy programming language is supported by the <a href='http://www.apache.org'>Apache Software Foundation</a> and the Groovy community.
</p><div text-align='right'>
<img src='../img/asf_logo.png' title='The Apache Software Foundation' alt='The Apache Software Foundation' style='width:60%'/>
</div><p>Apache&reg; and the Apache feather logo are either registered trademarks or trademarks of The Apache Software Foundation.</p>
</div>
</div><div class='clearfix'>&copy; 2003-2023 the Apache Groovy project &mdash; Groovy is Open Source: <a href='http://www.apache.org/licenses/LICENSE-2.0.html' alt='Apache 2 License'>license</a>, <a href='https://privacy.apache.org/policies/privacy-policy-public.html'>privacy policy</a>.</div>
</div>
</footer></div>
</div>
</div>
</div>
</div><script src='../js/vendor/jquery-1.10.2.min.js' defer></script><script src='../js/vendor/classie.js' defer></script><script src='../js/vendor/bootstrap.js' defer></script><script src='../js/vendor/sidebarEffects.js' defer></script><script src='../js/vendor/modernizr-2.6.2.min.js' defer></script><script src='../js/plugins.js' defer></script><script src='https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.js'></script><script>document.addEventListener('DOMContentLoaded',prettyPrint)</script><script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-257558-10', 'auto');
ga('send', 'pageview');
</script>
</body></html>