【发布时间】:2019-07-14 15:20:28
【问题描述】:
我一直在使用 API 调用以 JSON 格式对其进行结构化,以便稍后将其推送到数据库中。然后代码如下所示:
getPage() {
curl --fail -X GET 'https://api.app.com/v1/test?page=1&pageSize=1000&sort=desc' \
-H 'Authorization: Bearer 123abc456pickupsticks789' \
-H 'cache-control: no-cache'
}
getPage \
| jq -c '.items | .[] | {landing_id: .landing_id, submitted_at: .submitted_at, answers: .answers, email: .hidden.email}' \
> testpush.json
当我运行它时,它会产生这个错误:jq: error (at <stdin>:0): Cannot iterate over null (null)
我看过this one、or this one from this site和this response.等解决方案
常见的解决方案似乎是在[] 前面使用?,我在底部的jq 行中尝试了它,但它仍然不起作用。它只会生成一个空的 json 文件。
我是否误读了其他答案的要点,没有将我的 ? 放在正确的位置?>
【问题讨论】:
-
检查以确保响应实际上是一个包含
items属性的对象,该属性包含一个数组。 -
这个问题的答案对我不起作用。幸运的是,您的问题中的引用确实 -
.myArrayElement?例如:.answers[]?
标签: jq