如何使用Jolt将N个对象的json数组与键展平?

时间:2019-05-01 20:41:27

标签: flatten jolt

我有一个非常简单的用例,但似乎无法将注意力转移到将使转置成为可能的移位规范上。主要是将树层次结构简化为简单的输出数组。

如何将输入的JSON转换为


    @objc func yesswiped (recognizer: UISwipeGestureRecognizer){
        print("swipe pls")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        setNavigationBar()


        let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(yesswiped))
        let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(yesswiped))

        leftSwipe.direction = .left
        rightSwipe.direction = .right

        self.view.addGestureRecognizer(leftSwipe)
        self.view.addGestureRecognizer(rightSwipe)
}

进入此输出:

{
  "123": [
    {
      "VALUE_ONE": "Y",
      "VALUE_TWO": "12"
    },
    {
      "VALUE_ONE": "N",
      "VALUE_TWO": "2"
    }
  ],
  "456": [
    {
      "VALUE_ONE": "Y",
      "VALUE_TWO": "35"
    }
  ]
}

请注意,我不知道每个对象的键(“ 456”,“ 123”等)是什么,因此震动规格必须足够通用才能转换任何键,只有已知的字段名称才是“ VALUE_ONE”和“ VALUE_TWO”。

1 个答案:

答案 0 :(得分:0)

此步骤将达到目的:

    [
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "VALUE_ONE": "&2.[&1].value_one_new_name",
          "VALUE_TWO": "&2.[&1].value_two_new_name",
          "$1": "&2.[&1].key"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[]"
      }
    }
  }
]