0

How can I print the value after "games", "toys", "books"? Here's the output of jq '.' on the file:

 {
    "reports": [
        {
          "games": 3,
          "toys": 0,
          "books": 3,
          "to": "2017-07-22T00:00:00.000+0000",
          "from": "2017-07-21T00:00:00.000+0000",
          "eventType": "bookedIn",
          "rowName": "CWQ"
        },
        {
          "games": 0,
          "toys": 0,
          "books": 0,
          "to": "2017-07-22T00:00:00.000+0000",
          "from": "2017-07-21T00:00:00.000+0000",
          "eventType": "bookedOut",
          "rowName": "AED"
        }
    ]
 }

I tried with

{ echo "["; cat file; echo "]"; } | jq -r ' .[] | ( "\n" + .rowName + ":", "games -> " + (.games | tostring), "toys -> " + (.toys | tostring), "books -> " + (.books | tostring) ) '

It seems I'm doing something wrong.

UPDATE Just for my knowledge assuming that books have another 2 items: provider and name :-?

jq -r '.reports |
.[] |
("\n" + .rowName + ":",
 "Games: " + (.games | tostring),
 "Toys: " + (.toys | tostring),
 .books | .[] |
 ("\n" + .name + ": ",
 "provider: " + (.provider | tostring) ) ) 
' response.json

Ill get jq: error (at response.json:1): Cannot iterate over null (null)

3
  • That's what happens when you change the requirements after the product is delivered. Aug 20, 2017 at 2:17
  • you are missing the closing ] Aug 20, 2017 at 2:19
  • There are multiple { } like those too I did put them all and I forgot to close it after the second one
    – Cravenica
    Aug 20, 2017 at 2:20

3 Answers 3

1

Using the JSON command line parser jq:

$ jq -r '.reports[] |          
         "\(.rowName) :",     
         "books -> \(.books)",
         "toys -> \(.toys)",  
         "games -> \(.games)\n"' input.json

CWQ:
books -> 3
toys -> 0
games -> 3

AED:
books -> 0
toys -> 0
games -> 0

Note: This uses jq's string interpolation and iterates over all entries in the reports array, picking out the relevant information.


Update: Handling .books being an array of objects:

Given

{
  "reports": [
    {
      "games": 3,
      "toys": 0,
      "books": [
        {
          "provider": "The provider",
          "name": "The name"
        }
      ],
      "to": "2017-07-22T00:00:00.000+0000",
      "from": "2017-07-21T00:00:00.000+0000",
      "eventType": "bookedIn",
      "rowName": "CWQ"
    },
    {
      "games": 0,
      "toys": 0,
      "books": [
        {
          "provider": "The 1st provider",
          "name": "The 1st name"
        },
        {
          "provider": "The 2nd provider",
          "name": "The 2nd name"
        }
      ],
      "to": "2017-07-22T00:00:00.000+0000",
      "from": "2017-07-21T00:00:00.000+0000",
      "eventType": "bookedOut",
      "rowName": "AED"
    }
  ]
}

We can simply do

$ jq -r '.reports[] |
         "\(.rowName):",
         ( .books[] | "book name -> \(.name)",
                      "book provider -> \(.provider)" ),
         "toys -> \(.toys)",
         "games -> \(.games)\n"'

CWQ:
book name -> The name
book provider -> The provider
toys -> 0
games -> 3

AED:
book name -> The 1st name
book provider -> The 1st provider
book name -> The 2nd name
book provider -> The 2nd provider
toys -> 0
games -> 0
11
  • I cannot use jq command unfortunately :(
    – Cravenica
    Aug 19, 2017 at 20:11
  • @Kimmyshor Why?
    – Kusalananda
    Aug 19, 2017 at 20:14
  • I don't have the packages and I'm not able to install those :(
    – Cravenica
    Aug 19, 2017 at 20:15
  • @Kimmyshor I'm happy to see that you managed to install jq.
    – Kusalananda
    Aug 20, 2017 at 6:12
  • Yes, well I realized that you can't make omelet without breaking few eggs...
    – Cravenica
    Aug 20, 2017 at 6:14
0
awk -v RS='[{},]' '/^"(games|toys|books)"/{ sub("^.*:",""); print; }' file
0
0
0
50
30
20
10
30
20

or for just one word:

awk -v pattern=games -v RS='[{},]' '$0 ~ "^\"" pattern "\"" { sub("^.*:",""); print; }' file
0
50
10
1
  • this also works: cat result.txt | tr '":{}[]' ',' | awk -F "," '{for (i=1;i<=NF;i++) if($i == "books") print $(i+2)}' , but i need to add text between them
    – Cravenica
    Aug 19, 2017 at 20:31
0

Let's parse that JSON with a JSON parser, .

jq -r '
    .reports |
    .[] |
    (
        "\n" + .rowName + ":",
        "games -> " + (.games | tostring),
        "toys -> "  + (.toys  | tostring),
        "books -> " + (.books | tostring)
    )
' file
CWQ:
games -> 3
toys -> 0
books -> 3

AED:
games -> 0
toys -> 0
books -> 0
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .