0

My JSON output looks like this;

{  
   "1":{  
      "state":{  
         "on":false,
         "bri":124,
         "hue":14985,
         "sat":252,
         "effect":"none",
         "xy":[  
            0.5182,
            0.4363
         ],
         "ct":480,
         "alert":"none",
         "colormode":"xy",
         "reachable":true
      },
      "type":"Extended color light",
      "name":"Slaapkamer rechts",
      "modelid":"LCT001",
      "manufacturername":"Philips",
      "uniqueid":"00:17:88:01:00:dc:36:68-0b",
      "swversion":"66013187"
   },
   "2":{  
      "state":{  
         "on":false,
         "bri":125,
         "hue":14984,
         "sat":252,
         "effect":"none",
         "xy":[  
            0.5182,
            0.4363
         ],
         "ct":480,
         "alert":"none",
         "colormode":"xy",
         "reachable":true
      },
      "type":"Extended color light",
      "name":"Slaapkamer links",
      "modelid":"LCT001",
      "manufacturername":"Philips",
      "uniqueid":"00:17:88:01:00:dc:33:99-0b",
      "swversion":"66013187"
   }
}

Does anyone know a easy way to make this output grep-able? Or any other way to put the output on one line so I can use tools like grep, awk etc on the json output? If I do a grep right now on the word 'hue' then I get 2 lines;

"hue":14985,
"hue":14984,

While I sometimes only want to grep on the first or the 2nd one..

Any ideas?

Alex van Es
  • 1,141
  • 2
  • 15
  • 27
  • Looks like you need to parse it, and I don't recommend you to do it with awk or sed. Instead use something like [jshon](http://kmkeen.com/jshon/). – stek29 Dec 27 '15 at 18:03
  • 2
    Please include an example input and expected output. I can't understand what you mean with *"I sometimes only want to grep on the first or the 2nd one"* – Andrea Corbellini Dec 27 '15 at 18:04
  • Or you just want to remove all newlines? – stek29 Dec 27 '15 at 18:04

2 Answers2

6

With jq:

jq '.["1"].state.hue' file

Output:

14985

It is not possible to use '.1.state.hue' because numbers are a special case.


jq '.["2"].state.hue' file

Output:

14984

jq '.[].state.hue' file

Output:

14984
14985
Cyrus
  • 84,225
  • 14
  • 89
  • 153
0

using awk an approach

awk '/hue/{++count;if(count==1){print}}'

change count to whatever occurrence you would like to obtain

repzero
  • 8,254
  • 2
  • 18
  • 40