【问题标题】:Conditionally add a field in JSON transformation using JQ使用 JQ 有条件地在 JSON 转换中添加字段
【发布时间】:2019-02-21 19:40:54
【问题描述】:

我正在尝试使用 JQ 将我的 JSON 转换为不同的结构。我能够实现我的新结构,但是如果源中不存在 Null 对象,我将获得带有 Null 对象的字段,如果它们具有空值,我的客户想要删除这些字段..

当我迭代时,我能够获得新格式的结构。但其他结构即将推出。

代码片段-https://jqplay.org/s/w2N_Ozg9Ag

JSON

    {
              "amazon": {
                "activeitem": 2,
                "createdDate": "2019-01-15T17:36:31.588Z",
                "lastModifiedDate": "2019-01-15T17:36:31.588Z",
                "user": "net",
                "userType": "new",
                "items": [
                  {
                    "id": 1,
                    "name": "harry potter",
                    "state": "sold",
                    "type": {
                      "branded": false,
                      "description": "artwork",
                      "contentLevel": "season"
                    }
                  },
                  {
                    "id": 2,
                    "name": "adidas shoes",
                    "state": null ,
                    "type": {
                      "branded": false,
                      "description": "Spprts",
                      "contentLevel": "season"
                    }
                  },
                  {
                    "id": 3,
                    "name": "watch",
                    "type": {
                      "branded": false,
                      "description": "walking",
                      "contentLevel": "special"
                    }
                  },
                  {
                    "id": 4,
                    "name": "adidas shoes",
                    "state": "in inventory",
                    "type": {
                      "branded": false,
                      "description": "running",
                      "contentLevel": "winter"
                    }
                  }
                ],
                "product": {
                  "id": 4,
                  "name": "adidas shoes",
                  "source": "dealer",
                  "destination": "resident"
                }
              }
            }

JQ 查询:

       .amazon | { userType: .userType,     userName: .user,     itemCatalog: (.items | map({ itemId: .id, name, state} ))   }

预期响应:

        {
              "userType": "new",
              "userName": "net",
              "itemCatalog": [
                {
                  "itemId": 1,
                  "name": "harry potter",
                  "state": "sold"
                },
                {
                  "itemId": 2,
                  "name": "adidas shoes"
                },
                {
                  "itemId": 3,
                  "name": "watch"
                },
                {
                  "itemId": 4,
                  "name": "adidas shoes",
                  "state": "in inventory"
                }
              ]
            }

通过我的查询,我得到 state : null 用于具有空值或 null 值的条目。在这些情况下,我想隐藏字段本身。

【问题讨论】:

标签: json jq


【解决方案1】:

可怕的解决方案,查询后删除。一定有更整洁的方法吗? (我今天开始用jq了)

.amazon | { userType: .userType, userName: .user, itemCatalog: (.items | map({ itemId: .id, name, state} )) }| del(.itemCatalog[].state |select(. == null))

https://jqplay.org/s/jlNYmJNi25

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多