I have searched and seen many question similar to mine but none of the answer satisfy me.

My scenario is: i am writing a script which will going to run on the server. i have a curl command which return a json body and i want to pick all the values with specific key and i can not use any external tool for parsing json like jq and i dont even have python there just simple bash shell.

so my question is how i do it with using just default shell?

there are going to multiple values for that same key and i want to make the array of these value.

an example json:-


if say 'name' then i want array("suneel" "adi")

Note: No use of external tool and with just bash commands.

  • I guess this is not Ubuntu, as you would have python. Also, grep is an external tool... as well as awk or sed. Please clarify.
    – pLumo
    Commented Aug 21, 2019 at 11:09
  • @pLumo I just learned about jq and installed it an hour ago. (Before reading this question). To say jq it can't be used makes the question above sound like a homework project. Python is not installed on all systems but GNU utilities and Bash usually are. If this is a homework project I think that's how the prof made the rules of engagement. Commented Aug 21, 2019 at 11:51
  • @pLumo you can assume any machine with awk , sed and grep and do not have python..and cannot install any other package..this is the condition for my working. i already tried to talk with bosses for jq.but no change in the condition. Commented Aug 21, 2019 at 14:15
  • i already know about 'jq' its a great tool and it took me hardly 10 min to make this work with jq.....but the condition is no jq... Commented Aug 21, 2019 at 14:16

3 Answers 3


A bash function like below can be used:

function jsonValue() {
awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'$KEY'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p

I've saved this function as jsonVal and then sourced this file using source jsonVal. You can very well use it within your script.

It expects two arguments. First argument is the property name. If you need all values, skip second argument. If specific value is needed, you can add the second argument as shown below.

[root@localhost Desktop]# cat data.json | jsonValue id
[root@localhost Desktop]# cat data.json | jsonValue id 1
[root@localhost Desktop]# cat data.json | jsonValue id 2
[root@localhost Desktop]# cat data.json | jsonValue name
[root@localhost Desktop]# cat data.json | jsonValue name 1
[root@localhost Desktop]# cat data.json | jsonValue name 2
[root@localhost Desktop]#

Hope this helps.

  • 1
    If a value contains : , } this seems to break :(
    – Phill
    Commented May 18, 2020 at 4:56

Using tools other than proper json parsers will always be prone to errors or security issues.

Your best option: If you don't have the tools you need to do your work, ask your IT/Server admin to install them.

Anyways, the following will work at least for your example:

Using grep -P:

$ curl ... | grep -Po '"name":"\K[^"]*'

With normal grep:

$ curl ... | grep -o '"name":"[^"]*' | cut -d'"' -f4

If you have "name" somewhere outside of "people" which you do not want, this will obviously fail.


This is a little overkill but I already have an app that reads gmail meta data in order to Trash daily backup files compressed and attached to gmail messages. The file format is similar to yours:


{u'internalDate': u'1541947153000', 
u'historyId': u'1517343', 
    {u'mimeType': u'multipart/mixed',
     u'headers': [
        {u'name': u'Return-Path', u'value': u'<[email protected]>'}, 
        {u'name': u'Received', u'value': u'from alien (node-ISP. [IPv6.Address])        by smtp.gmail.com with ESMTPSA id x184-v6sm1211487pfx.42.2018.        for <[email protected]>        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);        Sun, 11 Nov 2018 06:39:35 -0800 (PST)'}, 
        {u'name': u'Message-ID', u'value': u'<[email protected]>'}, 
        {u'name': u'From', u'value': u'root <[email protected]>'}, 
        {u'name': u'X-Google-Original-From', u'value': u'"root" <[email protected]>'}, 
        {u'name': u'Received', u'value': u'by alien (sSMTP sendmail emulation); Sun, 11 Nov 2018 07:39:13 -0700'}, 
        {u'name': u'Date', u'value': u'Sun, 11 Nov 2018 07:39:13 -0700'}, 
        {u'name': u'to', u'value': u'[email protected]'}, 
        {u'name': u'Subject', u'value': u'Daily-alien-Ubuntu-16.04-Backup-2018-11-11-Sunday.tar.gz.64'}
u'snippet': u'', 
u'sizeEstimate': 18340047, 
u'threadId': u'1670336bc9ac099d', 
u'labelIds': [u'IMPORTANT', u'SENT', u'Label_12'], 
u'id': u'1670336bc9ac099d'

Here is the code snippet from the large bash script:

GrepLine () {


    Srch2="{u'name': u'Date', u'value': u'"
    Srch3="{u'name': u'Subject', u'value': u'" # sometimes lower-case "subject"
    Srch4="u'sizeEstimate': "
    Srch5="u'labelIds': \[u'"
    Srch6="u'id': u'" 

    # grep allows fast search (compiled C) and supports case insensitivity
    HeaderDate=$(grep -oiPm1 "$Srch2\K[^$End1]+" <<< "$plLine")
    HeaderSubject=$(grep -oiPm1 "$Srch3\K[^$End3]+" <<< "$plLine")
    [[ $HeaderSubject == "" ]] && HeaderSubject="GREP ERROR: Subject blank"

    Size=$(grep -oiPm1 "$Srch4\K[^$End4]+" <<< "$plLine")

    LabelIds=$(grep -oiPm1 "$Srch5\K[^$End5]+" <<< "$plLine")
    LabelIds="${LabelIds//\', u\'/$LabelSep}"
    let TrimLen--
    LabelIds="${LabelIds:0:TrimLen}" # Remove trailing '
    # Convert "Label_12" to "Backup", etc.

    MessageId=$(grep -oiPm1 "$Srch6\K[^$End1]+" <<< "$plLine")

    # Some email messages have 50 extra spaces or more.
    HeaderSubject="${HeaderSubject%% }"

    # Convert HTML to ASCII, eg: Can&#39;t wear the same one
    # Replace external command: Line=$(sed 's/&amp;/\&/g; s/&lt;/\</g; 
    # s/&gt;/\>/g; s/&quot;/\"/g; s/&#39;/\'"'"'/g; s/&ldquo;/\"/g; 
    # s/&rdquo;/\"/g;' <<< "$Line") -- With faster builtin commands.
    HeaderSubject="${HeaderSubject//&nbsp;/ }"
    HeaderSubject="${HeaderSubject//&ldquo;/'"'}" # TODO: ASCII/ISO for opening quote
    HeaderSubject="${HeaderSubject//&rdquo;/'"'}" # TODO: ASCII/ISO for closing quote

    # Truncate subject to 80 characters or whatever variable is set to
    HeaderSubject="${HeaderSubject//|/?}"       # Strip out Yad array separators
    HeaderSubject="${HeaderSubject//\--/==}"    # Yad doesn't like --
#    HeaderSubject="${HeaderSubject//\'/\"}"   # Yad doesn't like
    [[ $HeaderSubject == "" ]] && HeaderSubject="PARSING ERROR: Subject blank"

    # Convert date to YAD format
    HeaderDate=$(date -d "$HeaderDate" +'%Y-%m-%d')

} # GrepLine

Not the answer you're looking for? Browse other questions tagged .