如何在Jolt中比较两个字符串值

huangapple go评论68阅读模式
英文:

How to compare two string values in jolt

问题

  1. 如果 from_Storeto_Store 的值不匹配,则在输出 JSON 中保留该映射的完整元素。

  2. 如果值匹配,则整个元素不应包含在输出中。

期望的输出 JSON

[
  {
    "from_Store": "Test2",
    "to_Store": "Test3",
    "items": [
      {
        "UPC": "8240959370210",
        "shippedQuantity": 1
      }
    ]
  }
]

在 Jolt 中是否有比较字符串的函数?

英文:

I want to filter out the input JSON based on two field values of input JSON.

Input JSON

[
  {
    "from_Store": "Test1",
    "to_Store": "Test1",
    "items": [
      {
        "UPC": "8240959370255",
        "shippedQuantity": 1
      }
    ]
  },
  {
    "from_Store": "Test2",
    "to_Store": "Test3",
    "items": [
      {
        "UPC": "8240959370210",
        "shippedQuantity": 1
      }
    ]
  }
]
  1. If the values of the from_Store and to_Store do not match, then keep that complete element of the map in the output JSON.
  2. If the values match, then the entire element should not be in the output

Expected output JSON

[
  {
    "from_Store": "Test2",
    "to_Store": "Test3",
    "items": [
      {
        "UPC": "8240959370210",
        "shippedQuantity": 1
      }
    ]
  }
]

Is there any function to compare the strings in Jolt?

Any help would be appreciated!

答案1

得分: 1

你可以为每个节点交换to_Storefrom_Store属性的键值对,无论是否形成数组。如果形成数组,那么我们将只获得1个元素,例如,属性的值相等。否则,我们将获得2个元素,因此,我们将它们保留为结果,例如

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*_*": "Cnt[&1].Count.@0" // 用唯一的下划线代表两个属性,例如 "from_store" 和 "to_store"
      },
      "@": "Original" // 复制初始 JSON 值
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "Cnt": {
        "*": {
          "Count": "=size(@(1,&))"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "Count": "&1",
          "*": "&1.&"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "1": { // 第二个组件
          "2": { // 保留值如果它的值是 2
            "@(2,[0])": "[]"
          }
        }
      }
    }
  }
]

在网站http://jolt-demo.appspot.com/上的演示如下:

如何在Jolt中比较两个字符串值

英文:

You can exchange key-value pairs for to_Store and from_Store attributes whether an array is formed or not per each node. If array is formed, then we'll get only 1 element, eg. values of the attributes are equal. Otherwise, we'll get 2 elements, so, we'll keep them as result such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*_*": "Cnt[&1].Count.@0" // to represent both of the attributes with unique underscores, eg. "from_store" and "to_store"
      },
      "@": "Original" // replicate the initial JSON value
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "Cnt": {
        "*": {
          "Count": "=size(@(1,&))"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "Count": "&1",
          "*": "&1.&"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "1": { // the second component
          "2": { // keep the value if its value is 2
            "@(2,[0])": "[]"
          }
        }
      }
    }
  }
]

the demo on the site http://jolt-demo.appspot.com/ is

如何在Jolt中比较两个字符串值

huangapple
  • 本文由 发表于 2023年7月10日 19:38:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653353.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定