使用操作连接两个JSON键

x7rlezfr  于 2023-10-21  发布在  其他
关注(0)|答案(1)|浏览(86)

我有一个JSON:

[
  {
    "taskId": 26,
    "adsName": "test name 5",
    "cityId": 2,
    "regionId": 46,
    "biddingPrice": 50,
    "limit": 140
  },
  {
    "taskId": 27,
    "adsName": "test name 6",
    "cityId": 5,
    "regionId": null,
    "biddingPrice": 60,
    "limit": 150
  },
  {
    "taskId": 28,
    "adsName": "test name 7",
    "cityId": null,
    "regionId": 224,
    "biddingPrice": 60,
    "limit": 150
  }
]

我想把cityIdregionId合并成一个字段cities。顺序无关紧要,但regionId中的值应该始终为负(带负号)。所以,从cityId = 2, regionId = 46组合中,我期待“城市”:“2、-46”等
我期望:

[
  {
    "name": "test name 5",
    "all_limit": 140,
    "cities": "2, -46"
  },
  {
    "name": "test name 6",
    "all_limit": 150,
    "cities": "5"
  },
  {
    "name": "test name 7",
    "all_limit": 150,
    "cities": "-224"
  }
]
niwlg2el

niwlg2el1#

您可以使用以下转换

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "cts1": ["=notNull(@(1,cityId))", "=concat('-',@(1,regionId))"],
              // prefix "regionId" values with "-" if "cityId"s are null
        "cts2": ["=isNull(@(1,cityId))", "=concat(', -',@(1,regionId))"],
              // prefix "regionId" values with ", -" if "cityId"s are non-null
        "cities": "=concat(@(1,cts1),@(1,cts2))"
      }
    }
  },
  { // pick up the desired attributes while renaming them
    "operation": "shift",
    "spec": {
      "*": {
        "adsN*": "[#2].n&(0,1)",
        "limit": "[#2].all_&",
        // deliver them respectively by the upper nodes' indexes by using [#2] 
        // which traverses upwards by incrementing starts from 1
        "cities": {
          "*, -": { "$1": "[#4].&(1,1)" }, // cases in values ending with  ", -"
          "*": { "$1": "[#4].&1" } // others
        }
      }
    }
  },
  { // exchange the key-value pairs for the "cities" attribute
    "operation": "shift",
    "spec": {
      "*": {
        "all_limit|name": "[#2].&",
        "*": {
          "$": "[#3].@0"
        }
      }
    }
  }
]

网站http://jolt-demo.appspot.com/上的***演示***是:

相关问题