2

Example json file content in file /tmp/t

 [
    {
      "name:first"   : "trevor",
      "last"    : "wellington",
      "from"    : "england",
      "age"     : 52,
      "sports"  : [ "rugby", "badmitton", "snooker" ]
    },
    {
      "name:first"   : "yoni",
      "last"    : "halevi",
      "from"    : "israel",
      "age"     : 26,
      "sports"  : [ "soccer", "windsurfing" ]
    },
    {
      "name:first"   : "cory",
      "last"    : "parker",
      "from"    : "united states",
      "age"     : 31,
      "sports"  : [ "windsurfing", "baseball", "extreeeeme kayaking" ]
    }
  ]

This works fine cat /tmp/t | jsawk -n 'out(this.last)'

But this does not cat test.json | jsawk -n 'out(this.name:first)'

Likely related to: Selecting a JSON object with a colon in the key
and How do I access these weird JSON items with jQuery?

But cat test.json | jsawk -n 'out(this.name[':first'])' does not work either

Community
  • 1
  • 1
Etienne Low-Décarie
  • 13,063
  • 17
  • 65
  • 87

1 Answers1

4

Here you go:

... | jsawk -n 'out(this["name:first"])'

this.name:first doesn't work because a bare : cannot be in object attributes.

For example, given this JavaScript object:

x = {
  "name:first"   : "cory",
  "last"    : "parker",
  "from"    : "united states",
  "age"     : 31,
  "sports"  : [ "windsurfing", "baseball", "extreeeeme kayaking" ]
}

These are valid:

  • x.last and x['last']
  • x.from and x['from']
  • ...
  • but only x['name:first'] (x.name:first is not)
janos
  • 120,954
  • 29
  • 226
  • 236