json JOLT转换---从元素添加标题

yquaqz18  于 2023-05-23  发布在  其他
关注(0)|答案(1)|浏览(173)

下面是JSON对象:

{
  "trans-details": [
    {
      "customernum": "41700013",
      "crmordernum": "SO-000048949",
      "customerordernum": "Over delivery BRAZI",
      "itemnumber": "A0002309953",
      "status": 3,
      "pagecount": 64,
      "orderstatus": 3,
      "requestedqty": 0
    },
    {
      "customernum": "41700013",
      "crmordernum": "SO-000049948",
      "customerordernum": "Over delivery test ZA",
      "itemnumber": "A0002309911",
      "status": 3,
      "pagecount": 64,
      "orderstatus": 3,
      "requestedqty": 0
    }
  ]
}

预期输出

{
  "Header": {
    "Pagenumber": "1",
    "Countryid": "7002",
    "First Page": "true",
    "Last Page": "false",
    "countryId": 675,
    "dealerNumber": 675000,
    "Total Pages": "64" //(PAGE COUNT)
  },
  "TRANS-DETAILS": [
    {
      "customernum": "41700013",
      "crmordernum": "SO-000048949",
      "customerordernum": "Over delivery BRAZI",
      "itemnumber": "A0002309953",
      "status": 3,
      "orderstatus": 3,
      "requestedqty": 0
    },
    {
      "customernum": "41700013",
      "crmordernum": "SO-000048949",
      "customerordernum": "Over delivery BRAZI",
      "itemnumber": "A0002309953",
      "status": 3,
      "orderstatus": 3,
      "requestedqty": 0
    }
  ]
}

"PAGECOUNT:64"应该只显示在头部分,而不是在元素中。请帮助。谢谢

liwlm1x9

liwlm1x91#

您可以使用以下转换规范

[
  { // generate an extra attribute to represent the key of the array in order 
    // to be able to process within the next specs 
    "operation": "shift",
    "spec": {
      "*": {
        "$": "ttl",
        "@": "&"
      }
    }
  },
  { // get the uppercase title
    "operation": "modify-overwrite-beta",
    "spec": {
      "ttl": "=toUpper(@(1,&))"
    }
  },
  { // replace of the array's title with uppercase form of it
    "operation": "shift",
    "spec": {
      "tr*": "@1,ttl"
    }
  },
  { // take pagecount to an outer object, namely "Header"
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "pagecount": "Header.Total Pages",
          "*": {
            "@": "&3[&2].&1"
          }
        }
      }
    }
  },
  { // pick only one(the first) Total Pages value 
    "operation": "cardinality",
    "spec": {
      "Header": {
        "*": "ONE"
      }
    }
  },
  { // add the attributes with fixed values
    "operation": "modify-overwrite-beta",
    "spec": {
      "Header": {
        "Pagenumber": "1",
        "Countryid": "7002",
        "First Page": "true",
        "Last Page": "false",
        "countryId": 675,
        "dealerNumber": 675000
      }
    }
  },
  { // sort the objects in alphabetical order
    "operation": "sort"
  }
]

相关问题