xidel:如何从包含点(.)的JSON键中检索值?

huangapple go评论80阅读模式
英文:

xidel: how to retrieve value from JSON key containing dots(.)?

问题

  • 尝试检索 1 在:
$ cat object.json
{
    "apiVersion": "apps/v1",
    "kind": "Deployment",
    "metadata": {
        "annotations": {
            "deployment.kubernetes.io/revision": "1"
        }
    }
}

</br>

$ xidel -e '($json).metadata.annotations["deployment.kubernetes.io/revision"]' -s object.json
{
  "deployment.kubernetes.io/revision": "1"
}

已尝试:

'($json).metadata.annotations."deployment.kubernetes.io/revision"'
'($json).metadata.annotations.("deployment.kubernetes.io/revision")'
'($json).metadata.annotations.(deployment.kubernetes.io/revision)'
英文:

I try to retrieve 1 in :

$ cat object.json 
{
    "apiVersion": "apps/v1",
    "kind": "Deployment",
    "metadata": {
        "annotations": {
            "deployment.kubernetes.io/revision": "1"
        }
    }
}

</br>

$ xidel -e &#39;($json).metadata.annotations[&quot;deployment.kubernetes.io/revision&quot;]&#39; -s object.json 
{
  &quot;deployment.kubernetes.io/revision&quot;: &quot;1&quot;
}

Tried

&#39;($json).metadata.annotations.&quot;deployment.kubernetes.io/revision&quot;&#39;
&#39;($json).metadata.annotations.(&quot;deployment.kubernetes.io/revision&quot;)&#39;
&#39;($json).metadata.annotations.(deployment.kubernetes.io/revision)&#39;

答案1

得分: 1

I hardly ever use the dot-notation, because as far as I know in terms of capabilities it's rather limited.

JSONiq notation:

-e '$json("metadata")("annotations")("deployment.kubernetes.io/revision")'

Xpath-like notation with the XPath 3.1 map:get() function:

-e '$json/metadata/annotations/map:get(.,"deployment.kubernetes.io/revision")'

XPath 3.1 "?" lookup operator:

-e '$json?metadata?annotations?"deployment.kubernetes.io/revision"'

A combination of all notations:

-e '$json("metadata")/annotations?"deployment.kubernetes.io/revision"'
英文:

I hardly ever use the dot-notation, because as far as I know in terms of capabilities it's rather limited.

JSONiq notation:

-e &#39;$json(&quot;metadata&quot;)(&quot;annotations&quot;)(&quot;deployment.kubernetes.io/revision&quot;)&#39;

Xpath-like notation with the XPath 3.1 map:get() function:

-e &#39;$json/metadata/annotations/map:get(.,&quot;deployment.kubernetes.io/revision&quot;)&#39;

XPath 3.1 "?" lookup operator:

-e &#39;$json?metadata?annotations?&quot;deployment.kubernetes.io/revision&quot;&#39;

A combination of all notations:

-e &#39;$json(&quot;metadata&quot;)/annotations?&quot;deployment.kubernetes.io/revision&quot;&#39;

答案2

得分: 0

我找到了:

$ xidel -e '($json).metadata.annotations(["deployment.kubernetes.io/revision"])' -s object.json
1

这样做是正确的吗?

英文:

I've found:

$ xidel -e &#39;($json).metadata.annotations([&quot;deployment.kubernetes.io/revision&quot;])&#39; -s object.json
1

Is it a correct way to do it?

huangapple
  • 本文由 发表于 2023年2月24日 04:35:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定