µjson — A Minimal JSON Parser and Transformer in Go

Oliver N
Oliver N
Jan 8 · 5 min read

µjson is a minimal JSON parser and transformer that works on unstructured (and trusted) JSON. It works by parsing input and calling the given callback function when encountering each item.

Motivation

Sometimes we just want to make some minimal changes to a JSON document or do some generic transformations without fully unmarshalling it. For example, removing blacklisted keys from response JSON. Why spend all the cost on unmarshalling into a map[string]interface{} just to immediately marshal it again. The following code is taken from StackOverflow:

With µjson, we can quickly write a simple transformation to remove "responseHeader" completely from all responses, once and forever. More on that later.

The original scenario that leads me to write the package is because of int64. When working in Go and PostgreSQL, I use int64 (instead of string) for ids because it’s more effective and has enormous space for randomly generated ids. It’s not as big as UUID, 128 bits, but still big enough for production use. In PostgreSQL, those ids can be stored as bigint and being effectively indexed. But for JavaScript, it can only process integer up to 53 bits (JavaScript has BigInt but that’s a different story, and using it will make things even more complicated).

So we need to wrap those int64s into strings before sending them to JavaScript. In Go and PostgreSQL, the JSON is {"order_id": 12345678} but JavaScript will see it as {"order_id": "12345678"}(note that the value is quoted). In Go, we can define a custom type and implement the json.Marshaler interface. But in PostgreSQL, that’s just not possible or too complicated. I wrote a service that receives JSON from PostgreSQL and converts it to be consumable by JavaScript. The service also removes some blacklisted keys or does some other transformations (for example, change orderId to order_id).

So I wrote a simple JSON parser and transformer. It can:

Important: Behavior is undefined for invalid JSON, use on trusted input only! For untrusted input, you might want to run it through json.Valid() before handing it to µjson.

Let’s see how µjson work by examples:

1. Print all keys and values in order

The callback function is called when an object key/value or an array key is encountered. It receives 3 params in order: level, key and value.

It’s important to note that key and value are provided as raw. Strings are always double-quoted. It’s there for keeping the library fast and ignoring unnecessary operations. For example, when you only want to reformat the output JSON properly; you don’t want to unquote those strings and then immediately quote them again; you just need to output them unmodified. And there are ujson.Unquote() and ujson.AppendQuote() when you need to get the original strings.

When processing arrays and objects, first the open bracket ([, {) will be provided as value, followed by its children, and the close bracket (], }). When encountering open brackets, you can make the callback function
return false to skip the object/array entirely.

 0             : {
1 "id" : 12345
1 "name" : "foo"
1 "numbers" : [
2 : "one"
2 : "two"
1 : ]
1 "tags" : {
2 "color" : "red"
2 "priority" : "high"
1 : }
1 "active" : true
0 : }

0. The simplest examples

To easily get an idea on level, key and value, here are the simplest examples:

In the first example, there is only a single boolean value. The callback function is called once with level=0, key is empty and value=true.

In the second example, the callback function is called 3 times. Two times for open and close brackets with level=0, key is empty and value is the bracketed character. The other time for the only key with level=1, key is "key" and value=42. Note that the key is quoted and you need to call usjon.Unquote() to retrieve the unquoted string.

The last example is like the second, but with an array instead. Keys are always empty inside arrays.

2. Reformat input

In this example, the input JSON is formatted with correct indentation. As processing the input key by key, the callback function reconstructs the JSON. It outputs each key/value pair in its own line, prefixed with spaces equal to the param level. There is a catch, though. Valid JSON requires commas between values in objects and arrays. So there is ujson.ShouldAddComma() for checking whether a comma should be inserted.

There is a built-in method ujson.Reconstruct() when you want to remove all the whitespaces.

3. Remove blacklisted keys

This example demonstrates removing some keys from the input JSON. The key param is compared with a pre-defined list. If there is a match, the blacklisted key and its value are dropped. The callback function returns false for skipping the entire value (which may be an object or array). Note that the list is quoted, i.e. "numbers" and "active" instead of number and active. For more advanced checking, you may want to run ujson.Unquote() on the key.

As you see in the output, "numbers" and "active" are removed.

4. Wrap int64 in string

This is the original motivation behind µjson. The following example finds keys ending with _id" ("order_id", "item_id", etc.) and converts their values from numbers to strings, by simply wrapping them in double-quotes.

For valid JSON, values are never empty. We can test the first byte of value (value[0]) to get its type:

In this case, we check value[0] within 09 to see whether it’s a number, then insert double-quotes.

After processing, the numbers in "order_id" and "item_id" are quoted as strings. And JavaScript should be happy now! 🎉 🎉

Recap

You can start using it by import "github.com/olvrng/ujson". The source code is short and easy to read. Feedback is welcome 👋

The Startup

Get smarter at building your thing. Join The Startup’s +741K followers.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +741K followers.

Oliver N

Written by

Oliver N

A software maker specialized in JavaScript and Go @ olvrng.github.io

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +741K followers.