alohapinilla
alohapinilla

Reputation: 55

Bash extract string from JSON output and save it into Array

I'm trying to extract several substrings from one string, i want to save those substrings in an Array, so i thought of using a loop but i'm really new into bash scripting.

The original string is something like:

   {  
   "groups":[  
      {  
         "group":"",
         "favourites":[  
            {  
               "category":"",
               "longitude":1.0812308,
               "latitude":49.4304904,
               "test":"",
               "color":0,
               "name":"Place Henri Gadeau de Kerville, Centre Ville Rive Gauche"
            },
            {  
               "category":"",
               "longitude":1.940849,
               "latitude":48.57248,
               "test":"",
               "color":0,
               "name":"Rue Charles de Gaulle, Saint-Arnoult-en-Yvelines"
            },
            {  
               "category":"",
               "longitude":1.9358053,
               "latitude":48.570592,
               "test":"",
               "color":0,
               "name":"Rue des Remparts, Saint-Arnoult-en-Yvelines"
            },
            {  
               "category":"",
               "longitude":1.0856655,
               "latitude":49.4291327,
               "test":"",
               "color":0,
               "name":"Rue Marie Duboccage (Saint-Sever), Rouen"
            },
            {  
               "category":"",
               "longitude":1.0845655,
               "latitude":49.4251747,
               "test":"",
               "color":0,
               "name":"Rue Octave Crutel, Rouen"
            }
         ],
         "color":0
      }
   ]
}

And the desired output is an Array with the names in url mode from the "name" tag like:

Array[0]=Place%20Henri%20Gadeau%20de%20Kerville%2C%20Centre%20Ville%20Rive%20Gauche;
Array[1]=Rue%20Charles%20de%20Gaulle%2C%20Saint-Arnoult-en-Yvelines;
Array[2]=Rue%20des%20Remparts%2C%20Saint-Arnoult-en-Yvelines;
Array[3]=Rue%20Marie%20Duboccage%20(Saint-Sever)%2C%20Rouen;
Array[4]=Rue%20Octave%20Crutel%2C%20Rouen;
...

in order to save the values of the Array in another file and then using them. I've tried with grep

grep -o '^\"name\":.*\},$' $var 

but I cannot get a good result.

Upvotes: 1

Views: 881

Answers (1)

Inian
Inian

Reputation: 85895

In bash a simple loop, using the JSON parsing tool jq and process-substitution

#!/bin/bash

jsonArray=()
while IFS= read -r line
do
    jsonString="${line// /%20}"           # Replace blank-spaces with '%20'
    jsonString="${jsonString//,/%2C}"     # Replace ',' with empty '%2C'
    jsonString+=";"                       # Append a ';' at end of string
    jsonArray+=("$jsonString")            # Add it to the array
done< <(jq -r '.groups[].favourites[].name' newfile)

printf "%s\n" "${jsonArray[@]}"           # "${jsonArray[0]}","${jsonArray[1]}"...

In my example I have used the string in a file, for your case replace the line

done< <(jq -r '.groups[].favourites[].name' newfile)

with the actual command producing the JSON output as

done < <( json-cmd | jq -r '.groups[].favourites[].name')

On running the script

$ bash script.sh
Place%20Henri%20Gadeau%20de%20Kerville%2C%20Centre%20Ville%20Rive%20Gauche;
Rue%20Charles%20de%20Gaulle%2C%20Saint-Arnoult-en-Yvelines;
Rue%20des%20Remparts%2C%20Saint-Arnoult-en-Yvelines;
Rue%20Marie%20Duboccage%20(Saint-Sever)%2C%20Rouen;
Rue%20Octave%20Crutel%2C%20Rouen;

Upvotes: 2

Related Questions