使用'jq'[重复]从JSON输出中获取数据

问题描述 投票:0回答:2

这个问题在这里已有答案:

我有以下JSON输出:

{
  "201110131738QP27N": {
    "parent": 17,
    "name": "CentralServer",
    "status": "Active",
    "count": 6
  },
  "201803271459ICV69": {
    "name": "subaccount1",
    "status": "Active",
    "count": 1
  },
  "2018032715008ZM2G": {
    "name": "subaccount2",
    "status": "Active",
    "count": 1
  },
  "201803281536PSKR4": {
    "name": "Este e um teste",
    "status": "Active"
  }
}

我试图只获得以下数据:

201110131738QP27N
201803271459ICV69
2018032715008ZM2G
201803281536PSKR4

为了获得所需的数据,我正在尝试使用jq命令,但到目前为止我还没有成功。

json bash key jq
2个回答
1
投票

要获得没有数组的原始输出,请使用|.[]来打开它

jq --raw-output 'keys | .[]'  input.json

产生

201110131738QP27N
201803271459ICV69
2018032715008ZM2G
201803281536PSKR4

或更简单地写为jq --raw-output 'keys[]'


2
投票

使用keys builtin functionjq

假设您的输入JSON存储在文件input.json中:

$ cat input.json | jq 'keys'

生产:

[
  "201110131738QP27N",
  "201803271459ICV69",
  "2018032715008ZM2G",
  "201803281536PSKR4"
]
© www.soinside.com 2019 - 2024. All rights reserved.