【问题标题】:JOLT Transformation - Nested Json ObjectJOLT 转换 - 嵌套的 Json 对象
【发布时间】:2021-11-16 20:56:47
【问题描述】:

我有一个这样的嵌套 JSON 对象:

{
  "0": {
    "testone": 72,
    "testtwo": 1
  },
  "1": {
    "testone": 72,
    "testtwo": 1
  },
  "2": {
    "testone": 72,
    "testtwo": 1
  }
}

我想转换成:

[
   {
      "one":72,
      "two":1
   },
   {
      "one":72,
      "two":1
   },
   {
      "one":72,
      "two":1
   }
]

如何使用 JOLT 实现这一目标?感谢您的投入。

【问题讨论】:

    标签: json jolt


    【解决方案1】:

    您可以使用以下规格

    [
      //exchange key and values  
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "$": "[&2].@(0)"
            }
          }
        }
      },
      //split the values by the prefix "test"
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "*": "=split('test',@(1,&))"
          }
        }
      },
      //get rid of the prefixes "test" 
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "*": "=join('',@(1,&))"
          }
        }
      },
      //re-exchange key and values  
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "$": "[&2].@(0)"
            }
          }
        }
      }
    ]
    

    编辑由于您的评论):不幸的是,没有直接的重命名方法,但可以通过使用以下方法单独编写每个键值对来提供解决方法一个

    [
      //rename innermost key names
      {
        "operation": "shift",
        "spec": {
          "*": {
            "testone": "&1.one",
            "testtwo": "&1.two"
          }
        }
      },
      //get rid of the object keys
      {
        "operation": "shift",
        "spec": {
          "*": ""
        }
      }
    ]
    

    【讨论】:

    • 哇,这完美!如果你不介意我怎么能简单地将我的列重命名为testOnecustomNam 而没有任何规则,通过使用某种颠簸rename 操作。谢谢!
    • 谢谢你!欣赏!
    猜你喜欢
    • 1970-01-01
    • 2023-03-06
    • 2021-09-26
    • 1970-01-01
    • 2023-01-16
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多