Jolt转换应该处理请求数据可能为空、空或不存在的情况.它应该根据Jolt规范中指定的条件将数据转换为所需的输出.这些条件基于source属性和hqAddress,isInternal属性.

你能在这里帮助了解问题,并分享周围的任何material 来了解更多关于颠簸的知识吗?

Input.json - example 1

{
  "id": "testId",
  "basicInfo": {
    "hqAddress": "N",
    "source": "NIT",
    "accountNumber": "123",
    "isInternal": true
  }
}

ActualJolt.json

[
  {
    "operation": "shift",
    "spec": {
      "id": "envId",
      "basicInfo": {
        "source": {
          "SIT": {
            "hqAddress": {
              "P": {
                "#P-SIT": "system.env"
              },
               //if not P anything else or if hqAddress empty or not present in the request should be N-SIT
              "*": {
                "#N-SIT": "system.env"
              }
            }
          },
          "UAT": {
            "hqAddress": {
              "P": {
                "#P-UAT": "system.env"
              },
                //if not P anything else or if hqAddress empty or not present in the request should be N-UAT
              "*": {
                "#N-UAT": "system.env"
              }
            }
          },
          "*": {
            "@isInternal": {
              "true": {
                "#true": ["isInternalAccount", "isDevAccount"]
              },
              //if not true anything else or if isInteranl empty or not present in the request should be false
              "*": {
                "#false": ["isInternalAccount", "isDevAccount"]
              }
            }
          },
          "hqAddress": {
            "P": {
              "#P": "system.env"
            },
            //if not P anything else or if hqAddress empty or not present in the request should be Other
            "*": {
              "#Other": "system.env"
            }
          }
        }
      }
    }
  }

]

ExpectedOutput.json

{
  "envId" : "testId",
  "isInternalAccount":false,
  "isDevAccount":false,
  "system":{
     "env":"Other"
   } 
}

推荐答案

您可以从将"isInternal"键转换为"@2,isInternal",将底部的"hqAddress"键转换为"@1,hqAddress",以便在树中分别正确地定位它们的级别.

100操作符可以在modify规范中使用,以判断existencenullabilitygithub document for v0.0.22状态.

最后,可能需要基于预期结果进行布尔转换,该结果显示布尔值,而转换可能会产生它们的字符串类型.

因此,您可以将您的代码转换为以下代码:

[
  {
    "operation": "shift",
    "spec": {
      "id": "envId",
      "basicInfo": {
        "source": {
          "SIT": {
            "hqAddress": {
              "P": {
                "#P-SIT": "system.env"
              },
              "*": {
                "#N-SIT": "system.env"
              }
            }
          },
          "UAT": {
            "hqAddress": {
              "P": {
                "#P-UAT": "system.env"
              },
              "*": {
                "#N-UAT": "system.env"
              }
            }
          },
          "*": {
            "@2,isInternal": {
              "true": {
                "#true": ["isInternalAccount", "isDevAccount"]
              },
              "*": {
                "#false": ["isInternalAccount", "isDevAccount"]
              }
            }
          },
          "@1,hqAddress": {
            "P": {
              "#P": "system.env"
            },
            "*": {
              "#Other": "system.env"
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "~isInternalAccount": false,
      "~isDevAccount": false,
      "~system": {
        "~env": "Other"
      }
    }
  },
  { // boolean results expected
    "operation": "modify-overwrite-beta",
    "spec": {
      "isInternalAccount|isDevAccount": "=toBoolean"
    }
  }
]

Json相关问答推荐

两种情况下过滤的JOLT规范

从json数组中删除特定元素

修改Deneb图表中工具提示的字体大小

使用Jolt v.0.1.1向每个json元素添加一个键-值对

Pandas 对REST API的自定义响应

来自json的可分析的构建报告

使用jq过滤复杂json对象中的数据

nlohmann json:为什么我会得到一个解析错误?

使用 jq 如何更改键的值?

在 python 中循环 JSON 数组

Android 如何判断小时时间是否在两个时间之间?

使用 jq 和 awk 拆分大型 JSON 文件

在 Flutter 中将对象转换为可编码对象失败

如何将西里尔字母转换为 utf16

避免 KeyError 的默认字典键

如何在不解析的情况下在javascript中同步包含JSON数据?

从 HttpResponse 获取 json

js 中奇怪的 JSON 解析行为,Unexpected token :

在 iPhone 上解析 JSON 日期

添加json文件注释