如何jq打印键(不是键的值)并迭代键来打印子值

时间:2016-12-29 19:26:39

标签: jq

我可以打印钥匙:

$ jq -r '.indices | keys[]'
plan
resource
user

我可以打印子值:

$ jq '.indices[] | .primaries.docs.count'
14208
1427143
104475

如何打印键和子值?

plan: 14208
resource: 1427143
user: 104475

2 个答案:

答案 0 :(得分:7)

以下可能有效:

levels(df1[,2])[levels(df1[,2])  %in% c("Breadth", "Height")]  <- "Height_Breadth"
d2 <- transform(aggregate(cbind(Combination = ID)~Measurement, df1, 
         subset = ID != "ALL", FUN = toString), ID = "ALL")
transform(merge(df1, d2, all.x=TRUE, by = c("ID", "Measurement")), 
                 Combination = Combination.y)[-(3:4)]

以上假设输入为:

$ jq -r '.indices | to_entries[] | "\(.key): \(.value.primaries.docs.count)"' input.json
plan: 14208
resource: 1427143
user: 104475

{ "indices": { "plan": { "primaries": { "docs": { "count": 123 }}}, "resource": { "primaries": { "docs": { "count": 456 }}}, "user": { "primaries": { "docs": { "count": 789 }}} } } 会将对象to_entries转换为数组:

indices

然后可以轻松映射。

答案 1 :(得分:5)

以下是直接使用密钥的解决方案:

  .indices
| keys[] as $k
| "\($k): \(.[$k].primaries.docs.count)"