我对这样的json文件有一个问题:
{
"name": "The 24H Automatic Eye Pencil",
"url": "/3ina/the-24h-automatic-pencil-waterproof-brow-pencil/p-16132421/",
"id": "16132421",
"gender": "W"
}
{
"name": "The 24H Automatic Eye Pencil",
"url": "/3ina/the-24h-automatic-pencil-waterproof-brow-pencil/p-16104273/",
"id": "16104273",
"gender": "W"
}
{}
{
"brand": "3INA",
"cats": [
"Makeup",
"Eyebrows",
"Eyebrow Makeup"
]
}我有三个数组。我想将最后一个数组复制到每个prevoius数组中,因此它将如下所示:
{
"name": "The 24H Automatic Eye Pencil",
"url": "/3ina/the-24h-automatic-pencil-waterproof-brow-pencil/p-16104259/",
"id": "16104259",
"gender": "W",
"brand": "3INA",
"cats": [
"Makeup",
"Eyebrows",
"Eyebrow Makeup"
]
}
{
"name": "The 24H Automatic Eye Pencil",
"url": "/3ina/the-24h-automatic-pencil-waterproof-brow-pencil/p-16132421/",
"id": "16132421",
"gender": "W",
"brand": "3INA",
"cats": [
"Makeup",
"Eyebrows",
"Eyebrow Makeup"
]
}我试图添加或乘以数组,但它不起作用。
当我添加特定的键( {name, url, id, gender} + {brand, cats} )时,它会显示键的位置,但是它将值设置为null。
你能帮我找到解决办法吗?
发布于 2022-10-14 20:21:07
通过使用-n和[inputs],或者通过使用-s,将流转换为数组,然后将last项添加到除最后一个.[:-1][]之外的所有。若要筛选空对象,请将其与select进行比较或检查其length。
jq -n '[inputs] | (.[:-1][] | select(. != {})) + last'jq -s '(.[:-1][] | select(length > 0)) + last'输出:
{
"name": "The 24H Automatic Eye Pencil",
"url": "/3ina/the-24h-automatic-pencil-waterproof-brow-pencil/p-16132421/",
"id": "16132421",
"gender": "W",
"brand": "3INA",
"cats": [
"Makeup",
"Eyebrows",
"Eyebrow Makeup"
]
}
{
"name": "The 24H Automatic Eye Pencil",
"url": "/3ina/the-24h-automatic-pencil-waterproof-brow-pencil/p-16104273/",
"id": "16104273",
"gender": "W",
"brand": "3INA",
"cats": [
"Makeup",
"Eyebrows",
"Eyebrow Makeup"
]
}发布于 2022-10-14 21:01:31
如果流不适合您的内存,则必须使用--stream选项将输入分解为可管理的部分。但是,通过流处理,您不能同时轻松地访问不同的部分。因此,您需要一次传递来检索最后一项,另一次则将其添加到其他项。因为在处理过程中,没有指示项是最后一个项,所以您可以过滤掉那些与所添加的项完全匹配的项(这也很方便地过滤掉以前的空项)。
jq --argfile last <(jq 'reduce inputs as $in (.; $in)' big.json) --stream \
-n 'fromstream(inputs) + $last | select(. != $last)' big.json{
"name": "The 24H Automatic Eye Pencil",
"url": "/3ina/the-24h-automatic-pencil-waterproof-brow-pencil/p-16132421/",
"id": "16132421",
"gender": "W",
"brand": "3INA",
"cats": [
"Makeup",
"Eyebrows",
"Eyebrow Makeup"
]
}
{
"name": "The 24H Automatic Eye Pencil",
"url": "/3ina/the-24h-automatic-pencil-waterproof-brow-pencil/p-16104273/",
"id": "16104273",
"gender": "W",
"brand": "3INA",
"cats": [
"Makeup",
"Eyebrows",
"Eyebrow Makeup"
]
}https://stackoverflow.com/questions/74074360
复制相似问题