问题描述
我试图在将JSON输出移动到较低数组时对其进行扁平化。在下面的示例中,我需要将所有内容都保留在“值”下,还将“时间戳记”移到值结果中。有什么提示或文档可以帮助您吗?
谢谢!
输入:
{ "readings": [
{
"meter": "78522546","records": [
{
"timestamp": "22/02/2019 17:10:00","values": {
"DateTime": 1550848206,"ExportWh": 2136.5,}
},{
"timestamp": "22/02/2019 17:15:00",}
}
]
},{
"meter": "78522548","records": [
{
"timestamp": "22/02/2019 17:15:00","values": {
"DateTime": 1550848246,"ExportWh": 0,{
"meter": "78522570","values": {
"DateTime": 1550848226,"ExportWh": 3152012603293.037,{
"meter": "78522572","values": {
"DateTime": 1550848236,"ExportWh": 1112.172,{
"meter": "78522589","values": {
"DateTime": 1550848217,}
}
]
} ] }
所需的输出:
{
"timestamp": "22/02/2019 17:10:00","DateTime": 1550848206,"ExportWh": 2136.5
},{
"timestamp": "22/02/2019 17:15:00","DateTime": 1550848246,"ExportWh": 0
},{
"timestamp": "22/02/2019 17:10:00","DateTime": 1550848226,"ExportWh": 3152012603293.037
},"DateTime": 1550848236,"ExportWh": 1112.172
},"DateTime": 1550848217,"ExportWh": 0
}
这是我对规范的理解程度,但从这里继续绕行:
[
{
"operation": "shift","spec": {
"readings": {
"*": {
"records": {
"*": {
"timestamp": "&","@(0,values)": ""
}
}
}
}
}
}
]
解决方法
检查此规范
[
{
"operation": "shift","spec": {
"readings": {
"*": {
"records": {
"*": {
"values": {
"@": "[].@(2,timestamp)"
}
}
}
}
}
}
},{
"operation": "shift","spec": {
"*": {
"*": {
"@": "[]","$": "[&2].timestamp"
}
}
}
}
]