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.

13
  • 3
    Your input looks like a JSON string, use appropriate JSON parsing tool like jq Commented Dec 30, 2016 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? Commented Dec 30, 2016 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? Commented Dec 30, 2016 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 Commented Dec 30, 2016 at 17:32
  • Can you post the other two entries also, because it involves (, need to confirm how you want to have it Commented Dec 30, 2016 at 17:38

1 Answer 1

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

2 Comments

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
@user7358525: they are not the right tools for parsing JSON, jq is your friend. Happy you found it useful.

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.