我已经成功地在jolt的帮助下压平了一个son. 扁平化的SON现在应该充当输入SON 对于以下情况:

每次情况出现时

"values-*-identifier" = "colour"  AND  "values-*-value" = "green" 

已满,则值"green"应替换为"gre".

这意味着规范必须执行带有"AND"的if/else,并必须比较密钥内的索引(该索引应该具有相同的数字),以确保该值属于标识符.

输入sson:

{
  "values_0_identifier": "country",
  "values_0_value": "red",
  "values_0_value_unit_key": "",
  "values_0_language": "",
  "values_1_identifier": "metal",
  "values_1_value": "red",
  "values_1_value_unit_key": "",
  "values_1_language": "",
  "values_2_identifier": "colour",
  "values_2_value": "green",
  "values_2_value_unit_key": "",
  "values_2_language": ""
}

期望输出

{
  "values_0_identifier": "country",
  "values_0_value": "red",
  "values_0_value_unit_key": "",
  "values_0_language": "",
  "values_1_identifier": "metal",
  "values_1_value": "red",
  "values_1_value_unit_key": "",
  "values_1_language": "",
  "values_2_identifier": "colour",
  "values_2_value": "gre",
  "values_2_value_unit_key": "",
  "values_2_language": ""
}

到目前为止我try 了这个规格

[
  {
    "operation": "shift",
    "spec": {
      "values_2_value": {
        "green": {
          "values_2_identifier": {
            "colour": {
              "#gre": "value_2_identifier"
            }
          }
        }
      },
      "*": "&"
    }
  }
]

1st issue: my if_else condition has no result. It seems, that it is swallowing the mapped value. 2nd issue: Even when this spec should work. Of course I don't want to specify the value "-2-" within the key. Here, some kind of wildcard like -- should work. Whereby -- should naturally always have the same index.
Each kind of help will be highly appreciated. Thanks in advance for any kind of hint.

推荐答案

您可以通过使用按索引0、1、2开始分组

  • &(2,1):爬上树2级并 Select 第1个星号替换
  • &(2,2):爬到树上2层并 Select 第2个星号替代品

表示,例如

[
  { //group by indexes 0,1,2
    "operation": "shift",
    "spec": {
      "values_*_*": {
        "*": {
          "@1": "&(2,1).&(2,2)"
        }
      }
    }
  },
  { //
    "operation": "shift",
    "spec": {
      "*": {
        "*": "values_&1_&", //other attributes
        "value": { "": "" }, //get rid of extra "value" attributes
        "identifier": {
          "@": "values_&2_&", // to replicate "identifier" itself
          "*": { // if identifier IS NOT colour 
            "@2,value": "values_&3_value"
          },
          "colour": {
            "@2,value": {
              "green": {
                "#gre": "values_&5_value"
              },
              "*": { // value IS NOT green
                "@1": "values_&5_value"
              }
            }
          }
        }
      }
    }
  }
]

站点https://jolt-demo.appspot.com/上的demo是:

enter image description here

Json相关问答推荐

使用Circe将嵌套字段添加到杨森

PowerShell脚本-替换json数组(转义$var将被视为不带双引号的文本)

错误:在 NX 工作区中找不到模块../swagger.json

在 postgres 14 中将记录转换为所需的 json 格式

未知的META规范,无法验证.[规范v1.0.1]

使用jq根据对象中键的值查找对象

嵌套存储在字符串变量中的 JSON 对象

将 json 文件转换为 json 对象会打乱对象的顺序

验证和格式化 JSON 文件

读取 HttpwebResponse json 响应,C#

将文件发送到 Rails JSON API

如何使用 SwiftyJSON 将字符串转换为 JSON

ASP.NET MVC 读取原始 JSON 发布数据

通过 JSON 发送 HTML 代码

如何在 Java 中将 YAML 转换为 JSON?

如何在dart Flutter 中将json字符串转换为json对象?

C++:使用 nlohmann json 从文件中读取 json 对象

case 类只有一个字段时如何将json转为 case 类

NSURLRequest 中不支持的 URL

使用 JSON.NET 序列化/反序列化对象字典