1

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.

Inian
  • 80,270
  • 14
  • 142
  • 161
  • 3
    Your input looks like a `JSON` string, use appropriate `JSON` parsing tool like jq – Inian Dec 30 '16 at 17:09
  • yes it is from a `JSON` however I have to get the input from a sqlite query, and then treat it, you think i could still make a script using jq tools? – alohapinilla Dec 30 '16 at 17:16
  • If you can download and install `jq`, I can provide a solution in it, and why only 3 elements in the array? What about the rest? – Inian Dec 30 '16 at 17:17
  • the three points (...) in the end means "and so on for the others", yeah sure I can install it to make it easier for you – alohapinilla Dec 30 '16 at 17:32
  • Can you post the other two entries also, because it involves `(`, need to confirm how you want to have it – Inian Dec 30 '16 at 17:38
  • althought there are only 5 entries in the exemple, there could be many more in just one line, so that is why its important the loop – alohapinilla Dec 30 '16 at 17:52
  • like i said in the question, i want to take those names in the `JSON` and transform them into URL format, anyway just with being able to extract them into an Array would be awesome – alohapinilla Dec 30 '16 at 18:04
  • that means changing the characters like `spaces into %20` and `, into %2C` – alohapinilla Dec 30 '16 at 18:06
  • Possible duplicate of [Parse JSON to array in shell script](http://stackoverflow.com/questions/38364261/parse-json-to-array-in-shell-script). There are numerous answers to your question available at this link. – davidcondrey Dec 31 '16 at 10:24
  • @davidcondrey: You could do it without the down-vote! Happy to accept it as duplicates but not at the expense of a down vote. – Inian Dec 31 '16 at 10:29
  • jq is not built in, that's why I down voted. – davidcondrey Dec 31 '16 at 10:31
  • @davidcondrey: Well that's not fair, I speifically insisted OP to download and install the tool and he was OK to do it. Doesn't mean you have to down-vote it when suggesting the right tool to get the job done, any other tools than `jq` would be a serious mistake – Inian Dec 31 '16 at 10:32
  • @davidcondrey: Refer OP's comment above _yeah sure I can install it_, Am not sure why you want to do this when OP is using a third party tool to get the proper tool for process his input. – Inian Dec 31 '16 at 10:34

1 Answers1

2

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;
Inian
  • 80,270
  • 14
  • 142
  • 161
  • it works like a charm, thanks so much, from the beginning i was thinking of using `grep, sed or awk` but this approach is a lot better – alohapinilla Dec 30 '16 at 18:35
  • @user7358525: they are not the right tools for parsing `JSON`, `jq` is your friend. Happy you found it useful. – Inian Dec 30 '16 at 18:35