简体   繁体   English

使用 JQ 有条件地在 JSON 转换中添加一个字段

[英]Conditionally add a field in JSON transformation using JQ

I am trying to transform my JSON to different structure using JQ.我正在尝试使用 JQ 将我的 JSON 转换为不同的结构。 I am able to achieve my new structure, however i am getting feilds with Null objects if they are not present in Source, my client wants to remove the fields if they are having null values..我能够实现我的新结构,但是如果源中不存在 Null 对象,我会得到它们,如果它们具有空值,我的客户想要删除这些字段..

As i iterate i am able to get the structure in new format.当我迭代时,我能够以新格式获取结构。 But additional structures are coming.但额外的结构即将到来。

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

JSON 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 Query: JQ 查询:

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

Expected Response:预期回应:

        {
              "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"
                }
              ]
            }

With the query i have, i am getting state : null for the entries which has empty or null values.通过我的查询,我得到state : null对于具有空值或空值的条目。 I want to hide the field itself in these cases.在这些情况下,我想隐藏字段本身。

Try this JQ Query:试试这个 JQ 查询:

jq '.amazon| { userType: .userType, userName: .user, itemCatalog: [.items[] | {id: .id, name: .name, state: .state}]}'  

Warning this doesn't account for values of null.警告这不考虑空值。 So this is half the solution.所以这是解决方案的一半。

Demo演示

Horrible solution, delete them after the query.可怕的解决方案,在查询后删除它们。 There must be a neater way?必须有更整洁的方法吗? (I started using jq today) (我今天开始用jq)

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

https://jqplay.org/s/jlNYmJNi25 https://jqplay.org/s/jlNYmJNi25

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM