【问题标题】:JOLT transformation which involve linking by an attibutes value涉及通过属性值链接的 JOLT 转换
【发布时间】:2022-08-04 19:10:59
【问题描述】:

我需要使用 JOLT 将以下输入 JSON 转换为下面列出的格式。链接将由第一个数组元素的 deps.name 与第二个数组元素的 spec.name 完成。我对链接一无所知。谢谢你的帮助。

输入json

[
  {
    \"key\": \"Primary\",
    \"metadata\": {
      \"name\": \"35f8d9fac891\"
    },
    \"deps\": [
      {
        \"name\": \"e6ae6d29edf8\"
      }
    ],
    \"spec\": {
      \"vattr1\": \"vval1\",
      \"vattr2\": \"vval2\"
    }
  },
  {
    \"key\": \"Secondary\",
    \"metadata\": {
      \"name\": \"hp74z\"
    },
    \"spec\": {
      \"name\": \"e6ae6d29edf8\",
      \"nattr1\": \"nval1\",
      \"nattr2\": \"nval2\",
      \"deps\": {
        \"Name\": \"5505da219463\"
      }
    }
  }
]

预期的输出格式

{
  \"key\": \"Primary\",
  \"metadata\": {
    \"name\": \"35f8d9fac891\"
  },
  \"deps\": [
    {
      \"name\": {
        \"key\": \"Secondary\",
        \"metadata\": {
          \"name\": \"hp74z\"
        },
        \"spec\": {
          \"name\": \"e6ae6d29edf8\",
          \"nattr1\": \"nval1\",
          \"nattr2\": \"nval2\",
          \"deps\": {
            \"Name\": \"5505da219463\"
          }
        }
      }
    }
  ],
  \"spec\": {
    \"vattr1\": \"vval1\",
    \"vattr2\": \"vval2\"
  }
}

    标签: json transformation jolt


    【解决方案1】:

    从这个规范开始,以查看数组的这两个对象是否要在一个新数组中累积,键是一个通用名称(“e6ae6d29edf8”) 如

    {
      "operation": "shift",
      "spec": {
        "*": {
          "@(0)": "@(1,spec.name)",
          "@": "@(1,deps[&].name)"
        }
      }
    }
    

    如果这些名字(其中一个来自规格名称另一个来自deps[].name) 不匹配(去尝试一下!)

    然后使用

    {
      "operation": "shift",
      "spec": {
        "*": {
          "*": "common"
        }
      }
    }
    

    为了摆脱那个令人困惑的键名(“e6ae6d29edf8”) 通过将其转换为“常见的”.现在,我们得到两个对象的数组,需要替换“部门”与第二个对象的数组。调整转换规范旨在执行此类操作。然后,只选择“常见的”对象通过使用转移转换规范

    因此,完整的规格将是:

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "@(0)": "@(1,spec.name)",
            "@": "@(1,deps[&].name)"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": "common"
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": "=firstElement(@(1,&))",
          "SecondaryObject": "=lastElement(@(1,common))"
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "common": {
            "deps": {
              "*": "=(@(3,SecondaryObject))"
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "common": {
            "*": "&"
          }
        }
      }
    ]
    

    演示在网站上http://jolt-demo.appspot.com/

    编辑(考虑到存在多个要嵌入的对象的情况) :

    然后,您可以确定第一个规范中的属性"name_val" 用作即将在即将到来的规范中使用的通用标识符值,例如

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "@(0)": "@(1,spec.name)",
            "@": "@(1,deps[&].name)",
            "@(0,deps[&].name)": "name_val"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "@(2,name_val)": {
                "@1": "common[&2]"
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "common": {
            "0": "c1",
            "*": "c2"
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "c1": {
            "deps": {
              "*": {
                "*": "=(@(4,c2))"
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "c1": ""
        }
      }
    ]
    

    【讨论】:

    • 非常感谢巴巴罗斯的回复。代替使用 lastElement(@(1,common))",有没有办法我们可以使用 spec.name 匹配来选择它?因为,有可能在末尾添加一个额外的 json 元素。在这种情况下, lastElement 将不再是我正在寻找的那个。
    • 你是对的@guru,一个通用的案例会更好。我现在能够构建它。最好的祝愿。
    猜你喜欢
    • 1970-01-01
    • 2010-11-30
    • 1970-01-01
    • 1970-01-01
    • 2011-12-21
    • 2022-06-28
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    相关资源
    最近更新 更多