join data by position of different json elements on dataweave 2.0 join data by position of different json elements on dataweave 2.0 json json

join data by position of different json elements on dataweave 2.0


Something like this should work

%dw 2.0output application/jsonvar filteredExport = (payload.Z_HYD_GET_INVOICES."export") filterObject ($$ ~= "ACCOUNTING" or $$ ~= "FISCAL")var filteredExportArray = filteredExport pluck $var accounting = filteredExport.ACCOUNTING pluck $var fiscal = filteredExport.FISCAL pluck $---{    invoices: filteredExportArray map {        accounting: {            accountingDocumentID: accounting[$$].DOCNUM,            taxEntryDate: accounting[$$].PSTDAT,            company: accounting[$$].BUKRS        },        fiscal: {            fiscalDocument: {                fiscalDocumentID: fiscal[$$].DOC_DOCNUM,                fiscalDocumentCategory: if(!isEmpty(fiscal[$$].DOC_NFTYPE)) fiscal[$$].DOC_NFTYPE else if(!isEmpty(fiscal[$$].DOC_INVTYPE)) fiscal[$$].DOC_INVTYPE else null            }        }    }}

You could also use a join function on the two arrays as you mentioned you'd like to see

%dw 2.0import * from dw::core::Arraysoutput application/jsonvar filteredExport = (payload.Z_HYD_GET_INVOICES."export") filterObject ($$ ~= "ACCOUNTING" or $$ ~= "FISCAL")var accounting = filteredExport.ACCOUNTING pluck $var fiscal = filteredExport.FISCAL pluck $var joined = leftJoin(accounting, fiscal, (a) -> a.DOCNUM, (f) -> f.DOC_DOCNUM)---{    invoices: joined map {        accounting: {            accountingDocumentID: $.l.DOCNUM,            taxEntryDate: $.l.PSTDAT,            company: $.l.BUKRS        },        fiscal: {            fiscalDocument: {                fiscalDocumentID: $.r.DOC_DOCNUM,                fiscalDocumentCategory: $.r.DOC_NFTYPE            }        }    }}


Give this a shot.

%dw 2.0output application/jsonfun returnFiscalDoc (docnum) = {    fiscalDocument : {(payload.Z_HYD_GET_INVOICES.export.FISCAL.*row filter ($.DOC_DOCNUM ~= docnum) map  {        fiscalDocumentID: $.DOC_DOCNUM,        fiscalDocumentCategory: $.DOC_NFTYPE default $.DOC_INVTYPE    })}}---invoices: payload.Z_HYD_GET_INVOICES.export.ACCOUNTING.*row map {   accounting :      {         accountingDocumentID: $.DOCNUM,         taxEntryDate: $.PSTDAT,         company:$.BUKRS     },   fiscal: returnFiscalDoc($.DOCNUM)}

Though keep in mind your data under FISCA.row has DOC_NFTYPE and DOC_INVTYPE. Thus used default in the function above to get to the value of the required.

"FISCAL": {                "row": {                    "DOC_DOCNUM": "0002990790",                    "DOC_NFTYPE": "ZW"                },                "row": {                    "DOC_DOCNUM": "0003006170",                    "DOC_INVTYPE": "ZW"                }            }