0

The object API returning containing dots, spaces, and ( . ) in it. How to rename these keys.

[
 {
    CLIENTID: 100022,
    CLIENTNAME: 'DHIREN R RAJDEV',
    CLIENTCODE: '1011D',
    CLIENTTYPE: 'Discretionary',
    ACCOUNTTYPE: 'Separate Account',
    'Account Open Date': '30-06-2014',
    INCEPTIONDATE: '30-06-2014',
    PERFORMANCEREPORTINGDATE: '30-06-2014',
    CHARGEUPTO: 43289.00011574074,
    BANK ACC NUMBER.SINGLE: 239020,
    BANK ACC NUMBER.DOUBLE: 789032
},{
    CLIENTID: 100023,
    CLIENTNAME: 'KEERTHI',
    CLIENTCODE: '1011E',
    CLIENTTYPE: 'Discretionary',
    ACCOUNTTYPE: 'Separate Account',
    'Account Open Date': '30-06-2014',
    INCEPTIONDATE: '30-06-2014',
    PERFORMANCEREPORTINGDATE: '30-06-2014',
    CHARGEUPTO: 67893.00011574074,
    BANK ACC NUMBER.SINGLE: 456781,
    BANK ACC NUMBER.DOUBLE: 345234
},{
    CLIENTID: 100024,
    CLIENTNAME: 'RAJDEV R',
    CLIENTCODE: '1011W',
    CLIENTTYPE: 'Discretionary',
    ACCOUNTTYPE: 'Separate Account',
    'Account Open Date': '30-06-2014',
    INCEPTIONDATE: '30-06-2014',
    PERFORMANCEREPORTINGDATE: '30-06-2014',
    CHARGEUPTO: 239076.00011574074,
    BANK ACC NUMBER.SINGLE: 541234,
    BANK ACC NUMBER.DOUBLE: 340987
}
]

Like change key "BANK ACC NUMBER.SINGLE" to BANK_ACC_NUMBER_SINGLE and BANK ACC NUMBER.DOUBLE to BANK_ACC_NUMBER_DOUBLE.

6
  • @Andreas The above JSON absolutely doesn't validate. Please explain what you mean by your comment. Commented Jun 18, 2021 at 6:51
  • I hope these keys are inside quote? Commented Jun 18, 2021 at 6:54
  • @TimBiegeleisen The above does not validate because all keys need to have double quote and all values must use double quote as well instead of single quote. Spaces and dots in keys are no issue.. Commented Jun 18, 2021 at 6:55
  • I just export the data from excel. Some keys come with a double-quote and some of them are not. Commented Jun 18, 2021 at 6:59
  • @NicoVanBelle Right...so my guess is that the OP wants to remove these space and dot characters (or replace them) so that the JSON becomes valid. Commented Jun 18, 2021 at 7:00

1 Answer 1

1

Hope this will help you.

Logic.

  • Loop through the array of object and pick individual objects.
  • Loop through the keys of that specific object.
  • Check whether there is a dot or a space in that key. If there is a dor or space, format that key string as per the requirement, and aassign the value of the old key to that new one.
  • Delete the node with dot or space from the object and add the new formated key with the old value.
    const list = [
      {
        CLIENTID: 100022,
        CLIENTNAME: 'DHIREN R RAJDEV',
        CLIENTCODE: '1011D',
        CLIENTTYPE: 'Discretionary',
        ACCOUNTTYPE: 'Separate Account',
        'Account Open Date': '30-06-2014',
        INCEPTIONDATE: '30-06-2014',
        PERFORMANCEREPORTINGDATE: '30-06-2014',
        CHARGEUPTO: 43289.00011574074,
        'BANK ACC NUMBER.SINGLE': 239020,
        'BANK ACC NUMBER.DOUBLE': 789032
      }, {
        CLIENTID: 100023,
        CLIENTNAME: 'KEERTHI',
        CLIENTCODE: '1011E',
        CLIENTTYPE: 'Discretionary',
        ACCOUNTTYPE: 'Separate Account',
        'Account Open Date': '30-06-2014',
        INCEPTIONDATE: '30-06-2014',
        PERFORMANCEREPORTINGDATE: '30-06-2014',
        CHARGEUPTO: 67893.00011574074,
        'BANK ACC NUMBER.SINGLE': 456781,
        'BANK ACC NUMBER.DOUBLE': 345234
      }, {
        CLIENTID: 100024,
        CLIENTNAME: 'RAJDEV R',
        CLIENTCODE: '1011W',
        CLIENTTYPE: 'Discretionary',
        ACCOUNTTYPE: 'Separate Account',
        'Account Open Date': '30-06-2014',
        INCEPTIONDATE: '30-06-2014',
        PERFORMANCEREPORTINGDATE: '30-06-2014',
        CHARGEUPTO: 239076.00011574074,
        'BANK ACC NUMBER.SINGLE': 541234,
        'BANK ACC NUMBER.DOUBLE': 340987
      }
    ]
    list.forEach((item) => {
      Object.keys(item).forEach((key) => {
        if (key.indexOf(' ') > -1 || key.indexOf('.') > -1) {
          newKey = key.split(' ').join('_');
          newKey = newKey.split('.').join('_');
          item[newKey] = item[key];
          delete(item[key]);
        }
      })
    })
    console.log(list)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.