JSON is a text-based data exchange format primarily used between browsers and servers. In this article, we are going to look at JSON encoding and decoding APIs provided by the Go.

JSON (JavaScript Object Notation) is one of the most popular data exchange formats on the web. It’s a text encoded format, which means JSON data is a string of characters written in valid JSON format. You can follow the format of JSON from RFC 7159 documentation.

The example below is a valid JSON format, however, you may need to convert this text-data into a string data-type first in order to work with JSON APIs provided by the language.

{
    "firstName": "John",
    "lastName": "Doe",
    "age": 21,
    "heightInMeters": 1.75,
    "isMale": true,
    "profile": null,
    "languages": [ "English", "French" ],
    "grades": {
        "math": "A",
        "science": "A+"
    }
}

The JSON format was created as a means to transport data between browser and server that can be easily encoded from a JavaScript object or decoded to a JavaScript object by the JavaScript engine. Hence, JSON data looks exactly like a JavaScript object but in a string format.

💡 A JSON object (like above) is a valid JavaScript object. You can paste this JSON data in the console of the JavaScript engine and JavaScript will construct a valid JavaScript object from it.

In JavaScript, we can use built-in JSON.parse(json_string) function to convert a JSON string to a JavaScript object. If we want to send a JavaScript object to the server, we can use JSON.stringify(js_object) function which returns a JSON string.

Image for post

(Google Chrome DevTools)

💡 While sending JSON data to a server, we do not need to necessarily convert JSON text-data to a string. We can transport JSON data as binary data with application/json (MIME Type) as the _Content-Type_ request header value. A server may handle this data appropriately by looking at this header value.

As we can see, the JSON format looks similar to the Map data structure (_Object_ in case of JavaScript or _Dictionary_ in case of Python) with keys and values. A JSON key is strictly a string, while the value can be any supported data type.

JSON format supports primarily 6 data types viz. string, number, boolean, null, array and object. In JavaScript, all these data types are valid data types, however, that might not be the case in other languages and a program may need to coerce the non-supported data type to a supported value.

The number data type in JavaScript can represent both signed/unsigned integers and floating-point numbers. The null isn’t actually a data type but an empty value that can translate into an empty value that a language supports for example nil in the case of Go.

[
    "John Doe",
    21,
    [ "English", "French" ],
    {
        "math": "A",
        "science": "A+"
    }
]

The example above is also a valid JSON format. A JSON can represent a dictionary or a list and items can be any supported data types. When this JSON data is decoded, it will get converted to an array in JavaScript or any supported List data structure in other languages, and vice-versa.

In Go, the closest we can get to JSON representation is through map or struct data types. Both of these data structures can store complex data in key:value pairs. Keep in mind, they are designed to do more than just that.

While decoding a JSON string, we need a container that can store the JSON data in a valid data type like a map or a struct so that we can access JSON data idiomatically. In a similar manner, while encoding JSON data, we need an object like a map or a struct that can be converted to valid JSON format.

Let’s dive into APIs provided by the Go to encode and decode JSON data. Go provides most of the JSON encoding/decoding APIs from the encoding/json package.

#golang #json #developer #api

Working with JSON in Go
1.80 GEEK