Parsing and Editing a json file with bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing and Editing a json file with bash script
# 1  
Old 04-20-2016
Parsing and Editing a json file with bash script

I am trying to automate editing of a json file using bash script.

The file I initially receive is

Code:
{
  "appMap": {
    "URL1": {
      "name": "a"
    },
    "URL2": {
      "name": "b"
    },
  "URL3": {
      "name": "c"
    },
}

WHat I would like to do is replace URL1,URL2,URL3 with URL4,URL5,URL6.

I do not want to use a sed command like:

Code:
sed -i "s/URL1/URL4/g" <file>

But I want to parse through it and change values based on "name".

Is this something that is possible using bash ? Or do I need to look into perl or python ?
# 2  
Old 04-20-2016
Hello Juniad Subhani,

Could you please try following and let me know if this helps.
Code:
perl -pi -e 's/URL1/URL4/g;s/URL2/URL5/g;s/URL3/URL6/g'  Input_file

Wanted to add here this will change Input_file with the new values.

Thanks,
R. Singh
# 3  
Old 04-20-2016
Some more/refined details, please! I guess you don't want to modify based on "name" (the constant string), but on name's value, i.e. "a", "b", or "c", and all of this just in the "appMap" section?
# 4  
Old 04-20-2016
Hello RudiC,

Yes its more like if "name" is "a" , use URL4
If "name" is "b", use URL5
If "name" is "c", use URL6
# 5  
Old 04-20-2016
Here is an awk approach:-
Code:
awk -F'[" ]' '
        BEGIN {
                A["a"] = "URL4"
                A["b"] = "URL5"
                A["c"] = "URL6"
        }
        /URL/ {
                s = $0
                getline
                if ( $(NF-1) in A )
                        sub( /URL[0-9]+/, A[$(NF-1)], s )
                print s
        }
        1
' file.json

# 6  
Old 04-20-2016
Well, although a parser is not simply done but usually an intricate piece of software, here is
- a bash script
- replacing ALL kind of URLs (abc.com; def.org; ghi.de; ...)
- only in "appMar" section
As the URL can assume any shape, we can't rely on a string constant ("URLnn") but need to check for the file structure (the URL is the first entry in the second nesting level).

Try
Code:
#!/bin/bash
#
TGSCT=0
LEVEL=0
URL=""

declare -A RP
RP["a"]=XYZ.com                                         # set up replacement URLs
RP["b"]=YZX.org
RP["c"]=ZXY.biz

while IFS="" read LN                                    # don't destroy original line
 do     IFS=": " read P1 P2 R <<< $LN                   # get elements from line; use bashs "here string"
        [ "$P1" != "${P1/appMap}" ] && TGSCT=1          # test for the right target section ("appMap")

        OLDLVL=$LEVEL                                   # save nesting level
        T1=${LN//\{}                                    # compute actual nesting level (based on braces)
        T2=${LN//\}}
        LEVEL=$((LEVEL + ${#T2} - ${#T1}))              # which is count opening braces minus count closing braces

        [ "$TGSCT" -eq 0 ] &&   { echo "$LN"
                                  continue
                                }                       # not in right section: print and leave

        [ "$OLDLVL" -eq 2 ] &&  { URL=${P1//\"}         # test for nesting level (URL is 2), compute URL w/o double quotes
                                  TMP="$LN"             # save URL in variable and total line in temp. var
                                }

        [ "$P1" != "${P1/name}" ] && {  NM=${P2//\"}    # test for "name" tag, compute name value w/o double quotes
                                        LN="${TMP/$URL/${RP[$NM]}}"$'\n'"$LN"
                                                        # replace old URL with new URL
                                        URL=""; }       # empty URL variable

        [ -z "$URL" ] && echo "$LN"                     # print only if no URL has been intercepted

        [ "$LEVEL" -eq 0 ] && TGSCT=0                   # zero nesting level means target section left

   done < file                                          # read from json file

This is just straightforward functionality to prove it's working, there's nothing extra like error checking or debugging. If need be, this is left to the reader.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Building JSON command with bash script

Hello. I'm new to bash script and I'm learning the basics by writing some scripts. Recently a friend of mine asked me if I could try to write a script to him to automate a couple of processes that uses JSON RPCs. I'll try to explain in few words the workflow just to contextualize the problem.... (48 Replies)
Discussion started by: psysc0rpi0n
48 Replies

2. Shell Programming and Scripting

JSON structure to table form in awk, bash

Hello guys, I want to parse a JSON file in order to get the data in a table form. My JSON file is like this: { "document":{ "page": }, { "column": } ] }, { ... (6 Replies)
Discussion started by: Gescad
6 Replies

3. Shell Programming and Scripting

Bash script - cygwin (powershell?) pull from GitHub API Parse JSON

All, Have a weird issue where i need to generate a report from GitHub monthly detailing user accounts and the last time they logged in. I'm using a windows box to do this (work issued) and would like to know if anyone has any experience scripting for GitAPI using windows / cygwin / powershell?... (9 Replies)
Discussion started by: ChocoTaco
9 Replies

4. Shell Programming and Scripting

Bash script not parsing file with spaces in path

Hi everyone, I'm trying to write my first ever shell script, the OS is Raspbian. The code I have written must be executed whenever a certain database has been modified. The database resides on a Windows server to which I have a mount point, and I have no control over the Windows server at all so... (2 Replies)
Discussion started by: gjws
2 Replies

5. Shell Programming and Scripting

How to define a variable in a BASH script by using a JSON file online?

Hello, I would like to modify an existing script of mine that uses a manually defined "MCVERSION" variable and make it define that variable instead based on this JSON file stored online: https://s3.amazonaws.com/Minecraft.Download/versions/versions.json Within that JSON, I 'm looking for... (4 Replies)
Discussion started by: nbsparks
4 Replies

6. Shell Programming and Scripting

Column parsing in a bash script - HELP

I would like to setup a script that pulls in time/date in two seperate columns, and then name the other columns as listed below: Column1=Art/TJ output Column2=Art/TJ output Column3=TJ output column4=Art output Column5=If time/date past 12:00 noon -fail Colume6=If time/date before... (1 Reply)
Discussion started by: walnutpony123
1 Replies

7. Shell Programming and Scripting

Problems editing file with awk in bash script

Hello dear users, here I have a script to manipulate .csv files that are like this originally: And I need to make a script to delete certain fields. Each field is separated with a comma. So, here is my script (at least a part of it): Field $1 is composed of a name, and then a... (5 Replies)
Discussion started by: sr00t
5 Replies

8. Shell Programming and Scripting

Help parsing filename with bash script

Hi all! Looking for some help parsing filenames in bash. I have a directory full of files named "livingroom-110111105637.avi". The format is always date and time (yymmddhhmmss). I'm looking to parse the filenames so they are a little more easily readable. Maybe rename them to... (4 Replies)
Discussion started by: mtehonica
4 Replies

9. Web Development

bash script editing my apache config files

okay i'm going to try to say this uber-simple: I use dropbox (file-sync service). in order for dropbox sync files, they must be its children eg. somewhere under /home/jzacsh/Dropbox]. I want to now use it to keep my development files in sync across my machines: easy: just move my dev. files... (2 Replies)
Discussion started by: jzacsh
2 Replies

10. Shell Programming and Scripting

Conversion of bash parsing script to perl?

I need help with a perl parsing script. I have some error logs on a windows machine that I need to parse from a text file, but I know nothing about perl. I usually run this bash script on my linux box and it does just what I need. How would I do the same thing with perl and port it to my windows... (2 Replies)
Discussion started by: cstovall
2 Replies
Login or Register to Ask a Question