我有json的源代码如下:

{
  "name": "Andrew",
  "address": {
    "permanentAddress": null,
    "temporaryAddress": null
  }
}

想要按照以下方式映射到目的地地址:

{
  "person": {
    "name": "Andrew",
    "address": {}
  }
}

如果permanantAddress和temporaryAddress为null,则将person. address设置为Empty而非null:

我试过下面 我试过用下面的震动变换

[
  {
    "operation": "shift",
    "spec": {
      "name": "person.name",
      "address": {
        "permanentAddress": "person.address.permanentAddress",
        "temporaryAddress": "person.address.temporaryAddress"
      }
    }
  }
]

答案是:

{
  "person" : {
    "name" : "Andrew",
    "address" : {
      "permanentAddress" : null,
      "temporaryAddress" : null
    }
  }
}

推荐答案

您可以使用以下转换:

[
  { // set the values of the attributes to "NulL" literal if they're null
    "operation": "modify-overwrite-beta",
    "spec": {
      "address": {
        "~permanentAddress|~temporaryAddress": "NulL"
      }
    }
  },
  { // exchange key-value pairs for "NulL" values  
    "operation": "shift",
    "spec": {
      "*": "&",
      "address": {
        "*": {
          "NulL": {
            "@": "&3.&1"
          },
          "*": {
            "$": "&3.&2"
          }
        }
      }
    }
  },
  { // get rid of the keys 与 "NulL" values
    "operation": "remove",
    "spec": {
      "address": {
        "NulL": ""
      }
    }
  }
]

当前 case 的demo(on https://jolt-demo.appspot.com/)是:

enter image description here

Note that:如果你想排除所有属性,包括可能除了当前两个属性之外的属性,那么只需替换一对,

"~permanentAddress|~temporaryAddress": "NulL"

"~*": "NulL"

Json相关问答推荐

无法根据vega规范中的条件设置文本 colored颜色

SWIFT中的网络经理

当console.log返回TRUE值时,解析的JSON中的字段未定义

盒子图显示不正确

将=分隔值文件转换为:json文件

使用 serde 和 csv crates 将嵌套的 json 对象序列化为 csv

如何从条带订阅响应对象中正确获取 struct 项?

Kotlin Android Room 处理 Moshi TypeConverter 中的空对象列表

Rails 控制器无需编码即可渲染 json

N1QL 聚合查询 Couchbase

如何解决名为 null 的map值

一起使用 Argparse 和 Json

如何使用 json.net 将数据表转换为 json 字符串?

ASP.NET MVC:使用 JsonResult 控制属性名称的序列化

jQuery AJAX 和 JSON 格式

JSON 到 JSON 转换器

是否可以将数据写入本地 json 文件,除了Angular 之外什么都没有?

JSON 格式的 Amazon S3 响应?

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

如何从 github API 解析链接头