无法使用JOLT解析器获得预期输出。

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

Unable to get expected output using JOLT parser

问题

我有以下input

[
  {
    "ownerId": "XTV7I728",
    "vehicleId": "999",
    "engines": [
      {
        "engineId": "1",
        "engineName": "Standard Engine",
        "engineHp": 300
      },
      {
        "engineId": "2",
        "engineName": "Custom Engine",
        "engineHp": 450
      }
    ]
  }
]

我需要如下的expected输出:

[{
  "ownerId": "XTV7I728",
  "vehicleId": "999",
  "engines": {
    "engineId": "1",
    "engineName": "Standard Engine",
    "engineHp": 300
  }
},
{
  "ownerId": "XTV7I728",
  "vehicleId": "999",
  "engines": {
    "engineId": "2",
    "engineName": "Custom Engine",
    "engineHp": 450
  }
}]

我尝试了以下的spec但是报错了:

[{
  "ownerId": "ownerId",
  "*": {
    "vehicleId": "vehicleId",
    "engineId": "engineId",
    "engineHp": "engineHp"
  }
}]

错误信息:

运行转换时出错。

JOLT Chainr 'operation' 必须在索引0处实现Transform或ContextualTransform。

请告诉我我哪里做错了。

英文:

I have below input:

[
  {
    "ownerId": "XTV7I728",
    "vehicleId": "999",
    "engines": [
      {
        "engineId": "1",
        "engineName": "Standard Engine",
        "engineHp": 300
      },
      {
        "engineId": "2",
        "engineName": "Custom Engine",
        "engineHp": 450
      }
    ]
  }
]

I need the expected output as below:

    [{
      "ownerId": "XTV7I728",
      "vehicleId": "999",
      "engines":{
      "engineId": "1",
      "engineName": "Standard Engine",
      "engineHp": 300
}
    },
    {
      "ownerId": "XTV7I728",
      "vehicleId": "999",
"engines":{
      "engineId": "2",
      "engineName": "Custom Engine",
      "engineHp": 450
    }
}] 

I tried below spec but it throws an error:

[{
  "ownerId": "ownerId",
  "*": {
    "vehicleId": "vehicleId",
    "engineId": "engineId",
    "engineHp": "engineHp"
  }
}]

Error:

Error running the Transform.

JOLT Chainr 'operation' must implement Transform or ContextualTransform at index:0.  

Please let me know where I went wrong.

答案1

得分: 1

您需要指定操作(如果使用 Chain spec),然后是一个执行实际“遍历”对象树的 `spec` 部分。应该像这样工作:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "engines": {
          "*": {
            "@(2,ownerId)": "[#2].ownerId",
            "@(2,vehicleId)": "[#2].vehicleId",
            "*": "[#2].&"
          }
        }
      }
    }
  }
]
英文:

You have to specify the operation (if using a Chain spec) and then a spec section that does the actual "walking" of your object tree. This should work:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "engines": {
          "*": {
            "@(2,ownerId)": "[#2].ownerId",
            "@(2,vehicleId)": "[#2].vehicleId",
            "*": "[#2].&"
          }
        }
      }
    }
  }
]

huangapple
  • 本文由 发表于 2020年5月19日 21:11:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/61891901.html
匿名

发表评论

匿名网友

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

确定