我有一个JSON源代码如下:

{
  "name": "test",
  "age" 10,
  "city": null,
  "pinCode": "400081",
  "address": {
    "buildingNo": "B-301"
  }
} 

如果city100,则更新addressnull,例如:

"person" : {
    "name" : "test",
    "age" : 10,
    "address" : null
}

否则,如果city100,则保持如下结果:

{
  "person" : {
    "name" : "test",
    "age" : 10,
    "address" : {
      "pinCode" : "400081",
      "buildingNo" : "B-301"
    }
  }
}

我试过像下面这样的jolt transform:

[
  {
    "operation": "shift",
    "spec": {
      "name": "person.name",
      "age": "person.age",
      "city": {
        "null": {
          "@(1,person.address)": null
        },
        "*": {
          "@1": "person.address.city"
        }
      },
      "pinCode": "person.address.pinCode",
      "address": {
        "buildingNo": "person.address.buildingNo"
      }
    }
  }
]

推荐答案

null是预定义的值,但"null""NulL"等是一些不相同的任意文字.您可以将city设置为一个文字,如modify规范中的"NulL".以及一个104操作符,以便能够在shift规范中的条件中使用.

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "~city": "NulL"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "name|age": "person.&",
      "city": {
        "NulL": {
          "@": "person.address"
        },
        "*": {
          "@1": "person.address.&2", // bring value(@1) amd key(&2) of "city" attribute 
          "@2,pinCode": "person.address.pinCode",
          "@2,address": {
            "*": "person.address.&" // all elements in "address" object
          }
        }
      }
    }
  }
]

Json相关问答推荐

在Jenkins中使用ReadJSON读取json子元素

使用JQ从jsonl文件中删除具有匹配键/值的行

使用 JSON 和相对日期设置日历视图中 SharePoint 列表项的背景 colored颜色 格式

jq如何合并两个json对象

(已回答)JSON 读取函数返回未定义而不是预期值 - Typescript

爆炸没有数组的 struct pyspark

如何 Select 一个值,这是可选的 - 使用 jq

Shell脚本空格转义

将 colly 包输出文本添加到 golang 中的映射

如何使用 ConfigurationBuilder 解析现有的 json 字符串(不是文件)

错误字符串的长度超过了maxJsonLength属性设置的值

从 HttpResponse 获取 json

如何将 LinkedTreeMap 转换为 gson JsonObject

使用 jq,将对象数组转换为具有命名键的对象

Jackson 动态属性名称

使用 ajax 将 JSON 发送到 PHP

如何在 Python 中合并两个 json 字符串?

JSON 模式 - 如果对象*不*包含特定属性则有效

将多个值存储在json中的单个键中

仅使用字符串和值解析 JSON 对象