如果条件为真,则获取数组JOLT Spec

问题描述

我是JOLT的新手,在jolt sepc转换后获取正确的JSON时遇到了一些问题。以下是给定的json,使用的Jolt Spec和预期的输出。但是,当我处理Jolt Spec时,在JSON输出中没有得到ReportedQtyUom和ReportedQty数据。请任何人让我知道我要去哪里了。谢谢

给出JSON

[
  {
    "MaterialId": "na-103437","LocationId": "0TB9","OHReportingDate": "2020-08-18T20:57:16Z","TankNumber": "KTLA","stocks": [
      {
        "stockStatus": "OnHand","quantity": [
          {
            "uom": "KG","amount": "0.000","ISOUom": "KGM"
          }
        ]
      },{
        "stockStatus": "damaged",{
        "stockStatus": "Qualityinspection","ISOUom": "KGM"
          }
        ]
      }
    ]
  },{
    "MaterialId": "na-103437","TankNumber": "KTLJ","ISOUom": "KGM"
          }
        ]
      }
    ]
  }
]

使用了JOLT规范

[
  {
    // Keeping the same three nested arrays structure,//  build all the output "elements" into a parallel 
    //  structure,creating a "header" object and an 
    //  array of attachment objects.
    "operation": "shift","spec": {
      "*": {
        "MaterialId": "[&1].MaterialId","LocationId": "[&1].LocationId","OHReportingDate": "[&1].OHReportingDate","TankNumber": "[&1].TankNumber","stocks": {
          "*": {
            "stockStatus": {
              "OnHand": {
                "quantity": {
                  "*": {
                    "@(0,uom)": "[&7].POI","@(0,amount)": "[&7].XYZ"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

预期

[ {
  "MaterialId" : "na-103437","LocationId" : "0TB9","OHReportingDate" : "2020-08-18T20:57:16Z","TankNumber" : "KTLA","ReportedQtyUom" : "KG","ReportedQty": "0.000"
},{
  "MaterialId" : "na-103437","TankNumber" : "KTLJ","ReportedQty" : "0.000"
}]

解决方法

您的规格将以onHand的形式检查stockStatus并尝试将其移动一个级别,因为OnHand为String,对于写入数量内的班次将返回null。 当您检查stockStatus为onHand时,请向后遍历2级并尝试移动这些值。

[
  {
    "operation": "shift","spec": {
      "*": {
        "MaterialId": "[&1].MaterialId","LocationId": "[&1].LocationId","OHReportingDate": "[&1].OHReportingDate","TankNumber": "[&1].TankNumber","stocks": {
          "*": {
            "stockStatus": {
              "OnHand": {
                "@(2,quantity.[0].uom)": "[&5].ReportedQtyUom","@(2,quantity.[0].amount)": "[&5].ReportedQty"
              }
            }
          }
        }
      }
    }
  }
]