0

Can someone hint me how to extract the NC value of the string-based custom key-value format which is the domainDetails related value of the below data-structure ...

{
  notAfterTimeStamp: 123456787,
  notBeforeTimeStamp: 123435637,
  domainDetails: 'NC=test.com,O=xyz,L=uvw,C=AS',
  Requested: 'CN=issuer Private Test CA ,OU=abc,OU=Testing',
} 

I tried to extract and print the value of NC from domainDetails as below. But the result of my attempt is undefined.

console.log("domainDetails:"+JSON.stringify(response.data.domainDetails.NC));
Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
Divya
  • 9
  • 4
  • 1
    Would you mind sharing an [mcve] of what you've already tried? It would also be pertinent to read up on how to access property values in JS – evolutionxbox Mar 16 '23 at 13:51
  • @evolutionxbox- yes.updated the question that what I tried. – Divya Mar 16 '23 at 14:10
  • `JSON.stringify` converts an object _into_ JSON. If the data is already an object, then `response.data.domainDetails` will get the string, but `.NC` won't work because there is no such property – evolutionxbox Mar 16 '23 at 14:11
  • @evolutionxbox Yes. Is there a way to get the value of NC in this case? – Divya Mar 16 '23 at 14:42
  • Does this help https://stackoverflow.com/questions/1293147/how-to-parse-csv-data? – evolutionxbox Mar 16 '23 at 14:43
  • There is no real complexity and the issue hardly has anything to do with JSON ... it all boils down to how to parse a specific value (here `NC`) from a string (here `"NC=test.com,O=xyz,L=uvw,C=AS",`) – Peter Seliger Mar 16 '23 at 15:13
  • @evolutionxbox ... The string the OP needs to parse from does not validly cover CSV formats but seems to feature some custom key-value format which one might try tackling with a regex like ... [`/(?[_\p{L}\p{N}\p{S}]+)=(?:(?:(?['"])(?.*?(?<!\\))\k)|(?[^,\n\r]+))/ug`](https://regex101.com/r/oKOAdE/1) – Peter Seliger Mar 16 '23 at 16:08
  • @Divya ... 1/2 ... A straight forward parsing approach which covers exactly the custom schema of the values of `domainDetails` respectively `Requested` looks like this ... `Object.fromEntries("NC=test.com,O=xyz,L=uvw,C=AS".split(',').map(customEntry => customEntry.split('=')))['NC']`. It splits the string at any occurring comma. The result array gets `map`ped into an array of `key` and `value` items by splitting each custom entry at the `=` character. – Peter Seliger Mar 16 '23 at 16:26
  • @Divya ... 2/2 ... The resulting array of entry-arrays then gets passed to `Object.fromEntries` which creates an object where the OP finally can access the specific value the OP is looking for ... here by using the `NC`-key. – Peter Seliger Mar 16 '23 at 16:27
  • @PeterSeliger -- Thanks a lot. Your suggestion worked in my case. – Divya Mar 16 '23 at 16:44

1 Answers1

0

From my above comments ...

"There is no real complexity and the issue hardly has anything to do with JSON ... it all boils down to how to parse a specific value (here NC) from a string (here "NC=test.com,O=xyz,L=uvw,C=AS")"

A straight forward parsing approach which covers exactly the custom schema of the values of domainDetails respectively Requested looks like this ...

Object
  .fromEntries(
    'NC=test.com,O=xyz,L=uvw,C=AS'
      .split(',')
      .map(customEntry => customEntry.split('='))
  )['NC']

It splits the string at any occurring comma. The result array gets mapped into an array of key and value items by splitting each custom entry at the = character.

The resulting array of entry-arrays then gets passed to Object.fromEntries which creates an object where the OP finally can access the specific value the OP is looking for ... here by using the NC-key.

The executable example code which takes the OP's data-structure into account will look like this ...

const responseData = {
  notAfterTimeStamp: 123456787,
  notBeforeTimeStamp: 123435637,
  domainDetails: 'NC=test.com,O=xyz,L=uvw,C=AS',
  Requested: 'CN=issuer Private Test CA ,OU=abc,OU=Testing',
};

const parsedResponseData = Object
  .assign({}, responseData, {
    domainDetails: Object.fromEntries(
      responseData.domainDetails
        .split(',')
        .map(customEntry => customEntry.split('='))
    )
  });
const nc = parsedResponseData.domainDetails.NC

console.log({
  responseData,
  parsedResponseData,
  nc,
});
.as-console-wrapper { min-height: 100%!important; top: 0; }
Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
  • Can you please give me a hint to replace the text as O=DC l.m.c.,L=Apac Domain Area,C=GB instead of O=xyz,L=uvw,C=AS', in domainDetails after getting NC value? My entire Output has to be - NC=test.com,O=DC l.m.c.,L=Apac Domain Area,C=GB – Divya Mar 21 '23 at 05:49
  • @Divya ... what did you try? What happens if one just processes the new string value as one has already before ?.. `Object.fromEntries( 'NC=test.com,O=DC l.m.c.,L=Apac Domain Area,C=GB'.split(',').map(customEntry => customEntry.split('=')) )` – Peter Seliger Mar 21 '23 at 09:00