cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Formatting JSON data according to given scenarios

vaidyarm
Contributor

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
}
}
}
}

6 REPLIES 6

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

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 ?