英文:
Jolt Transform JSON Spec for Array Input
问题
以下是您提供的内容的翻译:
我正在尝试使用以下规范进行 JOLT 位移操作,但未能正常工作。不确定我做了什么错误。在这种情况下需要帮助。输出 JSON 以对象形式而不是数组形式呈现,并且位移也未按预期工作。
输入: [
{
"Header": {
"Number": 1,
"Id": "JO"
},
"Name": "John"
},
{
"Header": {
"Number": 2,
"Id": "JS"
},
"Name": "Justin"
}
]
规范: [
{
"operation": "shift",
"spec": {
"*": {
"Header": "Header",
"Name": "Header.Name"
}
}
}
]
期望输出: [
{
"Header": {
"Number": 1,
"Id": "JO",
"Name": "John"
}
},
{
"Header": {
"Number": 2,
"Id": "JS",
"Name": "Justin"
}
}
]
实际输出: {
"Header": [
{
"Number": 1,
"Id": "JO",
"Name": "John"
},
{
"Number": 2,
"Id": "JS"
}
]
}
英文:
I am trying to do JOLT shift operation with below spec which is not working. Not sure what mistake I have done. Need help in this case. Output JSON is coming as an object instead of Array and shift also not working as expected.
Input : [
{
"Header": {
"Number": 1,
"Id": "JO"
},
"Name": "John"
},
{
"Header": {
"Number": 2,
"Id": "JS"
},
"Name": "Justin"
}
]
Spec : [
{
"operation": "shift",
"spec": {
"*": {
"Header": "Header",
"Name": "Header.Name"
}
}
}
]
Expected Output : [
{
"Header": {
"Number": 1,
"Id": "JO",
"Name": "John"
}
},
{
"Header": {
"Number": 2,
"Id": "JS",
"Name": "Justin"
}
}
]
Actual Output : {
"Header" : [ {
"Number" : 1,
"Id" : "JO",
"Name" : "John"
}, {
"Number" : 2,
"Id" : "JS"
} ]
}
答案1
得分: 2
你还必须指定"Header"
对象在数组内部1。
此外,数组中放置"Header"
对象的索引,对应数组的每个元素。
以下是规范的示例(使用[&1]
- 与数组组合的ampersand通配符):
[
{
"operation": "shift",
"spec": {
"*": {
"Header": "[&1].Header",
"Name": "[&1].Header.Name"
}
}
}
]
来源:
- Shiftr.java javadocs:
- 其他答案:“如何使用Jolt转换数组?”
- 在jolt仓库中链接的demo应用,用于测试规范
英文:
You have to also specify that the "Header"
object is inside the array.
Moreover, the index of the array where you place the "Header"
object for each of the element of the array.
That's what the spec below does (using the [&1]
- apmersand wildcard combined with array):
[
{
"operation": "shift",
"spec": {
"*": {
"Header": "[&1].Header",
"Name": "[&1].Header.Name"
}
}
}
]
Sources:
- Shiftr.java javadocs:
- Other answer: "How do I transform an array using Jolt?"
- Demo application linked in the jolt repo to test the spec
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论