โ03-09-2020 12:39 AM
Hi,
I need to know the ways of performing the restructuring of the JSON payload based on the requirement.for ex. quiz_id value we need to distribute to each type of question.
below are input and expected output need to achieve through snaplogic pipeline
input json
{
โquizโ: {
โquiz_idโ : 123456789,
โsportโ: {
โq1โ: {
โquestionโ: โWhich one is correct team name in NBA?โ,
โoptionsโ: [
โNew York Bullsโ,
โLos Angeles Kingsโ,
โGolden State Warrirosโ,
โHuston Rocketโ
],
โanswerโ: โHuston Rocketโ
}
},
โmathsโ: {
โq1โ: {
โquestionโ: โ5 + 7 = ?โ,
โoptionsโ: [
โ10โ,
โ11โ,
โ12โ,
โ13โ
],
โanswerโ: โ12โ
},
โq2โ: {
โquestionโ: โ12 - 8 = ?โ,
โoptionsโ: [
โ1โ,
โ2โ,
โ3โ,
โ4โ
],
โanswerโ: โ4โ
}
}
}
}
Output Json
{
โquizโ: {
โsportโ: {
โq1โ: {
โquestionโ: โWhich one is correct team name in NBA?โ,
โoptionsโ: [
โNew York Bullsโ,
โLos Angeles Kingsโ,
โGolden State Warrirosโ,
โHuston Rocketโ
],
โanswerโ: โHuston Rocketโ,
โquiz_idโ : 123456789
}
},
โmathsโ: {
โq1โ: {
โquestionโ: โ5 + 7 = ?โ,
โoptionsโ: [
โ10โ,
โ11โ,
โ12โ,
โ13โ
],
โanswerโ: โ12โ,
โquiz_idโ : 123456789
},
โq2โ: {
โquestionโ: โ12 - 8 = ?โ,
โoptionsโ: [
โ1โ,
โ2โ,
โ3โ,
โ4โ
],
โanswerโ: โ4โ,
โquiz_idโ : 123456789
}
}
}
}
โ04-13-2020 06:22 AM
The wildcard JSON-Path approach should work here as well, but I think youโll need to use the Mapping Root feature of the Mapper to iterate over the outer array. So, in this case, youโd probably use the following as the Mapping Root in the Mapper:
$newPayload.PurchaseOrderERPRequest_V1.PurchaseOrder.item[*]
That tells the Mapper to apply the mappings in the table to all of the elements in the PurchaseOrder.item
array. Then, you would add a row to the mapping table that read the lineItemId
:
$lineItemId
And, then wrote it to every element of the AccountingCodingBlockAssignment
array, using this Target Path:
$AccountingCodingBlockDistribution.AccountingCodingBlockAssignment[*].lineItemId
โ04-15-2020 02:34 PM
thanks @tstack, archived this with below logic in mapper(did not used root path) :
Expression:
jsonPath($, โ$PurchaseOrderERPRequest_V1.PurchaseOrder.Item[*].lineItemIdโ)[0]
Target path:
$PurchaseOrderERPRequest_V1.PurchaseOrder.Item[0].AccountingCodingBlockDistribution.AccountingCodingBlockAssignment[*].[โlineItemIdโ]
as expected, this is distributing the value evenly across any number of object under AccountingCodingBlockAssignment,
however problem here is if item field have 100 lineItemId, its impractical to pass index for each, also we are not sure how many lineItemId will be there in each request.
How this can be handled dynamically so that no need to pass hard-coded index ?