简体   繁体   English

使用JQ进行JSON转换

[英]JSON Transformation using JQ

I am trying to transform my JSON to different structure using JQ. 我正在尝试使用JQ将JSON转换为不同的结构。 I am able to partially achieve my new structure, however i get a additional blocks of data. 我可以部分实现我的新结构,但是我得到了额外的数据块。

As i iterate i am able to get the structure in new format. 当我迭代时,我能够获得新格式的结构。 But additional structures are coming. 但是更多的结构正在出现。

Code Snippet- https://jqplay.org/s/3gulSlZiWz 代码段-https : //jqplay.org/s/3gulSlZiWz

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": "in inventory",
            "type": {
              "branded": false,
              "description": "Spprts",
              "contentLevel": "season"
            }
          },
          {
            "id": 3,
            "name": "watch",
            "state": "returned",
            "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: {   
             itemId: .items[].id, 
             name: .items[].name  
          }
      }

Expected Response: 预期回应:

 {
    "userType": "new",
    "userName": "net",
    "itemCatalog": {
      "itemId": 1,
      "name": "harry potter"
    },{
      "itemId": 2,
      "name": "adidas shoes"
    }, {
      "itemId": 3,
      "name": "watch"
    },{
      "itemId": 4,
      "name": "adidas shoes"
    }
  }

But getting something weird long duplicated response. 但是得到一些奇怪的长期重复的响应。

As already pointed out in a comment, the "expected response" is not JSON and probably not what you want anyway. 正如评论中已经指出的那样,“期望的响应”不是JSON,可能也不是您想要的。 The following would make sense and in any case illustrates how to iterate appropriately: 以下内容将很有意义,并且无论如何都说明了如何适当地进行迭代:

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

Output 产量

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

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

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