按语言环境对数组项进行分组 - 使用 JSONata 在 Node JS 中进行 JSON 转换

Grouping of array items by locale - JSON transformation in Node JS using JSONata

我卡在 JSON 中的 JSON 转换中。对此的任何帮助表示赞赏。

除了 JSONata 之外,是否有任何库可以在 Node JS 中实现这一点。

而且可以直接转换数据还是需要在JS中操作?

我的输入 json 看起来像

  "species": "mammels",
  "title": [
    {
      "locale": "en-GB",
      "value": "GB Title"
    },
    {
      "locale": "en-US",
      "value": "US Title"
    },
    {
      "locale": "en-RS",
      "value": "RS Title"
    }
  ],
  "shortDescription": [
    {
      "locale": "en-GB",
      "value": "GB shortDesc"
    },
    {
      "locale": "en-US",
      "value": "US shortDesc"
    }
  ],
  "longDescription": [
    {
      "locale": "en-GB",
      "value": "GB longDesc"
    }
  ],
  "zoos": [
        {
            "location": "Englang",
            "value": "National Zoo"
        },
        {
            "location": "Ireland",
            "value": "Nature home"
        }
    ],
  }

我需要如下转换后的输出。

   "dataSet":[
      {
         "data":[
            {
               "key":"Territory",
               "value":"SG"
            },
            {
               "key":"studios",
               "value":"National Zoo,Nature home"
            }
         ],
         "locale":"none"
      },
      {
         "data":[
            {
               "key":"Title",
               "value":"GB Title"
            },
            {
               "key":"shortDescription",
               "value":"GB shortDesc"
            },
            {
               "key":"longDescription",
               "value":"GB longDesc"
            }
         ],
         "locale":"en-GB"
      },
      {
         "data":[
            {
               "key":"Title",
               "value":"US Title"
            },
            {
               "key":"shortDescription",
               "value":"US shortDesc"
            }
         ],
         "locale":"en-US"
      },
      {
         "data":[
            {
               "key":"Title",
               "value":"RS Title"
            }
         ],
         "locale":"en-RS"
      }
   ]
   }
    {
    "dataSet":[{
    "territory": {"territory":species} ~> $each(function($v, $k) {
    {
        "locale": "none",
        "data": [{
        "key":$k,
        "value":$v}
        ]
    }
  }),    
    "title": $spread(title {
        locale: [ $ ~> | $ | {"key":"Title"}, "locale" | ]
    }).{
        "locale": $replace($keys()[0],"-","_"),
        "data": *
    },
    "shortDescription": $spread(shortDescription {
        locale: [ $ ~> | $ | {"key":"Synopsis"}, "locale" | ]
    }).{
        "locale": $replace($keys()[0],"-","_"),
        "data": *
    },"longDescription": $spread(longDescription {
        locale: [ $ ~> | $ | {"key":"Description"}, "locale" | ]
    }).{
        "locale": $replace($keys()[0],"-","_"),
        "data": *
    },
    "studios": $spread(zoos {
        locale: [ $ ~> | $ | {"key":"studios"}, "locale" | ]
    }).{
        "locale": $replace($keys()[0],"-","_"),
        "data": *
    }
}.[title,shortDescription,longDescription,studios,territory]{locale:data} ~> $each(function($v, $k) {
    {
      "data": [$v],
      "locale": $k
    }
  })]
    }

Try it out here. https://try.jsonata.org/-ZRuUcUwM