我有一个 JSON 实体流。这是其中之一称为“信息”:
null
null
{
"id": "qwefhu214o",
"number": "2346",
"date": "28.01.2019"
}
null
null
{
"id": "esg324lif",
"number": "1378",
"date": "29.05.2019"
}
{
"id": "gwrs853sdf",
"number": "4487",
"date": "20.12.2019"
}
我想从嵌套的 json 中提取键,所以它看起来像这样:null
null
{
"info_id": "qwefhu214o",
"info_number": "2346",
"info_date": "28.01.2019"
}
null
null
{
"info_id": "esg324lif",
"info_number": "1378",
"info_date": "29.05.2019"
}
{
"info_id": "gwrs853sdf",
"info_number": "4487",
"info_date": "20.12.2019"
}
我试试这个,但它不起作用:jqr::jq('.[].info |= with_entries(.key |= "info_" + .)')
它说:Error: lexical error: invalid char in json text.
NA
(right here) ------^
我猜是因为 NULL。我怎么能那样做?我应该把“?”放在某个地方吗?在代码中?该代码适用于存在 np NULL 的情况
最佳答案
显示的“嵌套 JSON”实际上是一个 JSON 实体流,
以下过滤器将产生所需的结果:
if . then with_entries(.key |= "info_" + .) else . end
您可以根据需要轻松修改此过滤器。
关于重命名 JSON 实体流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62747608/