---
title: Parse JSON Values Using Grep
description: Sometimes you need to quickly find a value in a JSON object, using grep allows you to use native functionality to get the information you need.
date: 2021-05-05
tags: JSON, grep
source: https://brianchildress.co/blog/parse-json-using-grep
---

# Parse JSON Values Using Grep

Sometimes you need to quickly find a value in a JSON object from a script or the command line. Using [grep](https://ss64.com/osx/grep.html) allows us to use native functionality (OS dependent) to get the information we need. I prefer to use native functionality as much as possible, without having to install additional tools or dependencies. The common recommendation for parsing JSON from the command line is to use [jq](https://stedolan.github.io/jq/), this works but I wanted something _simple_.

Recently I needed to retrieve a value from my AWS access keys, the response from the [AWS CLI](https://docs.aws.amazon.com/cli/latest/reference/iam/list-access-keys.html) looks something like this:

_Example JSON File:_

```json
[
    {
        "UserName": "Bob",
        "Status": "Active",
        "CreateDate": "2013-06-04T18:17:34Z",
        "AccessKeyId": "AKIAIOSFODNN7EXAMPLE"
    }
]
```

I needed to get the value for the `AccessKeyId`. Using _grep_ we can easily get the value with a command like this:

```sh
grep -o '"AccessKeyId": "[^"]*' access-keys.json | grep -o '[^"]*$'
```

Breaking this down:

First we're using the `-o` flag, short for `--only-matching`, which will select only the line that matches the following pattern.   

The pattern we define is `'"AccessKeyId": "[^"]*'` where _AccessKeyId_ is the key that we're trying to find.  

Next, we pass the file to search, e.g. _access-keys.json_.

Finally we pipe the result into a similar `grep -o` command to strip away the double-quotes and return just the value.
