使用 jq 提取 json
extracting json using jq
我有像 this.How 这样的示例 Json 文件可以从中提取数据吗?
我试了几次things.But都没有得到想要的结果
{
"adults":{
"car":{"length":[20,25],"width":[225,40]},
"tractor":{"length":[20,23],"width":[223,40]}
},
"children":{
"cycle":{"length":[20,21],"width":[221,40]},
}
}
我想将此数据插入到数据库中,包含以下列:
age_group |车辆 |尺寸
所以我的最终 table 会是
age_group | vehicle | measurements
------------------------------------
adults car {"length":[20,25],"width":[225,40]}
adults tractor {"length":[20,21],"width":[223,40]}
children cycle {"length":[20,21],"width":[221,40]}
如何使用 jq 根据需要将 json 对象的信息提取到文本文件中?
编辑:如何获取下面要求的内键?
age_group | vehicle | Dimension
------------------------------------
adults car length
adults car width
adults tractor length
adults tractor width
children cycle length
children cycle width
jq'to_entries | map(.key as $mykey | .value | to_entries | map([$mykey, .key, .value])) | add 'test.json
- 将JSON对象转换为标准形式jq
- 记住我们解析过的密钥
- 解析值JSON对象
- 构造输出
更新:
jq 'to_entries | map(.key as $mykey | .value | to_entries | map(.key as $mykey2 | .value | to_entries | map([$mykey, $mykey2, .key]))) | add | add ' test.json
jq提供了两种我们可以操作json文件的工具。首先我们可以过滤json文件中的数据,但是过滤可能会丢失数据。其次我们可以映射和减少数据。
所以我们首先必须把你的变量json变成我们可以访问它的键值对的固定结构。之后我们使用过滤器从值字段中提取数据。由于您已经给出了一个 json 文件,其中嵌套了 3 个变量 dict,因此我们必须执行三次才能将变量 json 转换为固定字段。
我有像 this.How 这样的示例 Json 文件可以从中提取数据吗?
我试了几次things.But都没有得到想要的结果
{
"adults":{
"car":{"length":[20,25],"width":[225,40]},
"tractor":{"length":[20,23],"width":[223,40]}
},
"children":{
"cycle":{"length":[20,21],"width":[221,40]},
}
}
我想将此数据插入到数据库中,包含以下列:
age_group |车辆 |尺寸
所以我的最终 table 会是
age_group | vehicle | measurements
------------------------------------
adults car {"length":[20,25],"width":[225,40]}
adults tractor {"length":[20,21],"width":[223,40]}
children cycle {"length":[20,21],"width":[221,40]}
如何使用 jq 根据需要将 json 对象的信息提取到文本文件中?
编辑:如何获取下面要求的内键?
age_group | vehicle | Dimension
------------------------------------
adults car length
adults car width
adults tractor length
adults tractor width
children cycle length
children cycle width
jq'to_entries | map(.key as $mykey | .value | to_entries | map([$mykey, .key, .value])) | add 'test.json
- 将JSON对象转换为标准形式jq
- 记住我们解析过的密钥
- 解析值JSON对象
- 构造输出
更新: jq 'to_entries | map(.key as $mykey | .value | to_entries | map(.key as $mykey2 | .value | to_entries | map([$mykey, $mykey2, .key]))) | add | add ' test.json
jq提供了两种我们可以操作json文件的工具。首先我们可以过滤json文件中的数据,但是过滤可能会丢失数据。其次我们可以映射和减少数据。
所以我们首先必须把你的变量json变成我们可以访问它的键值对的固定结构。之后我们使用过滤器从值字段中提取数据。由于您已经给出了一个 json 文件,其中嵌套了 3 个变量 dict,因此我们必须执行三次才能将变量 json 转换为固定字段。