Json Array value to column formating

For the json below I need to be able to make the TabStatus.TabLabel value a column and TabStatus.TabValue as the value of that column and discard the rest of the data in the Array. Note that the TabStatus array can have up to 166 items where below I only show 2.

Input:
[{
“EnvelopeID” : “f76fa554-5bf3-44df-a992-1c04e6be5280”,
“RecipientId” : “22cca495-954e-4930-8070-3be40ada6105”,
“TabStatus” : [{
“TabType” : “Approve”,
“Status” : “Active”,
“XPosition” : “1025”,
“YPosition” : “1500”,
“TabLabel” : “SCT Approve”,
“TabName” : “SCT Approve”,
“TabValue” : “A”,
“DocumentID” : “1”,
“PageNumber” : “2”,
“OriginalValue” : null
}, {
“TabType” : “Decline”,
“Status” : “Active”,
“XPosition” : “1026”,
“YPosition” : “1565”,
“TabLabel” : “SCT Decline”,
“TabName” : “SCT Decline”,
“TabValue” : “N”,
“DocumentID” : “1”,
“PageNumber” : “2”,
“OriginalValue” : null
}
]
}, {
“EnvelopeID” : “18ea8193-085d-482a-bdac-a8024821c27c”,
“RecipientId” : “0f0962e2-32e4-4d7e-a520-71aa2366c3ed”,
“TabStatus” : [{
“TabType” : “Approve”,
“Status” : “Active”,
“XPosition” : “1025”,
“YPosition” : “1500”,
“TabLabel” : “SCT Approve”,
“TabName” : “SCT Approve”,
“TabValue” : “N”,
“DocumentID” : “1”,
“PageNumber” : “2”,
“OriginalValue” : null
}, {
“TabType” : “Decline”,
“Status” : “Active”,
“XPosition” : “1026”,
“YPosition” : “1565”,
“TabLabel” : “SCT Decline”,
“TabName” : “SCT Decline”,
“TabValue” : “Y”,
“DocumentID” : “1”,
“PageNumber” : “2”,
“OriginalValue” : null
}
]
}
]

output:

[ {
“EnvelopeID” : “f76fa554-5bf3-44df-a992-1c04e6be5280”,
“RecipientId” : “22cca495-954e-4930-8070-3be40ada6105”,
“SCT Approve” : “A”,
“SCT Decline” : “N”
}, {
“EnvelopeID” : “18ea8193-085d-482a-bdac-a8024821c27c”,
“RecipientId” : “0f0962e2-32e4-4d7e-a520-71aa2366c3ed”,
“SCT Approve” : “N”,
“SCT Decline” : “Y”
}
]

You’re looking for the extend() method on objects. It will take an array of key/value pairs and return a new object consisting of the original object plus the properties from the array. But, first, we need to change the TabStatus array into pairs using the map() method, like so:

$TabStatus.map(x => [x.TabLabel, x.TabValue])

Putting it all together, you can use an object literal to create an object with the values you want to pass through and then call extend with the massaged TabStatus array:

{
    EnvelopeID: $EnvelopeID,
    RecipientId: $RecipientId
}.extend($.TabStatus.map(x => [x.TabLabel, x.TabValue]))[/code]

There are other ways to do this as well, you could call extend on the original document (e.g. $.extend(...)). But, that would include the original TabStatus array in the output, so you’d have to use a separate mapper to delete that.

(Thanks for providing example output, makes things much easier.)

1 Like

This worked great! Thanks!