当键是数字字符串时,使用jq从json获取数据 [英] Getting data from json using jq when key is numerical string
问题描述
我是jq的新手,在解析json时遇到问题
I am new to jq and facing an issue while parsing my json
我在这样的变量中存储了一个json
I have a json stored in a variable like this
temp='{ "1": { "my_name": "one" }, "2": { "my_name": "two" } }'
现在我需要获取其他两个条目的my_name值
Now I need to get the value of my_name for both other entries
我已经尝试过类似的事情
I have tried something like this
echo $temp | jq '.1' //out put 0.1
I was assuming to get { "my_name": "one" }
类似地,我也获得了my_name值
And similarly to get my_name value I did
echo $temp | jq '.1.my_name' // Its output is giving me error
任何人都可以帮助确定我的语法有什么问题以及如何纠正它.
Can any one please help to identify what is wrong with my syntax and how can I correct it.
推荐答案
只是将数字解释为浮点数.您需要在明确地使用键字符串的上下文中使用它.
Just the number gets interpreted as a float. You need to use it in a context where it is unambiguously a key string.
echo "$temp" | jq '.["1"]["my_name"]'
和
echo "$temp" | jq '.["1"]'
获取包含的字典.
有了一个足够新的jq
(我认为> = 1.4),您还可以说
With a sufficiently new jq
(I think >= 1.4) you can also say
echo "$temp" | jq '."1".my_name'
这篇关于当键是数字字符串时,使用jq从json获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!