如何在每个 json object 拆分 jq 输出
how to split jq output at every json object
{
"title": "...",
"description": "...",
"languageKey": "ja"
} {
"title": "...",
"description": "...",
"languageKey": "ja"
} {
"title": "...",
"description": "...",
"languageKey": "ja"
}
此输出被提供给 curl 请求
参考:https://www.elastic.co/blog/loading-wikipedia
curl localhost:9200/dewikiquote/_search -d '{"query": {"match_all":{}}}' \
| jq '.hits.hits[] | ._source' \
| jq '{title: .heading[0], description: .text, languageKey: "ja"} \
| curl -H 'Content-Type: application/json' -XPUT 'http://localhost:11223/context/api/add' -d @-
但低于 java 端点仅被命中一次,地图 data
只有一个标题、描述和语言键。为什么其他 json 项丢失了?
@RequestMapping(path = "/api/add", method = RequestMethod.PUT)
public void add(@RequestBody Map<String, String> data)
{
...
那是因为 jq 输出没有在每个 json object 处拆分吗?如果是的话,我该如何用 jq 做到这一点?
我认为你需要这样尝试:
curl localhost:9200/dewikiquote/_search -d '{"query": {"match_all":{}}}' \
| jq '.[] | {title: .title, description: .description, languageKey: "ja"} \
| curl -H 'Content-Type: application/json' -XPUT 'http://localhost:11223/context/api/add' -d @-
您在返回的 json 列表中缺少“.[]”数组运算符
这是一个替代方法,它循环遍历结果并单独发送每个结果:
请注意 -c
以不缩进打印每个输出。
curl localhost:9200/dewikiquote/_search -d '{"query": {"match_all":{}}}' \
| jq '.hits.hits[] | ._source' \
| jq -c '{title: .heading[0], description: .text, languageKey: "ja"} \
| while read next; do curl -H 'Content-Type: application/json' -XPUT 'http://localhost:11223/context/api/add' -d $next; done
{
"title": "...",
"description": "...",
"languageKey": "ja"
} {
"title": "...",
"description": "...",
"languageKey": "ja"
} {
"title": "...",
"description": "...",
"languageKey": "ja"
}
此输出被提供给 curl 请求
参考:https://www.elastic.co/blog/loading-wikipedia
curl localhost:9200/dewikiquote/_search -d '{"query": {"match_all":{}}}' \
| jq '.hits.hits[] | ._source' \
| jq '{title: .heading[0], description: .text, languageKey: "ja"} \
| curl -H 'Content-Type: application/json' -XPUT 'http://localhost:11223/context/api/add' -d @-
但低于 java 端点仅被命中一次,地图 data
只有一个标题、描述和语言键。为什么其他 json 项丢失了?
@RequestMapping(path = "/api/add", method = RequestMethod.PUT)
public void add(@RequestBody Map<String, String> data)
{
...
那是因为 jq 输出没有在每个 json object 处拆分吗?如果是的话,我该如何用 jq 做到这一点?
我认为你需要这样尝试:
curl localhost:9200/dewikiquote/_search -d '{"query": {"match_all":{}}}' \
| jq '.[] | {title: .title, description: .description, languageKey: "ja"} \
| curl -H 'Content-Type: application/json' -XPUT 'http://localhost:11223/context/api/add' -d @-
您在返回的 json 列表中缺少“.[]”数组运算符
这是一个替代方法,它循环遍历结果并单独发送每个结果:
请注意 -c
以不缩进打印每个输出。
curl localhost:9200/dewikiquote/_search -d '{"query": {"match_all":{}}}' \
| jq '.hits.hits[] | ._source' \
| jq -c '{title: .heading[0], description: .text, languageKey: "ja"} \
| while read next; do curl -H 'Content-Type: application/json' -XPUT 'http://localhost:11223/context/api/add' -d $next; done