What is the "XSLT" equivalent for JSON? Here is the new answer
Tags: json, transform, easygen, generator
Published: 2021-12-30 04:33:55

---
title: What is the "XSLT" equivalent for JSON? Here is the new answer
series: transforming json data to be more presentable to human beings
published: true
description: Time to give the 10+ year old question a new answer
tags: json,transform,easygen,generator
cover_image: https://www.imd.org/contentassets/11a6ae4d75ce4d4a83c9a8e87b3944e4/iai_07-18_big-teaser758x334.jpg
---

### Prefix

> _What is the "XSLT" equivalent for JSON?_  

This is the question that people had been asking [more than 10 years ago](https://stackoverflow.com/questions/1618038/xslt-equivalent-for-json). By then the `xslt` is still the number one answer.  
It's high time to take a look at the new kids in town now.

### What is "XSLT"?

XSLT stands for XSL Transformation. It is used to transform XML documents into into various other types of document (like transforming XML into HTML).

It's most important purpose is transform data, from XML form into a format more presentable to human beings.  
XSL is to XML is similar as CSS is to HTML. XSLT can be viewed as the rendering engine.

### What are the new choices now?

The second choice in the above answer had been [`jq`](https://stedolan.github.io/jq/), and I agree that it is very powerful.

> jq is like sed for JSON data - you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text. There are install packages for different OS.

But comparing to `jsonpath` it is more difficult to learn to most people, which brings us to the first tool to recommend this time -- [`jp`](https://github.com/go-jsonfile/jp/), because

`jp` is as simple as `jsonpath` but as powerful as `jq`.

### How about the rendering engine part?

The problem of both `jq` or `jp` is that although they can do data projection in any direction (doing data filtering in any way), their purpose are still focusing on data transformation, but when talking about rendering `json` data into a format more presentable to human beings, then there is no better tools than [`easygen`](https://github.com/go-easygen/easygen/). In fact, the `easygen` is a universal code/text generator that not only works on `json` data but it can take in data defined in YAML format as well. It can be used as any text (or html, or any other forms) generator for arbitrary purposes with arbitrary data and templates. It is built on top of the powerful Go template engine.

### To put them together

So the other day, I

- download `json` data via API
- filter the `json` data with the [`jp`](https://github.com/go-jsonfile/jp/releases/tag/v0.2.2) that supports filtering by regular expression (_hint: do you know any json filter that when given a video's file name, can tell a tv-series apart from a movie?_)
- then present the `json` data in human friendly format with the new `easygen` that can read from `stdin`.
- in the human friendly form, I need to convert the long number of size in bytes to the size in MB and GB, and I had to search for how to do calculation in Go template, but then was _delighted_ to realized that `easygen` _already_ supports the [calculation in Go template](https://github.com/go-easygen/easygen/issues/25#cal) long time ago.  

I'll give a more concrete example in the next article.

To me now,

`easygen` is to `json` what `xslt` is to `xml`, but much more powerful and versatile.

### Credit

The cover image is obtained from [imd.org](https://www.imd.org/contentassets/11a6ae4d75ce4d4a83c9a8e87b3944e4/iai_07-18_big-teaser758x334.jpg).