2

I am trying to transform my JSON to different structure using JQ. 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..

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

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:

       .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. I want to hide the field itself in these cases.

Aksanth
  • 269
  • 2
  • 13

1 Answers1

-1

Horrible solution, delete them after the query. There must be a neater way? (I started using jq today)

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

https://jqplay.org/s/jlNYmJNi25

Michal
  • 2,532
  • 1
  • 19
  • 27