5

How can i do dictionary structure in shell? My aim is to generate random words. Ex. dirty fish, good book, ugly piano or pesante pasta, giallo cane... Its js code look like this

    words ={

"italian" :
{
    "name" :
            [
             "gatto", 
             "cane", 
             "pasta", 
             "telefono", 
             "libro"
             ],

    "adjective" : 
            [
             "pesante", 
             "sottile", 
             "giallo", 
             "stretto",      
             ]
},
"english" :
{
    "name" : 
            [
             "fish", 
             "book",
             "guitar",
             "piano",
             ],     
    "adjective" :
            [
              "dirty",
              "good",
              "ugly",
              "great",   
             ]
}}

I want this:

words[english][adjective][1]
>> good
3
  • 8
    Shell is the wrong language for this. Some shells support associative arrays, but few, if any, support nesting one array in another. Commented Nov 25, 2014 at 12:53
  • See my answer, jq is a shell tool and can parse JSON easyly Commented Nov 25, 2014 at 13:15
  • 1
    Possible duplicate of Parsing JSON with Unix tools Commented Jul 8, 2019 at 20:11

2 Answers 2

24

The itself can't store complicated Data Structures, but like most of the time in shell, you can use external tools, I demonstrate here 6 different solutions, all in Unix* like shells:

First, your JSON is broken, this is a valid version in file.js :

{
   "italian" : {
      "name" : [
         "gatto",
         "cane",
         "pasta",
         "telefono",
         "libro"
      ],
      "adjective" : [
         "pesante",
         "sottile",
         "giallo",
         "stretto"
      ]
   },
   "english" : {
      "name" : [
         "fish",
         "book",
         "guitar",
         "piano"
      ],
      "adjective" : [
         "dirty",
         "good",
         "ugly",
         "great"
      ]
   }
}

Using

$ jq '.english.adjective[1]' file.js

Output:

good

Playing with jq and RANDOM shell variable :

$ echo $(
    jq ".english.adjective[$((RANDOM%4))], .english.name[$((RANDOM%4))]" file.js
)
"great" "piano"

jq, see the tutorial.

Using

$ rhino<<EOF 2>/dev/null
hash = $(<file.js)
print(hash.english.adjective[1])
EOF

Output:

...
good

Using

$ node<<EOF
hash = $(<file.js)
console.log(hash.english.adjective[1])
EOF

Output :

good

Using

Let's parse the DS in a perl command line :

$ perl -MJSON -0lnE '
    $words = decode_json $_;
    say $words->{english}->{adjective}->[1]
' file.js

Output:

good

Using

$ python<<EOF
import json
json_data = open('file.js')
data = json.load(json_data)
json_data.close()
print(data['english']['adjective'][1])
EOF

Output:

good

Using

$ ruby<<EOF
require 'json'
file = File.read('file.js')
data = JSON.parse(file)
print(data['english']['adjective'][1])
EOF

Output:

good
Sign up to request clarification or add additional context in comments.

Comments

1

Using pure 3.2+ without dependencies (such as jq, python, grep, etc.):

source <(curl -s -L -o- https://github.com/lirik90/bashJsonParser/raw/master/jsonParser.sh)
JSON=$(minifyJson "$JSON")
echo "Result is: $(parseJson "$JSON" english adjective 1)"

Output:

Result is: good

Try it.

1 Comment

Please don't post duplicate answers. If you find questions which are similar enough to warrant the same answer, leave a comment to suggest closing as duplicate.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.