Jolt Transformation: Un-nest object of form "key":{"value":"xyz"} to "key":"value"

huangapple go评论63阅读模式
英文:

Jolt Transformation: Un-nest object of form "key":{"value":"xyz"} to "key":"value"

问题

以下是你要翻译的内容:

Desired output

[
  {
    "key1": "abc",
    "key2": "xyz"
  },
  {
    "key1": "123",
    "key2": "456"
  }
]
英文:

I need to unwrap JSON objects in order to reduce the nesting of input like

[
  {
    "key1": {
      "value": "abc"
    },
    "key2": {
      "value": "xyz"
    }
  },
  {
    "key1": {
      "value": "123"
    },
    "key2": {
      "value": "456"
    }
  }
]

Instead it can just map straight to the value without the unnecessary object nesting.

Input JSON

[
  {
    "typedValues": {
      "key1": {
        "value": "abc"
      },
      "key2": {
        "value": "xyz"
      }
    }
  },
  {
    "typedValues": {
      "key1": {
        "value": "123"
      },
      "key2": {
        "value": "456"
      }
    }
  }
]

My spec attempt

I was able to remove the "typedValues" wrapping but unable to achieve the key:value reduction.

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "typedValues": {
          "@": ""
        }
      }
    }
  }

]

Output from attempt

[
  {
    "key1": {
      "value": "abc"
    },
    "key2": {
      "value": "xyz"
    }
  },
  {
    "key1": {
      "value": "123"
    },
    "key2": {
      "value": "456"
    }
  }
]

Desired output

[
  {
    "key1": "abc",
    "key2": "xyz"
  },
  {
    "key1": "123",
    "key2": "456"
  }
]

答案1

得分: 0

您可以使用shift变换,例如:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "value": "&3.&1" // &3: 上溯树3级以获取包装对象的最外层索引 // &1: 上溯1级后复制键1/2
          }
        }
      }
    }
  },
  { // 用于去除对象键
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

或者使用@value&通配符进行快速匹配,该通配符表示键值的当前级别复制:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": { // typedValues
          "*": {
            "@value": "[&3].&"
          }
        }
      }
    }
  }
]
英文:

You can use a shift transformaion such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "value": "&3.&1" // &3 : going 3 levels up the tree to grab the outermost indexes of the wrapper objects
          }                  // &1 : replicating key 1/2 after going 1 level up
        }
      }
    }
  },
  { // to get rid of the object keys
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

or shorly match @value with & wildcard which represents the current level replication of the key values

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": { //typedValues
          "*": {
            "@value": "[&3].&"
          }
        }
      }
    }
  }
]

huangapple
  • 本文由 发表于 2023年2月18日 02:09:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487871.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定