0

The following is the format of data that I need to parse in bash. Assuming that a bash variable holds this data, I need to be able to extract the value in xyz. Further, I need to be able to also extract aa and bb individually from with xyz.

"params": {
    "children": [
           {
                "abc": {
                    "pp": "1234567890",
                    "qq": "a.b.c"
                },
                "xyz": {
                    "aa": "0987654321",
                    "bb": "c.b.a"
                },
                "def": "p.q.r"
            }
        ],
        "def": "e.f.g.h"
    }

Any help on this is appreciated!

MLSC
  • 5,584
  • 7
  • 49
  • 85
Maddy
  • 1,251
  • 3
  • 20
  • 36

1 Answers1

1

The shell is not a good language for writing general parsers. That said, if your input format is more or less exactly as shown, with maximally one "name": "value" pair per line, try this:

$ eval $(sed -n 's/"\([^"]*\)"[ :]*"\([^"]*\)".*/\1=\2/p' inputfile)
$ echo $aa
0987654321
$ echo $bb
c.b.a
Jens
  • 65,924
  • 14
  • 115
  • 171