简体   繁体   English

JSONata,如何将单个元素转换为嵌套的 arrays?

[英]JSONata, how to transform single elements into nested arrays?

I am trying to convert query results into the correct form to display in a chart in node-red.我正在尝试将查询结果转换为正确的形式以显示在节点红色的图表中。

The data comes in an array results of objects and each object contains the details of one time series.数据来自对象数组results ,每个 object 包含一个时间序列的详细信息。

For the chart to display all series together the data needs to be converted into an "array of arrays of objects (the actual x/y points)".为了使图表一起显示所有系列,需要将数据转换为“arrays 对象数组(实际 x/y 点)”。

I am able to to this if the source JSON contains more than one time series, but if there is only one it fails.如果源 JSON 包含多个时间序列,我可以做到这一点,但如果只有一个它会失败。

The working example looks like this, whereas the array results can contain n (between 0 and N) elements.工作示例如下所示,而数组results可以包含 n 个(介于 0 和 N 之间)元素。 (See example with n=2 in JSONata Exerciser https://try.jsonata.org/2GqaqME4F ) (参见 JSONata 练习器 https://try.jsonata.org/2GqaqME4F中 n=2 的示例)

{
  "payload": {
    "results": [
      {
        "statement_id": 0,
        "series": [
          {
            "name": "table1",
            "columns": [
              "time",
              "temperature 1"
            ],
            "values": [
              [
                "2022-08-18T14:55:00Z",
                1.1
              ],
              [
                "2022-08-18T15:00:00Z",
                1.2
              ]
            ]
          }
        ]
      },
      {
        "statement_id": 1,
        "series": [
          {
            "name": "table1",
            "columns": [
              "time",
              "temperature 2"
            ],
            "values": [
              [
                "2022-08-18T14:55:00Z",
                2.1
              ],
              [
                "2022-08-18T15:00:00Z",
                2.2
              ]
            ]
          }
        ]
      }
    ]
  }
}

I'm converting the data right now with this JSONata我现在正在用这个 JSONata 转换数据

[
    {
        "series": [ $.payload.results.($.series.columns[1]) ],
        "labels": [ $.payload.results.("") ],
        "data": [
            $.payload.results.[series.values.( $[1] != null ? {"x": $[0].$toMillis(),"y": $[1]} )]
        ]
    }
]

and it gives me the expected results for n>1:它给了我 n>1 的预期结果:

[
  {
    "series": [
      "temperature 1",
      "temperature 2"
    ],
    "labels": [
      "",
      ""
    ],
    "data": [
      [
        {
          "x": 1660834500000,
          "y": 1.1
        },
        {
          "x": 1660834800000,
          "y": 1.2
        }
      ],
      [
        {
          "x": 1660834500000,
          "y": 2.1
        },
        {
          "x": 1660834800000,
          "y": 2.2
        }
      ]
    ]
  }
]

For the chart to display all series together it is important that data is an "array of arrays of objects (the actual x/y points)".要使图表一起显示所有系列,重要的是data是“对象的 arrays 数组(实际 x/y 点)”。

For n=1 (see for n=1 in JSONata Exerciser https://try.jsonata.org/F0prxQW4o )对于 n=1(参见 JSONata 练习器 https://try.jsonata.org/F0prxQW4o中的 n=1)

{
  "payload": {
    "results": [
      {
        "statement_id": 0,
        "series": [
          {
            "name": "table1",
            "columns": [
              "time",
              "temperature 1"
            ],
            "values": [
              [
                "2022-08-18T14:55:00Z",
                1.1
              ],
              [
                "2022-08-18T15:00:00Z",
                1.2
              ]
            ]
          }
        ]
      }
    ]
  }
}

the result is结果是

[
  {
    "series": [
      "temperature 1"
    ],
    "labels": [
      ""
    ],
    "data": [
      {
        "x": 1660834500000,
        "y": 1.1
      },
      {
        "x": 1660834800000,
        "y": 1.2
      }
    ]
  }
]

which is no longer an "array of arrays of objects", but an "array of objects".它不再是“对象数组 arrays”,而是“对象数组”。

Result should be结果应该是

[
  {
    "series": [
      "temperature 1"
    ],
    "labels": [
      ""
    ],
    "data": [
      [        //<--- This parenthesis is missing
        {
          "x": 1660834500000,
          "y": 1.1
        },
        {
          "x": 1660834800000,
          "y": 1.2
        }
      ]        //<--- This parenthesis is missing
    ]
  }
]

Could somebody tell me how a JSONata would have to look like to work for all n between 0 and N?有人能告诉我 JSONata 看起来如何适用于 0 和 N 之间的所有 n 吗?

You can force singleton sequences to be output as an array using the empty square brackets operator [] as described here .您可以使用此处所述的空方括号运算符[]将 singleton 序列强制为 output 作为数组。 Your expression would be:你的表达是:

[
    {
        "series": [ $.payload.results.($.series.columns[1]) ],
        "labels": [ $.payload.results.("") ],
        "data": [
            $.payload.results.[series.values.( $[1] != null ? {"x": $[0].$toMillis(),"y": $[1]} )][]
        ]
    }
]

See https://try.jsonata.org/kaXX2dhybhttps://try.jsonata.org/kaXX2dhyb

You can add [] after the $.payload.results to force JSONata to "keep array" (see docs about this behavior here: https://docs.jsonata.org/predicate#singleton-array-and-value-equivalence ).您可以在$.payload.results之后添加[]以强制 JSONata“保留数组”(请参阅此处有关此行为的文档: https://docs.jsonata.org/predicate#singleton-array-and-value-equivalence ) .

Here's the modified solution: https://stedi.link/1625Zpj这是修改后的解决方案: https://stedi.link/1625Zpj

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM