震动转换JSON数组输入的规范

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

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"
      }
    }
  }
]

来源:

  1. Shiftr.java javadocs
  2. 其他答案:“如何使用Jolt转换数组?”
  3. 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:

  1. Shiftr.java javadocs:
  2. Other answer: "How do I transform an array using Jolt?"
  3. Demo application linked in the jolt repo to test the spec

huangapple
  • 本文由 发表于 2020年8月27日 14:15:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63610203.html
匿名

发表评论

匿名网友

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

确定