Introducing jaycue – jq in your JS
Tags: work, javascript, jaycue, jq
Published: 2020-03-11 16:22:00

---
title: Introducing jaycue – jq in your JS
published: true
date: 2020-03-11 16:09:42 UTC
tags: work,javascript,jaycue,jq
canonical_url: https://brian.olore.net/wp/2020/03/introducing-jaycue-jq-in-your-js/
---

![jaycue logo](https://raw.githubusercontent.com/olore/jaycue/master/media/logo.png)<figcaption style="text-align: center">Shout out to my son for making the logo! Nice work Joseph!</figcaption>

Previously, I wrote about [how useful lodash](https://dev.to/olore/working-with-json-in-javascript-29l8) is when working with JSON in JavaScript. Then we took a look at how to deal with JSON from the command line using the [power of jq.](https://dev.to/olore/jq-the-json-cli-tool-2om0)

It should come as no surprise where I’m headed here: 
  **I want the power of jq filters in my JavaScript code**!

## Off we go

Instead of doing what I normally do: googling to see if it existed, then getting depressed that it wasn’t unique, I decided to just do it. I thought it would be a fun way to do a little test-driven development, which I kinda miss doing.

I figured that if I could find a way to write a test and run it against my code, grab the output, run it against jq proper, then compare the results, it’d prove that my code was doing what it should.

I’ve executed shell commands from Node.js before, so it seemed doable.

```javascript
> const exec = require('child_process').execSync;
undefined
> let command = `echo '{"foo": 123}' | jq ".foo"`
undefined
> exec(command).toString();
'123\n'
```

Cool. That was easy enough. So, in an attempt to make it more generic for a utility method that I can use in the tests, I should be able to do something like:

```javascript
> let command = `echo '${json}' | jq "${filter}"`
```

and pass any JSON string and any filter, then collect up the result, and compare!  
  
My goal is to make a function that would allow me to perform an expectation like:

```javascript
expect(myjq(jsonObject, filter))
    .toEqual(jq(jsonObject, filter));
```

Check it out here: [https://github.com/olore/jaycue/blob/master/tests/test-helper.js#L22](https://github.com/olore/jaycue/blob/master/tests/test-helper.js#L22)

## Now that we can test, let’s code!

Once that was in place, it was a matter of choosing which types of filters I wanted to support first. From the documentation, there are A LOT. I decided to go after the ones I would use most often, and expand from there. In particular, there was a single use case I wanted to solve. But to get there, I had to start with the basics.

The **Basic** filters could be serviced by [lodash.get](https://www.npmjs.com/package/lodash.get). For instance, both `versionString` and `.versionString` will work with

```javascript
lodash.get("versionString");
```

I would just need to chop off the leading period. Something like “name.firstName” would also work with [lodash.get](https://www.npmjs.com/package/lodash.get).

From there, I started down the list of [Basic Filters](https://stedolan.github.io/jq/manual/#Basicfilters). Adding in Array indexing and eventually Select filtering, which was the last piece of the puzzle for the use case I had in mind.   
  
Here it is:

```json
{
  "applicants": [
    {
      "identities": [
        {
          "type": null,
          "number": null
        },
        {
          "type": "SSN",
          "number": "987651234"
        }
      ]
    }
  ]
}

```

We need to get the `number` whose type is “SSN”, if it exists.  
We had code that looked like this:

```javascript
const ssn = get(data, 'submit.applicants[0].identities', [])
  .reduce((accum, identity) => {
    if (identity.type === 'SSN' && identity.number) {
      accum = identity.number;
    }
    return accum;
  }, '');

```

Whereas, a jq command like this would work:

```bash
cat test.json | jq '.applicants[0].identities[] | select(.type=="SSN") .number'
```

Now that we had **select** functionality, the above JavaScript code could be replaced with:

```javascript
const ssn = jq(data, '.applicants[0].identities[] | select(.type=="SSN") .number');
```

And just like that, we’re successful!

## Wrapping up

Please go [install jaycue](https://www.npmjs.com/package/jaycue) and let me know what you think:

```bash
npm install jaycue
```

Checkout some more great JSON tools:
[node-jq](https://www.npmjs.com/package/node-jq)
[JSONView (Chrome)](https://chrome.google.com/webstore/detail/jsonview/chklaanhfefbnpoihckbnefhakgolnmc)
[JSONovich (Firefox)](https://addons.mozilla.org/en-US/firefox/addon/jsonovich/)


A big shout-out to my 13yo son for making the jaycue logo! It was a true, family effort. Nice work Joseph!

I hope you have found this useful. I’d love to hear about what features of jq you think should be added next. And as always, I’d love to have you contribute to the [jaycue project](https://github.com/olore/jaycue)!

The post [Introducing jaycue – jq in your JS](https://brian.olore.net/wp/2020/03/introducing-jaycue-jq-in-your-js/) appeared first on [The Brian Olore Story](https://brian.olore.net).