Complex json data transformation

Hi, if I have a response like:

[
    {
        "itemId": 7,
        "offers": [
            {
                "price": {
                    "currency": "USD",
                    "base": 350,
                    "sale": 299.99,
                    "cost": 200
                },
            }
        ],
    },
    {
        "itemId": 7,
        "offers": [
            {
                "price": {
                    "currency": "USD",
                    "base": 350,
                    "sale": 299.99,
                    "cost": 200
                },
            }
        ],
    }
]

how can I transform it into:

[
{
  itemId:
  sale:
  base:
  currency:
  cost:
}
,
{
  itemId:
  sale:
  base:
  currency:
  cost:
}
]

I was able to extract offers, but I can’t extract the data inside offer

what i have is:

[
  {{range $index, $price := .}}
    {
      itemId: {{$price.itemId}}
      sale: {{ index $price.offers 0}}
    },
  {{end}}
]

Hello @Ethan_Ye

You can do so with the following transformation:

[
{{range $index, $item := .}}
	{
		"itemId": {{ $item.itemId }},
		"sale": {{ (index $item.offers 0).price.sale }},
		"base": {{ (index $item.offers 0).price.base }},
		"currency": "{{ (index $item.offers 0).price.currency }}",
		"cost": {{ (index $item.offers 0).price.cost }}
	}{{ if lt $index (sub (len .) 1) }},{{ end }}
{{ end }}
]

Tested against:

[
	{
		"itemId": 7,
		"offers": [
			{
				"price": {
					"currency": "USD",
					"base": 350,
					"sale": 299.99,
					"cost": 200
				}
			}
		]
	},
	{
		"itemId": 7,
		"offers": [
			{
				"price": {
					"currency": "USD",
					"base": 350,
					"sale": 299.99,
					"cost": 200
				}
			}
		]
	}
]

Result:

[

	{
		"itemId": 7,
		"sale": 299.99,
		"base": 350,
		"currency": "USD",
		"cost": 200
	},

	{
		"itemId": 7,
		"sale": 299.99,
		"base": 350,
		"currency": "USD",
		"cost": 200
	}

]

Thanks zaid! I’m able to transform my response through the above code, however, my response is truncated, do you know why? is it because I’m using a trial account? Attaching some sceenshots in case it helps

Hello @Ethan_Ye

Reason thats happening is because something is going wrong with the transformation. I believe its because of this line:

"priceListId": {{ $item.priceListId }},

More specifically this $item.priceListId. Unless you changed the input structure the right key name is itemId . And the right line would be.

"priceListId": {{ $item.itemId }},.

Let me know if that works.

Zaid

Hi @zaid,

Yes, I did change my inout structure, input has ‘priceListId’ attribute. Also, if you check the above test output, there was no issue. I’ve also tried itemId, it’s the same behavior, test output works but the actual response is still truncated.

The entire response from postman
[
    {
        "isSoftDeleted": false,
        "priceListId": 100000,
        "itemId": 7,
        "offers": [
            {
                "kind": "Clearance",
                "channel": 12,
                "price": {
                    "currency": "USD",
                    "base": 350,
                    "sale": 299.99,
                    "cost": 200
                },
                "offerCode": 1017,
                "range": []
            }
        ],
        "discountAmountDetails": []
    }
]

Also, what programing language does transformation use? I couldn’t find any detailed documentation on Response Body

Ah I see. I believe I know what it is. I have run into this before. I believe that sometimes the shape of the request can be weird. Not entirely sure why. You will a way to inspect the shape of the request. Is this a public API by any chance where I can test this? If not I can look into how you might be able to figure out what the structure might be on Monday. In terms of the the language, this uses GoLang templates.

@zaid
The endpoint is publicly facing, but it requires credentials to access. Unfortunately, i can’t share the creds with you.

In case it helps, this is my header(I’ve modified the account as it contains credentials):

and here is the request body:

here is the log for request and response:

Just to be clear, I did receive the actual response if no transformation is added.

Hi,

Any update on this?

Thanks

Can you share a sample of the response JSON or is the same as the first?

@Olu here is the exact response for the same request

[
    {
        "_id": "randomId",
        "isSoftDeleted": false,
        "priceListId": 100000,
        "itemId": 7,
        "itemSku": null,
        "offers": [
            {
                "_id": "randomId",
                "kind": "Clearance",
                "channel": 12,
                "startDate": "2021-08-12T12:00:00.000Z",
                "endDate": "2099-12-31T18:29:00.000Z",
                "price": {
                    "currency": "USD",
                    "base": 350,
                    "sale": 299.99,
                    "cost": 200
                },
                "offerCode": 1017,
                "range": []
            }
        ],
        "discountAmountDetails": []
    }
]

Hello @Ethan_Ye I have been sick the past couple of days and haven’t had a chance to take a look. I will send over some example code to run so that we can investigate the structure of the object Tyk is receiving. Is API you’re using by any chance a shopify API?

Zaid

@zaid here is the API contract https://knowledgebase.fabric.inc/docs/openapi/offers/reference/#operation/getPriceByItemIds, its not shopify

1 Like

Hi @zaid - can we pls get some help here? We need to complete this analysis asap and any help will be appreciated

Hi @amit803

Can you please change the template to {{ . }} and share the result returned from Tyk after the transform.

Here is the response:

map[array:[map[_id:randomId discountAmountDetails:[] isSoftDeleted:false itemId:7 itemSku:<nil> offers:[map[_id:randomId channel:12 endDate:2099-12-31T18:29:00.000Z kind:Clearance offerCode:1017 price:map[base:350 cost:200 currency:USD sale:299.99] range:[] startDate:2021-08-12T12:00:00.000Z]] priceListId:100000]]] 

Hi @amit803

Notice the array key? That is what is causing the issue.

map[
    array: [
        map[
            _id: randomId
            discountAmountDetails: []
            isSoftDeleted: false
            itemId: 7
            itemSku: <nil> 
            offers: [
                map[
                    _id:randomId
                    channel:12
                    endDate:2099-12-31T18:29:00.000Z
                    kind:Clearance 
                    offerCode:1017 
                    price: map[
                        base:350
                        cost:200
                        currency:USD
                        sale:299.99
                    ]
                    range:[] 
                    startDate:2021-08-12T12:00:00.000Z
                ]
            ]
            priceListId:100000
        ]
    ]
]

The above translates to

{
    "array": [
        {
            "_id": "randomId",
            "discountAmountDetails": [],
            "isSoftDeleted": false,
            "itemId": 7,
            "itemSku": null,
            "offers": [
                {
                    "_id": "randomId",
                    "channel": 12,
                    "endDate": "2099-12-31T18:29:00.000Z",
                    "kind": "Clearance",
                    "offerCode": 1017 ,
                    "price": {
                        "base":350,
                        "cost":200,
                        "currency":"USD",
                        "sale":299.99
                    },
                    "range":[],
                    "startDate": "2021-08-12T12:00:00.000Z"
                }
            ],
            "priceListId":"100000"
        }
    ]
}

would require the following transformation:

[
{{$length := len .}}
{{range $index, $item := .array}}
         {
		"itemId": {{ $item.itemId }},
		"sale": {{ (index $item.offers 0).price.sale }},
		"base": {{ (index $item.offers 0).price.base }},
		"currency": "{{ (index $item.offers 0).price.currency }}",
		"cost": {{ (index $item.offers 0).price.cost }}
	}{{ if lt $index (sub $length 1) }},{{ end }}
{{ end }}
]

@zaid Thanks a lot for the help, the new transformation works now. I will follow up if I encounter any new issues.

No problem. I am glad I was able to help.