英文:
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"
}
}
}
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论