@@ -57,6 +57,7 @@ func init() {
5757
5858 h += "Options:\n "
5959 h += " -u, --ungron Reverse the operation (turn assignments back into JSON)\n "
60+ h += " -v, --values Print just the values of provided assignments\n "
6061 h += " -c, --colorize Colorize output (default on tty)\n "
6162 h += " -m, --monochrome Monochrome (don't colorize output)\n "
6263 h += " -s, --stream Treat each line of input as a separate JSON object\n "
@@ -95,6 +96,7 @@ func main() {
9596 versionFlag bool
9697 insecureFlag bool
9798 jsonFlag bool
99+ valuesFlag bool
98100 )
99101
100102 flag .BoolVar (& ungronFlag , "ungron" , false , "" )
@@ -111,6 +113,9 @@ func main() {
111113 flag .BoolVar (& insecureFlag , "insecure" , false , "" )
112114 flag .BoolVar (& jsonFlag , "j" , false , "" )
113115 flag .BoolVar (& jsonFlag , "json" , false , "" )
116+ flag .BoolVar (& valuesFlag , "values" , false , "" )
117+ flag .BoolVar (& valuesFlag , "value" , false , "" )
118+ flag .BoolVar (& valuesFlag , "v" , false , "" )
114119
115120 flag .Parse ()
116121
@@ -161,10 +166,12 @@ func main() {
161166 opts = opts | optJSON
162167 }
163168
164- // Pick the appropriate action: gron, ungron or gronStream
169+ // Pick the appropriate action: gron, ungron, gronValues, or gronStream
165170 var a actionFn = gron
166171 if ungronFlag {
167172 a = ungron
173+ } else if valuesFlag {
174+ a = gronValues
168175 } else if streamFlag {
169176 a = gronStream
170177 }
@@ -391,6 +398,48 @@ func ungron(r io.Reader, w io.Writer, opts int) (int, error) {
391398 return exitOK , nil
392399}
393400
401+ // gronValues prints just the scalar values from some input gron statements
402+ // without any quotes or anything of that sort; a bit like jq -r
403+ // e.g. json[0].user.name = "Sam"; -> Sam
404+ func gronValues (r io.Reader , w io.Writer , opts int ) (int , error ) {
405+ scanner := bufio .NewScanner (os .Stdin )
406+
407+ for scanner .Scan () {
408+ s := statementFromString (scanner .Text ())
409+
410+ // strip off the leading 'json' bare key
411+ if s [0 ].typ == typBare && s [0 ].text == "json" {
412+ s = s [1 :]
413+ }
414+
415+ // strip off the leading dots
416+ if s [0 ].typ == typDot || s [0 ].typ == typLBrace {
417+ s = s [1 :]
418+ }
419+
420+ for _ , t := range s {
421+ switch t .typ {
422+ case typString :
423+ var text string
424+ err := json .Unmarshal ([]byte (t .text ), & text )
425+ if err != nil {
426+ // just swallow errors and try to continue
427+ continue
428+ }
429+ fmt .Println (text )
430+
431+ case typNumber , typTrue , typFalse , typNull :
432+ fmt .Println (t .text )
433+
434+ default :
435+ // Nothing
436+ }
437+ }
438+ }
439+
440+ return exitOK , nil
441+ }
442+
394443func colorizeJSON (src []byte ) ([]byte , error ) {
395444 out := & bytes.Buffer {}
396445 f := jsoncolor .NewFormatter ()
0 commit comments