Reputation: 29
I am trying to manipulate my payload for a bulk API message body where I am updating multiple images at the same time. For example here I have two images:
[
{
"entry": {
"media_type": "image",
"position": [
"1",
"2"
],
"disabled": false,
"types": {
"image",
"small_image",
"thumbnail"
},
"content": {
"base64_encoded_data": [
"/9j/4ReLRXhpZgAATU0AKgAAAAgA....",
"/9j/4ReLRXhpZgAATU0AKgAAAAgA...."],
"type": "image/jpeg",
"name": [
"228186_1.jpg",
"228187_2.jpg"
]
},
"sku": [
"228186",
"228187"
]
}
}
]
but I need these to appear as two separate entries below each other... how can split this into into individual entries so that for position and sku etc it only shows one entry? eventually i will want to use this to list data for several images below each other to update the via bulk API.
This is my desired output
[
{
"entry": {
"media_type": "image",
"disabled": false,
"position": 1,
"types": [
"image",
"small_image",
"thumbnail"
],
"content": {
"base64EncodedData": ["/9j/4ReLRXhpZgAATU0AKgAAAAgA....],
"type": "image/png",
"name": "228186_1.jpg"
}
},
"sku": "228186"
},
{
"entry": {
"media_type": "image",
"disabled": false,
"position": 2,
"types": [
"image",
"small_image",
"thumbnail"
],
"content": {
"base64EncodedData": ["/9j/4ReLRXhpZgAATU0AKgAAAAgA....],
"type": "image/jpeg,
"name": "228187_2.jpg"
}
},
"sku": "228187"
}
]
Thanks!
Upvotes: 0
Views: 291
Reputation: 196
Making the same assumption as Michael Jones for payload a more precise one by preserving the order
If your input is an array of object, please use this:
%dw 2.0
import * from dw::util::Values
output application/json
---
flatten(payload map(v1, k1) ->
(v1.entry.position) map(v2, k2) ->
entry:{
"media_type": v1.entry.media_type,
"position": v1.entry.position[k2],
"disabled":v1.entry.disabled,
"types": v1.entry.types,
"content": {
"base64_encoded_data": v1.entry.content.base64_encoded_data,
"type": v1.entry.content."type",
"name": v1.entry.content.name[k2]
},
"sku": v1.entry.sku[k2]
})
Please note that in your input for "types" you have given as
"types": {
"image",
"small_image",
"thumbnail"
}
An object of strings, which is not correct. An object will always have key and value, So I am assuming it to be like this:
"types": [
"image",
"small_image",
"thumbnail"
]
If your input is an object of objects, you can use this:
%dw 2.0
output application/json
var indices = payload.entry.position map ($ as Number) - 1
---
indices map entry:{
(payload.entry - "content" - "sku" - "position" - "disabled" - "types"),
position: payload.entry.position[$],
(payload.entry - "content" - "sku" - "position" - "media_type" - "types"),
(payload.entry - "content" - "sku" - "position" - "disabled" - "media_type"),
content: {
base64_encoded_data: payload.entry.content.base64_encoded_data[$],
"type": payload.entry.content."type",
name: payload.entry.content.name[$]
},
sku: payload.entry.sku[$]
}
Upvotes: -1
Reputation: 1910
Assuming you meant your payload to look like this, and I'm taking some guesses here...
{
"entry":{
"media_type":"image",
"position":[
"1",
"2"
],
"disabled":false,
"types":[
"image",
"small_image",
"thumbnail"
],
"content":{
"base64_encoded_data":[
"/9j/4ReLRXhpZgAATU0AKgAAAAgA....",
"/9j/8ReLRXhpZgAATU0AKgAAAAgA...."
],
"type":"image/jpeg",
"name":[
"228186_1.jpg",
"228187_2.jpg"
]
},
"sku":[
"228186",
"228187"
]
}
}
And then also guessing on the shape of your desired output...
%dw 2.0
output application/json
var indices = payload.entry.position map ($ as Number) - 1
---
indices map {
(payload.entry - "content" - "sku" - "position"),
position: payload.entry.position[$],
content: {
base64_encoded_data: payload.entry.content.base64_encoded_data[$],
"type": payload.entry.content."type",
name: payload.entry.content.name[$]
},
sku: payload.entry.sku[$]
}
What I'm ensure about is if we can rely on the position object to be their positions in the payload, or to mean something else. You could also change it to something like var indices = 0 to sizeOf(payload.entry.position) - 1
Creates:
[
{
"media_type": "image",
"position": "1",
"disabled": false,
"types": [
"image",
"small_image",
"thumbnail"
],
"content": {
"base64_encoded_data": "/9j/4ReLRXhpZgAATU0AKgAAAAgA....",
"type": "image/jpeg",
"name": "228186_1.jpg"
},
"sku": "228186"
},
{
"media_type": "image",
"position": "2",
"disabled": false,
"types": [
"image",
"small_image",
"thumbnail"
],
"content": {
"base64_encoded_data": "/9j/8ReLRXhpZgAATU0AKgAAAAgA....",
"type": "image/jpeg",
"name": "228187_2.jpg"
},
"sku": "228187"
}
]
Upvotes: 2