Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I don't understand why key has to be in () for this to work:

# kbrandt at glade.local in ~ on git:master x [15:08:19]
$ cat host | jq '. | to_entries | map({ (.key) : .value.CPU.PercentUsed })' | tail
  {
    "rpi-b827eb2d7d23": 10.333333333333334
  },
  {
    "rpi-b827eb8d7c8d": 60
  },
  {
    "rpi-b827eba999fa": 40.733333333333334
  }
]

# kbrandt at glade.local in ~ on git:master x [15:08:54]
$ cat host | jq '. | to_entries | map({ .key : .value.CPU.PercentUsed })' | tail
jq: error: syntax error, unexpected FIELD (Unix shell quoting issues?) at <top-level>, line 1:
. | to_entries | map({ .key : .value.CPU.PercentUsed })
jq: 1 compile error
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.2k views
Welcome To Ask or Share your Answers For Others

1 Answer

When defining an object literal, the parentheses indicates that the expression value should be the property name. Otherwise if you didn't use parentheses, it's the literal name.

So these are equivalent ways to define an object with a "foo" property:

{ foo: 1 }
{ "foo": 2 }
"foo" as $name | { ($name): 3 }
{ somename: "foo" } | { (.somename): 4 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...