You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写JSONata查询提取Products对象中Type*和Category*的唯一值

JSONata Query to Extract and Deduplicate Type/Category Values into Items Array

Query Statement

{
  "Items": data.Products.(
    $area := Area;
    $key := Key;
    $filteredValues := $filter($$.keys(), $ ~> /^(Type|Category)/)
                       .($lookup($$, $))
                       ~> $unique();
    $filteredValues.{
      "Collection": $,
      "Area": $area,
      "Key": $key
    }
  )
}

Breakdown of the Query

  • Iterate through Products: Loop over each object in data.Products
  • Cache Context Values: Store the current product's Area and Key in variables ($area, $key) for reuse
  • Filter Relevant Keys: Use $filter with regex /^(Type|Category)/ to select only keys starting with "Type" or "Category"
  • Extract and Deduplicate Values: Fetch values for the filtered keys, then apply $unique() to remove duplicates
  • Map to Target Structure: Convert each deduplicated value into an object with Collection (the value itself), plus the cached Area and Key
  • Combine Results: All generated objects are automatically flattened into a single array under the Items field

Note on Input JSON Syntax

Your input JSON has missing commas that will cause parsing errors. Fix these lines:

  • First product: "Category_2" : "Peanut" → add a comma at the end: "Category_2" : "Peanut",
  • Second product: "Category_2" : "Pear" → add a comma at the end: "Category_2" : "Pear",

内容的提问来源于stack exchange,提问作者topto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 11:52:20