Home > database >  Jolt transform nested json array
Jolt transform nested json array

Time:12-01

I've json which includes multiple product and each products have multiple variant details. Using jolt I need just few fields from input json, following almost same structure as input json. I successfully iterate over products but when i try to iterate over each product variant, I'm not getting the desired output.

Input.json

{
  "products": [
    {
      "id": 6635020550275,
      "title": "UAT-9122021",
      "handle": "uat-9122021",
      "body_html": "UAT-9122021",
      "published_at": "2021-09-13T20:56:30 10:00",
      "created_at": "2021-09-13T20:56:28 10:00",
      "updated_at": "2021-09-13T21:05:17 10:00",
      "vendor": "Britax Test",
      "product_type": "",
      "tags": [],
      "variants": [
        {
          "id": 39516385214595,
          "title": "Default Title",
          "option1": "Default Title",
          "option2": null,
          "option3": null,
          "sku": "UAT-9122021",
          "requires_shipping": true,
          "taxable": true,
          "featured_image": null,
          "available": true,
          "price": "19.30",
          "grams": 0,
          "compare_at_price": null,
          "position": 1,
          "product_id": 6635020550275,
          "created_at": "2021-09-13T20:56:29 10:00",
          "updated_at": "2021-09-13T21:04:45 10:00"
        }
      ],
      "images": [],
      "options": [
        {
          "name": "Title",
          "position": 1,
          "values": [
            "Default Title"
          ]
        }
      ]
    },
    {
      "id": 6632446787715,
      "title": "UAT-992021",
      "handle": "uat-992021",
      "body_html": "UAT-992021",
      "published_at": "2021-09-09T22:53:05 10:00",
      "created_at": "2021-09-09T22:53:04 10:00",
      "updated_at": "2021-09-09T23:05:26 10:00",
      "vendor": "Britax Test",
      "product_type": "",
      "tags": [],
      "variants": [
        {
          "id": 39511546462339,
          "title": "Default Title",
          "option1": "Default Title",
          "option2": null,
          "option3": null,
          "sku": "UAT-992021",
          "requires_shipping": true,
          "taxable": true,
          "featured_image": null,
          "available": true,
          "price": "35.50",
          "grams": 0,
          "compare_at_price": null,
          "position": 1,
          "product_id": 6632446787715,
          "created_at": "2021-09-09T22:53:04 10:00",
          "updated_at": "2021-09-09T23:04:40 10:00"
        }
      ],
      "images": [],
      "options": [
        {
          "name": "Title",
          "position": 1,
          "values": [
            "Default Title"
          ]
        }
      ]
    }
  ]
}

Here is Spec.json

[
  {
    "operation": "shift",
    "spec": {
      "products": {
        "*": {
          "created_at": "productDoc[&1].createdDateTime",
          "id": "productDoc[&1].id",
          "variants": {
            "*": {
              "id": "productDoc[&1].variants.[&1].ecommVariantId",
              "created_at": "productDoc[&1].variants.[&1].createdDateTime"
            }
          }
        }
      }
    }
  }
]

Expected output I want.

{
    "productDoc": [{
        "createdDateTime": "2021-09-13T20:56:28 10:00",
        "id": 6635020550275,
        "variants": [{
            "ecommVariantId": [39516385214595],
            "createdDateTime": ["2021-09-13T20:56:29 10:00"]
        }]
    }, 
    {
        "createdDateTime": "2021-09-09T22:53:04 10:00",
        "id": 6632446787715,
        "variants": [{
            "ecommVariantId": [39516385214595],
            "createdDateTime": ["2021-09-09T22:53:04 10:00"]
        }]
    }]
}

Actual output i'm getting right now.

{
  "productDoc" : [ {
    "createdDateTime" : "2021-09-13T20:56:28 10:00",
    "id" : 6635020550275,
    "variants" : [ {
      "ecommVariantId" : [ 39516385214595, 39511546462339 ],
      "createdDateTime" : [ "2021-09-13T20:56:29 10:00", "2021-09-09T22:53:04 10:00" ]
    } ]
  }, {
    "createdDateTime" : "2021-09-09T22:53:04 10:00",
    "id" : 6632446787715
  } ]
}

CodePudding user response:

Don't use [&1] with nested productDoc. Use [&3] because actual product is up to level 3.

[
  {
    "operation": "shift",
    "spec": {
      "products": {
        "*": {
          "created_at": "productDoc[&1].createdDateTime",
          "id": "productDoc[&1].id",
          "variants": {
            "*": {
              "id": "productDoc[&3].variants.[&1].ecommVariantId",
              "created_at": "productDoc[&3].variants.[&1].createdDateTime"
            }
          }
        }
      }
    }
  }
]

Result

  • Related