将字符串转换为布尔值在JSON数组中的JOLT转换:

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

JOLT Transformation : Convert string to Boolean in JSON Array

问题

我想要将字符串值根据条件替换为布尔值。如果 IsCompleted 的值为 Yes,则输出值应为 true,如果为 No,则输出值应为 false

输入

[
  {
    "Id": 1,
    "Name": "Sumit",
    "IsCompleted": "Yes"
  },
  {
    "Id": 2,
    "Name": "Sumit",
    "IsCompleted": "No"
  }
]

JOLT 规范

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "IsCompleted": {
          "Yes": {
            "#true": "IsCompleted"
          },
          "No": {
            "#false": "IsCompleted"
          }
        },
        "*": "&"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "IsCompleted": "=toBoolean"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@": "[].&"
      }
    }
  }
]

预期输出

[
  {
    "Id": 1,
    "Name": "Sumit",
    "IsCompleted": true
  },
  {
    "Id": 2,
    "Name": "Sumit",
    "IsCompleted": false
  }
]

实际输出

[
  {
    "Id": [1, 2]
  },
  {
    "Name": ["Sumit", "Sumit"]
  },
  {
    "IsCompleted": [true, false]
  }
]
英文:

I want to replace the string value with a boolean based on the condition. If the IsCompleted value is Yes then the output value should be true if It is No then the output value should be false.

Input

[
  {
    "Id": 1,
    "Name": "Sumit",
    "IsCompleted": "Yes"
  },
  {
    "Id": 2,
    "Name": "Sumit",
    "IsCompleted": "No"
  }
]

JOLT Spec

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "IsCompleted": {
          "Yes": {
            "#true": "IsCompleted"
          },
          "No": {
            "#false": "IsCompleted"
          }
        },
        "*": "&"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "IsCompleted": "=toBoolean"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@": "[].&"
      }
    }
  }
]

Expected Output

[
  {
    "Id": 1,
    "Name": "Sumit",
    "IsCompleted": true
  },
  {
    "Id": 2,
    "Name": "Sumit",
    "IsCompleted": false
  }
]

Actual Output

[
  {
    "Id": [1, 2]
  },
  {
    "Name": ["Sumit", "Sumit"]
  },
  {
    "IsCompleted": [true, false]
  }
]

答案1

得分: 1

I just changed your spec to the true thing that should be. So you can use the following JOLT spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[&1].&",
        "IsCompleted": {
          "Yes": "#true [&3].&2",
          "No": "#false [&3].&2"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "IsCompleted": "=toBoolean"
      }
    }
  }
]
英文:

I just changed your spec to the true thing that should be. So you can use the following JOLT spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[&1].&",
        "IsCompleted": {
          "Yes": {
            "#true": "[&3].&2"
          },
          "No": {
            "#false": "[&3].&2"
          }
        }
      }
    }
	},
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "IsCompleted": "=toBoolean"
      }
    }
  }
]

huangapple
  • 本文由 发表于 2023年4月19日 18:12:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053281.html
匿名

发表评论

匿名网友

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

确定