Skip to content

Instantly share code, notes, and snippets.

@kurtroberts
Last active July 6, 2018 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurtroberts/c9d53a1c1f96631cb6cbd09b09c8af2d to your computer and use it in GitHub Desktop.
Save kurtroberts/c9d53a1c1f96631cb6cbd09b09c8af2d to your computer and use it in GitHub Desktop.
I really, really love `jq`

Wow, that's a lot of JSON for my bio...

$ cat data/bio/kurt-roberts.json
{"sys":{"space":{"sys":{"type":"Link","linkType":"Space","id":"----------"}},"id":"------------","type":"Entry","createdAt":"-------","updatedAt":"-------","environment":{"sys":{"id":"master","type":"Link","linkType":"Environment"}},"revision":2,"contentType":{"sys":{"type":"Link","linkType":"ContentType","id":"bio"}},"locale":"en-US"},"fields":{"name":"Kurt Roberts","title":"Chief Creative Technologist?","description":"Senior engineering and design team lead with 15+ years of experience creating solutions for high-profile brands such as Coca-Cola, BlueCross BlueShield America, General Mills and DirecTV, and powerful forces for change such as Facing History and Ourselves, The Washington Area Women's Foundation, Humane Society of the United States and Planned Parenthood.","pastClients":["Coca-Cola","BlueCross BlueShield America","General Mills","DirecTV","Facing History and Ourselves","Humane Society of the United States","Planned Parenthood."],"socialNetworks":[{"sys":{"space":{"sys":{"type":"Link","linkType":"Space","id":"------"}},"id":"-------","type":"Entry","createdAt":"--------","updatedAt":"--------------","environment":{"sys":{"id":"master","type":"Link","linkType":"Environment"}},"revision":2,"contentType":{"sys":{"type":"Link","linkType":"ContentType","id":"socialChannel"}},"locale":"en-US"},"fields":{"name":"Kurt's LinkedIn","url":"https://www.linkedin.com/in/kurtproberts/"}},{"sys":{"space":{"sys":{"type":"Link","linkType":"Space","id":"--------------"}},"id":"-----------","type":"Entry","createdAt":"----------","updatedAt":"----------","environment":{"sys":{"id":"master","type":"Link","linkType":"Environment"}},"revision":2,"contentType":{"sys":{"type":"Link","linkType":"ContentType","id":"socialChannel"}},"locale":"en-US"},"fields":{"name":"Kurt's Github","url":"https://github.com/kurtroberts"}}],"slug":"kurt-roberts"}} 

Let's at least pretty print it...

$ cat data/bio/kurt-roberts.json | jq '.'
{
  "sys": {
    "space": {
      "sys": {
        "type": "Link",
        "linkType": "Space",
        "id": "--------"
      }
    },
    "id": "---------",
    "type": "Entry",
    "createdAt": "--------",
    "updatedAt": "--------",
    "environment": {
      "sys": {
        "id": "master",
        "type": "Link",
        "linkType": "Environment"
      }
    },
    "revision": 2,
    "contentType": {
      "sys": {
        "type": "Link",
        "linkType": "ContentType",
        "id": "bio"
      }
    },
    "locale": "en-US"
  },
  "fields": {
    "name": "Kurt Roberts",
    "title": "Chief Creative Technologist?",
    "description": "Senior engineering and design team lead with 15+ years of experience creating solutions for high-profile brands such as Coca-Cola, BlueCross BlueShield America, General Mills and DirecTV, and powerful forces for change such as Facing History and Ourselves, The Washington Area Women's Foundation, Humane Society of the United States and Planned Parenthood.",
    "pastClients": [
      "Coca-Cola",
      "BlueCross BlueShield America",
      "General Mills",
      "DirecTV",
      "Facing History and Ourselves",
      "Humane Society of the United States",
      "Planned Parenthood."
    ],
    "socialNetworks": [
      {
        "sys": {
          "space": {
            "sys": {
              "type": "Link",
              "linkType": "Space",
              "id": "--------"
            }
          },
          "id": "--------",
          "type": "Entry",
          "createdAt": "--------",
          "updatedAt": "--------",
          "environment": {
            "sys": {
              "id": "master",
              "type": "Link",
              "linkType": "Environment"
            }
          },
          "revision": 2,
          "contentType": {
            "sys": {
              "type": "Link",
              "linkType": "ContentType",
              "id": "socialChannel"
            }
          },
          "locale": "en-US"
        },
        "fields": {
          "name": "Kurt's LinkedIn",
          "url": "https://www.linkedin.com/in/kurtproberts/"
        }
      },
      {
        "sys": {
          "space": {
            "sys": {
              "type": "Link",
              "linkType": "Space",
              "id": "--------"
            }
          },
          "id": "--------",
          "type": "Entry",
          "createdAt": "--------",
          "updatedAt": "--------",
          "environment": {
            "sys": {
              "id": "master",
              "type": "Link",
              "linkType": "Environment"
            }
          },
          "revision": 2,
          "contentType": {
            "sys": {
              "type": "Link",
              "linkType": "ContentType",
              "id": "socialChannel"
            }
          },
          "locale": "en-US"
        },
        "fields": {
          "name": "Kurt's Github",
          "url": "https://github.com/kurtroberts"
        }
      }
    ],
    "slug": "kurt-roberts"
  }
}

Since Contentful is being used read-only, we don't need any of the sys metadata...

$ cat data/bio/kurt-roberts.json | jq '.fields'
{
  "name": "Kurt Roberts",
  "title": "Chief Creative Technologist?",
  "description": "Senior engineering and design team lead with 15+ years of experience creating solutions for high-profile brands such as Coca-Cola, BlueCross BlueShield America, General Mills and DirecTV, and powerful forces for change such as Facing History and Ourselves, The Washington Area Women's Foundation, Humane Society of the United States and Planned Parenthood.",
  "pastClients": [
    "Coca-Cola",
    "BlueCross BlueShield America",
    "General Mills",
    "DirecTV",
    "Facing History and Ourselves",
    "Humane Society of the United States",
    "Planned Parenthood."
  ],
  "socialNetworks": [
    {
      "sys": {
        "space": {
          "sys": {
            "type": "Link",
            "linkType": "Space",
            "id": "--------"
          }
        },
        "id": "--------",
        "type": "Entry",
        "createdAt": "--------",
        "updatedAt": "--------",
        "environment": {
          "sys": {
            "id": "master",
            "type": "Link",
            "linkType": "Environment"
          }
        },
        "revision": 2,
        "contentType": {
          "sys": {
            "type": "Link",
            "linkType": "ContentType",
            "id": "socialChannel"
          }
        },
        "locale": "en-US"
      },
      "fields": {
        "name": "Kurt's LinkedIn",
        "url": "https://www.linkedin.com/in/kurtproberts/"
      }
    },
    {
      "sys": {
        "space": {
          "sys": {
            "type": "Link",
            "linkType": "Space",
            "id": "--------"
          }
        },
        "id": "--------",
        "type": "Entry",
        "createdAt": "--------",
        "updatedAt": "--------",
        "environment": {
          "sys": {
            "id": "master",
            "type": "Link",
            "linkType": "Environment"
          }
        },
        "revision": 2,
        "contentType": {
          "sys": {
            "type": "Link",
            "linkType": "ContentType",
            "id": "socialChannel"
          }
        },
        "locale": "en-US"
      },
      "fields": {
        "name": "Kurt's Github",
        "url": "https://github.com/kurtroberts"
      }
    }
  ],
  "slug": "kurt-roberts"
}

Maybe we make an object a real human wants to deal with (and that isn't streaming a ton of useless information to our web app)

$ cat data/bio/kurt-roberts.json | jq '. | { name: .fields.name, 
      title: .fields.title?, description: .fields.description?, 
      slug: .fields.slug, pastClients: .fields.pastClients?, 
      socialNetworks : [.fields.socialNetworks[].fields] }'
{
  "name": "Kurt Roberts",
  "title": "Chief Creative Technologist?",
  "description": "Senior engineering and design team lead with 15+ years of experience creating solutions for high-profile brands such as Coca-Cola, BlueCross BlueShield America, General Mills and DirecTV, and powerful forces for change such as Facing History and Ourselves, The Washington Area Women's Foundation, Humane Society of the United States and Planned Parenthood.",
  "slug": "kurt-roberts",
  "pastClients": [
    "Coca-Cola",
    "BlueCross BlueShield America",
    "General Mills",
    "DirecTV",
    "Facing History and Ourselves",
    "Humane Society of the United States",
    "Planned Parenthood."
  ],
  "socialNetworks": [
    {
      "name": "Kurt's LinkedIn",
      "url": "https://www.linkedin.com/in/kurtproberts/"
    },
    {
      "name": "Kurt's Github",
      "url": "https://github.com/kurtroberts"
    }
  ]
}

How much data did we save again?

$ cat data/bio/kurt-roberts.json | wc -c
    1995
$ cat data/bio/kurt-roberts.json | jq '. | { name: .fields.name, title: .fields.title?, description: .fields.description?, slug: .fields.slug, pastClients: .fields.pastClients?, socialNetworks : [.fields.socialNetworks[].fields] }' | wc -c
     912

almost 1K ?!?!?!?!?!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment