首页 > 解决方案 > 保留一些 json 键值对,同时使用 jolt 转换它们

问题描述

我刚开始学习颠簸。我想将 json 转换为所需的格式。我快完成了,但坚持这一点

我的输入 json 看起来像

{ "first_name": {
    "label": "First name",
    "type": "text",
    "value": "John"
  },
  "last_name": {
    "label": "Last name",
    "type": "text",
    "value": "Doe"
  },
  "email": {
    "label": "Email",
    "type": "text",
    "value": "johndoe@gmail.com"
  }
  "id": 123,
  "marital_status": "Single",
  "author_id": null,
  "company": null,
  "address": {
    "city": {
      "label": "city",
      "dom_type": "dropdown",
      "value": "test"
    },
    "state": {
      "label": "state",
      "dom_type": "dropdown",
      "value": "state"
    },
    "country": {
      "label": "country",
      "dom_type": "dropdown",
      "value": "country"
    }
  }
}

像这样的输出格式

{
 "first_name" : "John", "last_name" : "Doe", "email" : "johndoe@gmail.com", 
 "id": 123, "marital_status": "Single", "author_id": null, "company": null,
  "address" : { "city" : "test", "state" : "test", "country" : "test" }
}

我试过这个班次规范

[
  {
    "operation": "shift",
    "spec": {
      "address": {
        "*": {
          "@value": "address.&1"
        }
      },
      "*": {
        "@value": "&1"
      }
    }
  }
]

并得到

{
     "first_name" : "John", "last_name" : "Doe", "email" : "johndoe@gmail.com", "address" : { "city" : "test", "state" : "test", "country" : "test" }
 }

因为匹配器“*”丢弃了简单的键值对。我知道我错过了一些东西。有什么帮助吗?

标签: jsonjolt

解决方案


因为匹配器“*”丢弃了简单的键值对。-> 它不是丢弃它们,而是匹配它们,但没有找到“值”的子属性。

您的输入数据基本上是 3 种不同的格式

  1. 地址下面的东西
  2. 像“id”这样价值单一的东西
  3. 有嵌套数据的东西

“*”只匹配左侧的 / 键。

在这种情况下,您将需要显式列出单数键或具有嵌套数据的键。

规格

[
  {
    "operation": "shift",
    "spec": {
      "address": {
        "*": {
          "@value": "address.&1"
        }
      },
      "id|marital_status|author_id|company": "&",
      "*": {
        "@value": "&1"
      }
    }
  }
]

推荐阅读