0

How do you flatten the value in xml2js ? I am trying to flatten the value of sf:LastModifiedBy (field modified to lastModifiedByName). I am modifying the field names with the tagNameProcessor function. The valueProcessors can be used to modify values but if I console log the value none of the value is of type object which I think it should be.

Any idea what is wrong ?

The result is that the field should just be "lastModifiedByName": "Donald Duck" No object value just a string

Current code I am using

const resParsed = await xml2js.parseStringPromise(data, {
      ignoreAttrs: true,
      explicitArray: false,
      mergeAttrs: true,
      valueProcessors: [processors.parseNumbers, processors.parseBooleans],
      tagNameProcessors: [tagNameProcessor],
    });
'sf:Id': '',
'sf:LastModifiedBy': { 'sf:Id': '', 'sf:Name': 'Donald Duck' },
'sf:LastModifiedDate': '2021-06-01T12:00:18.000Z',
'sf:ParentId': '0PS3M0000004eOHAK',
'sf:PermissionsCreate': 'true',
'sf:PermissionsDelete': 'false',
'sf:PermissionsEdit': 'true',
'sf:PermissionsModifyAllRecords': 'false',
'sf:PermissionsRead': 'true',
'sf:PermissionsViewAllRecords': 'false',
'sf:SobjectType': 'SurveyResponse'

results in (not the same as above)

{
        "id": "1103M000000uLAfQAM",
        "lastModifiedByName": {
            "id": "",
            "lastModifiedByName": "Donald Duck"
        },
        "lastModifiedDate": "2021-04-29T07:54:29.000Z",
        "parentId": "0PS3M0000004cZtWAI",
        "create": true,
        "delete": true,
        "edit": true,
        "modifyAllRecords": false,
        "read": true,
        "viewAllRecords": false,
        "sObject": "Item_BOM_Package_Junction__c"
    }

Stackblitz: https://stackblitz.com/edit/stackblitz-starters-mce18q?file=index.js

In short, how do I parse this:

<sf:LastModifiedBy xsi:type="sf:User">
    <sf:Id xsi:nil="true"/>
    <sf:Name>Donald Duck</sf:Name>
</sf:LastModifiedBy>

into this:

"sf:LastModifiedBy": "Donald Duck"
jani_r
  • 637
  • 1
  • 7
  • 18

0 Answers0