如何选择在jq中作为变量提供的键的值? [英] How to select values of keys that are provided as variables in jq?

查看:111
本文介绍了如何选择在jq中作为变量提供的键的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这是输入,

{
    "a_key":        2,
    "another_key":  100,
    "one_more_key": -4.2
}

通过提供键名称作为变量来选择任何这些键的值的最佳方法是什么?理想情况下,我正在寻找类似的东西:

what would be the best way to select the value of any of these keys by providing the name of the key as a variable? Ideally, I was looking for something like:

"a_key" as $key | .$key

这会导致语法错误(意外的'$'").我想不出一种让jq评估变量的简单方法.

This result in a syntax error, though ("unexpected '$'"). I could not figure out a straightforward way to make jq evaluate the variable.

推荐答案

就像javascript中一样,jq支持索引.您可以按名称访问对象上的属性.

Just like in javascript, jq supports indexing. You can access properties by name on objects.

"a_key" as $key | .[$key]

这篇关于如何选择在jq中作为变量提供的键的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆