Skip to main content

Hi everyone,

 

In Anypoint Studio, i would like to put a value as a key JSON. by default, i couldn't map the key.

input:

{

"total_rows": 7,

"offset": 0,

"rows": [{

"id": "0dd206faa4e454bbbf8133ab8c0a87bc",

"key": "0dd206faa4e454bbbf8133ab8c0a87bc",

"doc": {

"_id": "0dd206faa4e454bbbf8133ab8c0a87bc",

"_rev": "1-c140584e7b8a7841b6a1b85baa77c82d",

"recordType": "product",

"productID": "CK234324",

"productName": "Branded Air Fly High Quality Sneakers",

"category": "Enterprise",

"SKU": "CKS100",

"price": 100,

"inventory": 35868

}

}]

}

 

expected Output:

[{

"CK234324": {

"_id": "0dd206faa4e454bbbf8133ab8c0a87bc",

"_rev": "1-c140584e7b8a7841b6a1b85baa77c82d",

"recordType": "product",

"productID": "CK234324",

"productName": "Branded Air Fly High Quality Sneakers",

"category": "Enterprise",

"SKU": "CKS100",

"price": 100,

"inventory": 35868

}

}]

Here an example if it's not clear.

 

Thanks All.

Djoly

8 respuestas
  1. 26 abr 2020, 17:12

    Then you can use the script as under:

    %dw 2.0

    output application/json

    import * from dw::core::Objects

    ---

    [(payload.rows map {

    ($..productID[0]) : $.doc

    }) reduce (it, ac = {}) -> (ac mergeWith it)]

0/9000