Using jq in an Observable notebook#
I use the jq
language quite a lot these days, mainly because I can get ChatGPT to write little JSON transformation programs for me very quickly.
I just figured out how to run jq
in an Observable notebook.
The key is the jq-web npm package, which provides an Emscripten-compiled version of jq
itself.
You can load that in an Observable notebook with this cell:
1jq = require("jq-web")
Now you can use the jq.json(data, jqScript)
function to run a conversion against some data.
Here’s a simple example from the jq-web
documentation:
1jq.json({2 a: {3 big: {4 json: [5 'full',6 'of',7 'important',8 'things'9 ]10 }11 }12}, '.a.big.json | ["empty", .[1], "useless", .[3]] | join(" ")')
I tend to want to run recipes against data from an Observable textarea - so I add a cell like this:
1viewof input = Inputs.textarea({2 placeholder: "Paste JSON here"3})
Then I can run a jq
recipe like it and assign the result to a variable:
1output = jq.json(JSON.parse(input), '.my.jq.program.here');
I can display that output like so:
1html`<h2>Output:</h2>2<textarea style="width: 80%; height: 20em">${JSON.stringify(3 output,4 null,5 26)}</textarea>
Here’s an example of a notebook I created using jq-web
.