This content originally appeared on DEV Community and was authored by suntong
Prefix
What is the "XSLT" equivalent for JSON?
This is the question that people had been asking more than 10 years ago. 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
, 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
, 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
. 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 thejp
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 neweasygen
that can read fromstdin
. - 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 long 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.
This content originally appeared on DEV Community and was authored by suntong
suntong | Sciencx (2021-12-30T04:33:55+00:00) What is the “XSLT” equivalent for JSON? Here is the new answer. Retrieved from https://www.scien.cx/2021/12/30/what-is-the-xslt-equivalent-for-json-here-is-the-new-answer/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.