5

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.

1
  • 1
    If it were proper json (e.g., wrapped in {}); you could use jq -r '.keys[] | .host' Commented May 30, 2013 at 16:05

2 Answers 2

10

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
Sign up to request clarification or add additional context in comments.

2 Comments

Could you add more explanation to this anwser?
@Andy it needs gnu grep.
1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.