我有一个JSON输入:

{
  "levelOne": [
    {
      "leveltwo": {
        "levelThree": [
          {
            "type": "typeOne",
            "leveltwo": {
              "Id": "101000094794",
              "Id2": "101000013207"
            }
          },
          {
            "type": "typeTwo",
            "leveltwo": {
              "Id": "101000013207",
              "Id2": "101000013207"
            }
          }
        ]
      }
    }
  ]
}

有没有一种Jolt规范可以降低每个关键点,包括嵌套对象中的关键点?(在这种情况下,在级别2下是什么)

{
  "levelone": [
    {
      "leveltwo": {
        "levelThree": [
          {
            "id": "101000094794",
            "id2": "101000013207",
            "type": "typeOne"
          },
          {
            "id": "101000013207",
            "id2": "101000013207",
            "type": "typeTwo"
          }
        ]
      }
    }
  ]
}

目前,我使用的规范(jolt模板)如下:`

[
  {
    "operation": "shift",
    "spec": {
      "levelOne": {
        "*": {
          "leveltwo": {
            "id": "&3[&2].leveltwo.id",
            "levelThree": {
              "*": {
                "leveltwo": {
                  "Id": "&6[&5].leveltwo.&3[&2].id",
                  "Id2": "&6[&5].leveltwo.&3[&2].id2"
                },
                "type": "&5[&4].leveltwo.&2[&1].type"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "$": "&1.key",
        "@": "&1.value"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "levelOne": {
        "key": "=toLower"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "value": "@(1,key)"
      }
    }
  }
]

enter image description here

预期结果(密钥应在较低级别三,类型一,类型二):

{
  "levelone": [
    {
      "leveltwo": {
        "levelthree": [
          {
            "id": "101000094794",
            "id2": "101000013207",
            "type": "typeone"
          },
          {
            "id": "101000013207",
            "id2": "101000013207",
            "type": "typetwo"
          }
        ]
      }
    }
  ]
}

谢谢!

推荐答案

您可以对每个对象键重复应用连续的shift-modify-shift三重奏(在本例中为levelOnelevelThree),这需要进行大小写转换,例如

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "id": "&3[&2].&1.id",
            "*": {
              "*": {
                "*": {
                  "Id": "&6[&5].&1.&3[&2].id",
                  "Id2": "&6[&5].&1.&3[&2].id2"
                },
                "type": "&5[&4].&3.&2[&1].type"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "$": "&1.key",
        "@": "&1.value"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "key": "=toLower"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "value": "@(1,key)"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": {
              "$": "&4.&3.&2.&1.key",
              "@": "&4.&3.&2.&1.value"
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": {
              "key": "=toLower",
              "value": {
                "*": {
                  "type": "=toLower"
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": {
              "value": "&4[&3].&2.@(1,key)"
            }
          }
        }
      }
    }
  }
]

Json相关问答推荐

try 将文本标记放置在条形图上的同一高度

从json数组中删除特定元素

Jolt-Json转换:通过引用标识符(而不是索引)设置值

如何使用 JOLT 使用输入数组中的值和层次 struct 中的其他字段创建数组

Spark-SQL中的from_unixtime函数未能给出正确的输出

使用 jq 将非统一的 json 输出转换为汇总表

使用 jq 将消息转换为数组

阅读 JSON 正文 Firebase 云函数

Oracle Apex - 将 JSON 对象分配给变量以返回

使用 Ansible 解析来自 Juniper 交换机的 JSON 输出

如何用 Xidel 正确读取这个 JSON 文件?

如何在 Go 中生成带有排序键的 JSON?

将 YAML 文件转换为 Python JSON 对象

在 Qt 4.7 中解析 JSON 的最简单方法

使用杰克逊创建一个 json 对象

有什么方法可以判断您安装的 gulp 版本吗?

NSManagedObject 属性值的 NSNull 处理

确保数组中的项目属性在 Json Schema 中是唯一的?

使用 C# 调用 json

如何使用 Gson 将 JSONArray 转换为 List?