Chapter 6. Working with JSON in Groovy

In this chapter, we will cover:

  • Parsing JSON messages with JsonSlurper
  • Constructing JSON messages with JsonBuilder
  • Modifying JSON messages
  • Validating JSON messages
  • Converting JSON message to XML
  • Converting JSON message to Groovy Bean
  • Using JSON to configure your scripts

Introduction

JSON (JavaScript Object Notation) (http://www.json.org/) is a data interchange format, similar in scope to XML or CSV. It uses a very lightweight serialization form and works very well for exchanging data between a web client (a browser) and a remote server, but it is not limited to only that.

JSON is also a subset of the object literal notation of JavaScript. Being a subset of JavaScript implies that a JSON payload can be processed by the language natively. This is the main reason why JSON has become such a popular format for transferring serialized data between a remote API (often a RESTful API) and a JavaScript client.

But JSON is also very lightweight—especially if we compare it to the very chatty XML, the dethroned king of serialization—making it a suitable candidate for a general purpose data processing format (if you can live with some of its limitations such as the lack of namespace or poor complex data types support).

This chapter's recipes introduce the reader to Groovy native support for reading and producing JSON. We also look at some more advanced recipes such as conversion from and to XML and Groovy Beans.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.172.56