我希望从这个JSON文件中提取cat事实:

[
  {
    "status": {
      "verified": true,
      "feedback": "",
      "sentCount": 1
    },
    "_id": "5887e1d85c873e0011036889",
    "user": "5a9ac18c7478810ea6c06381",
    "text": "Cats make about 100 different sounds. Dogs make only about 10.",
    "__v": 0,
    "source": "user",
    "updatedAt": "2020-09-03T16:39:39.578Z",
    "type": "cat",
    "createdAt": "2018-01-15T21:20:00.003Z",
    "deleted": false,
    "used": true
  },
  {
    "status": {
      "verified": true,
      "sentCount": 1
    },
    "_id": "588e746706ac2b00110e59ff",
    "user": "588e6e8806ac2b00110e59c3",
    "text": "Domestic cats spend about 70 percent of the day sleeping and 15 percent of the day grooming.",
    "__v": 0,
    "source": "user",
    "updatedAt": "2020-08-26T20:20:02.359Z",
    "type": "cat",
    "createdAt": "2018-01-14T21:20:02.750Z",
    "deleted": false,
    "used": true
  },
  {
    "status": {
      "verified": true,
      "sentCount": 1
    },
    "_id": "58923f2fc3878c0011784c79",
    "user": "5887e9f65c873e001103688d",
    "text": "I don't know anything about cats.",
    "__v": 0,
    "source": "user",
    "updatedAt": "2020-08-23T20:20:01.611Z",
    "type": "cat",
    "createdAt": "2018-02-25T21:20:03.060Z",
    "deleted": false,
    "used": false
  },
  {
    "status": {
      "verified": true,
      "sentCount": 1
    },
    "_id": "5894af975cdc7400113ef7f9",
    "user": "5a9ac18c7478810ea6c06381",
    "text": "The technical term for a cat’s hairball is a bezoar.",
    "__v": 0,
    "source": "user",
    "updatedAt": "2020-11-25T21:20:03.895Z",
    "type": "cat",
    "createdAt": "2018-02-27T21:20:02.854Z",
    "deleted": false,
    "used": true
  },
  {
    "status": {
      "verified": true,
      "sentCount": 1
    },
    "_id": "58e007cc0aac31001185ecf5",
    "user": "58e007480aac31001185ecef",
    "text": "Cats are the most popular pet in the United States: There are 88 million pet cats and 74 million dogs.",
    "__v": 0,
    "source": "user",
    "updatedAt": "2020-08-23T20:20:01.611Z",
    "type": "cat",
    "createdAt": "2018-03-01T21:20:02.713Z",
    "deleted": false,
    "used": false
  }
]

它的网址是https://cat-fact.herokuapp.com/facts.我知道访问信息不是问题,因为当我运行curl 'https://cat-fact.herokuapp.com/facts' | jq '.'时,我会得到整个文件作为回报.

跑了curl 'https://cat-fact.herokuapp.com/facts' | jq '. | {text}'次之后,

我得到错误jq: error (at <stdin>:0): Cannot index array with string "text".

跑了curl 'https://cat-fact.herokuapp.com/facts' | jq '. | {.text}'次之后,

返回:(23) Failed writing body

跑了curl 'https://cat-fact.herokuapp.com/facts' | jq '.[] | {text: .commit.text}'次之后,

这是返回的:

 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Dload  Upload   Total   Spent    Left  Speed                                           100  1675
100  1675    0     0   9463      0 --:--:-- --:--:-- --:--:--  9463
{
"text": null
}
{
"text": null
}
{
"text": null
}
{
"text": null
}
{ 
"text": null
}            

推荐答案

我会使用数组/对象值迭代器.[],然后使用["text"]来过滤:

jq '.[]["text"]' 

将示例文件作为输入将产生以下输出:

"Cats make about 100 different sounds. Dogs make only about 10."
"Domestic cats spend about 70 percent of the day sleeping and 15 percent of the day grooming."
"I don't know anything about cats."
"The technical term for a cat’s hairball is a bezoar."
"Cats are the most popular pet in the United States: There are 88 million pet cats and 74 million dogs."

以上也是您(当前)可以从中获得的确切输出:

curl -s 'https://cat-fact.herokuapp.com/facts' | jq '.[]["text"]'

Json相关问答推荐

从Razor Pages的AJAX Json呈现DataTables问题.Net GET

SWIFT中的网络经理

使用 Powershell,如何将 Azure AD 组成员转换为 Json 对象(文件),然后可以更新?

修改 boost::json::object 中的值

从 JSON 响应中获取最新版本发布字段

JQ 中的样本标准偏差

从 json 对象中过滤掉所有出现的给定属性

如何在 Node.js 中解析包含NaN的 JSON 字符串

JBuilder中未定义的局部变量或方法json

如何使用 CORS 实现 JavaScript Google Places API 请求

以 unicode 将 pandas DataFrame 写入 JSON

有没有办法使用 Jackson 将 Map 转换为 JSON 表示而不写入文件?

NSManagedObject 属性值的 NSNull 处理

强制 JSON.NET 在序列化 DateTime 时包含毫秒(即使 ms 组件为零)

SCRIPT5009:JSON未定义

设置对象不是 JSON 可序列化的

如何从 MySQL 中检索 JSON 数据?

JSON.stringify 向我的 Json 对象添加额外的 \ 和 "" 的问题

按键值过滤 JSON

从动态 json 数据更新力有向图上的链接