1
$ curl -LNs "http://urladrescom/content.json" > content.json

content.json

{
   "k":[
      {
         "i":1,
         "n":"NAME 1",
         "p":[
            {
               "b":"Event 1",
               "c":"00:00",
               "d":"03:00"
            },
            {
               "b":"Event 2",
               "c":"23:00",
               "d":"00:00"
            }
         ]
      },
      {
         "i":2,
         "n":"NAME 2",
         "p":[
            {
               "b":"Event 1",
               "c":"07:15",
               "d":"09:15"
            },
            {
               "b":"Event 2",
               "c":"22:00",
               "d":"23:15"
            },
            {
               "b":"Event 3",
               "c":"23:15",
               "d":"02:30"
            }
         ]
      },
      {
         "i":3,
         "n":"NAME 3",
         "p":[
            {
               "b":"Event 1",
               "c":"07:15",
               "d":"09:15"
            },
            {
               "b":"Event 2",
               "c":"22:00",
               "d":"23:15"
            },
            {
               "b":"Event 3",
               "c":"23:15",
               "d":"02:30"
            }
         ]
      }
   ]
}

I want to get the content in "NAME 2" under Event 1,2,3 with a Bash script (grep, awk, sed, etc..) (or Python in a Bash script cmd).

I want print result :

NAME 2 \ Event 1 \ 07:15 \ 09:15
NAME 2 \ Event 2 \ 22:00 \ 23:15
NAME 2 \ Event 3 \ 23:15 \ 02:30
  • 2
    I'd suggest using jq instead e.g. jq -r --arg name "NAME 2" '.k[] | select(.n==$name) | .p[] | [$name, .b, .c, .d] | @tsv' content.json – steeldriver Aug 19 '18 at 23:43
  • 1
    jq is not in my system. my system platform mips only grep sed awk curl and cmd python ! – tioma Aug 20 '18 at 0:02
  • @tioma You should be able to install jq, either by using your package manager, or by compiling it yourself. – Kusalananda Aug 20 '18 at 7:31
2

You can use this Python to do what you want:

$ cat parse.py
#!/bin/python

import json
#from pprint import pprint

with open('content.json') as f:
  data = json.load(f)

for dict in data["k"]:
  if (dict["n"] == "NAME 2"):
    for elem in dict["p"]:
      print(dict["n"] + ' \\ ' + elem["b"] + ' \\ ' + elem["c"] + ' \\ ' + elem["d"])

Example

$ ./parse.py
NAME 2 \ Event 1 \ 07:15 \ 09:15
NAME 2 \ Event 2 \ 22:00 \ 23:15
NAME 2 \ Event 3 \ 23:15 \ 02:30
|improve this answer|||||
0

Using the command line JSON parser jq...

There might be a shorter jq expression that does this, but this is what I came up with:

$ jq -r --arg name "NAME 2" '.[][]|select(.n==$name).p[]|[$name,.[]]|join(" \\ ")' content.json
NAME 2 \ Event 1 \ 07:15 \ 09:15
NAME 2 \ Event 2 \ 22:00 \ 23:15
NAME 2 \ Event 3 \ 23:15 \ 02:30

Or, using slightly more selective code, as suggested by steeldriver,

$ jq -r --arg name "NAME 2" '.k[]|select(.n==$name).p[]|[$name, .b, .c, .d]|join(" \\ ")' content.json
NAME 2 \ Event 1 \ 07:15 \ 09:15
NAME 2 \ Event 2 \ 22:00 \ 23:15
NAME 2 \ Event 3 \ 23:15 \ 02:30

name is a jq variable whose value is passed on the command line. The jq code is doing five things:

  1. Extract the k arrays in the data that we're interested in.
  2. Select the particular array that we want to output (and the p bit from this).
  3. Create an array for outputting (since the name is not part of the data, it has to be inserted).
  4. Do output with the particular delimiter inserted.
|improve this answer|||||

Not the answer you're looking for? Browse other questions tagged or ask your own question.