~svbn/stefan.vanburen.xyz

c21f16dc2600f4cf7430e133b43d7b9aab44df99 — Stefan VanBuren 2 months ago 0a600d4
til: Add first TIL and section
6 files changed, 50 insertions(+), 1 deletions(-)

M content/blog/_index.md
A content/til/_index.md
A content/til/jq-key-exists.md
M layouts/_default/baseof.html
R layouts/{_default/list.html => blog/list.html}
A layouts/til/list.html
M content/blog/_index.md => content/blog/_index.md +1 -1
@@ 1,4 1,4 @@
+++
title = "blog posts on stefan.vanburen.xyz"
title = "Blog Posts"
description = "list of blog posts on stefan.vanburen.xyz"
+++

A content/til/_index.md => content/til/_index.md +4 -0
@@ 0,0 1,4 @@
+++
title = "TILs"
description = "list of TIL posts on stefan.vanburen.xyz"
+++

A content/til/jq-key-exists.md => content/til/jq-key-exists.md +33 -0
@@ 0,0 1,33 @@
---
title: "Use jq to see if a JSON key exists"
date: 2021-04-22T11:11:03-04:00
draft: false
---

I'm constantly using [`jq`](https://stedolan.github.io/jq/) to deal with JSON via the CLI.

Today I needed to figure out the difference between 

By default, "querying" a key via `jq` will return `null` whether the key exists and is `null`, **or** the key doesn't exist:

```fish
△ # NOTE: the following is in fish, but should be straightforward to port to other shells

△ set json '{"test": null}'

△ echo $json | jq .test
null

△ echo $json | jq .nonexistant
null
```

Instead, you can use the `jq`'s [`has`](https://stedolan.github.io/jq/manual/#has(key)) function to determine the difference:

```fish
△ echo $json | jq 'has("test")'
true

△ echo $json | jq 'has("nonexistant")'
false
```

M layouts/_default/baseof.html => layouts/_default/baseof.html +1 -0
@@ 12,6 12,7 @@
      <nav>
        <a href="/">home</a>
        <a href="/blog">blog</a>
        <a href="/til">TIL</a>
        <a href="/colophon">colophon</a>
        <a href="/now">now</a>
        <a href="/index.xml">rss</a>

R layouts/_default/list.html => layouts/blog/list.html +0 -0
A layouts/til/list.html => layouts/til/list.html +11 -0
@@ 0,0 1,11 @@
{{ define "main" }}
<h1>TIL</h1>

<ol>
  {{ range .Pages.ByDate.Reverse }}
  <li>
    <span class="nums">{{ .Date.Format "2006-01-02" }}</span> ⌁ <a href="{{.RelPermalink}}">{{ .Title }}</a>
  </li>
  {{ end }}
</ol>
{{ end }}