Parsing JSON in shell script with regex

Tags: json shell
Question!

I need to parse the jsonarray by using regex. My json is

"keys": [
      {
        "host": "example.com"       
      },
      {
        "host": "example.net"
      }
    ]

I need to get the two hosts values.



Answers
When you want to extract text, grep is your friend:

grep -Po '(?<="host": ")[^"]*' myjsonFile

For example:

kent$  echo '"keys": [
      {
        "host": "example.com"       
      },
      {
        "host": "example.net"
      }
    ]'|grep -Po '(?<="host": ")[^"]*'

example.com
example.net
By : Kent


The following regex will grab your host values using a non-greedy kleene wildcard:

/"host":\s"(.*?)"/


This video can help you solving your question :)
By: admin