javascript to get substring of recordNumber

Viewed 26

I have a string as below and i wanted to extract the recordNumber value. For ex: i need to extract the value like PTL-HPCAN-2022-00157

{"Request":"{\"header\":{\"action\":\"SubmitRecordAndCalculateFees\",\"username\":\"PUBLICUSER\",\"trxId\":\"2c348091-3f1c-4ccb-b854-92cd5d164565\",\"language\":\"EN\"},\"body\":{\"recordNumber\":\"PTL-HPCAN-2022-00157\"}}"}
2 Answers

Try below code:

var inputString = {"Request":"{\"header\":{\"action\":\"SubmitRecordAndCalculateFees\",\"username\":\"PUBLICUSER\",\"trxId\":\"2c348091-3f1c-4ccb-b854-92cd5d164565\",\"language\":\"EN\"},\"body\":{\"recordNumber\":\"PTL-HPCAN-2022-00157\"}}"}

console.log(JSON.parse(inputString.Request).body.recordNumber)

Your string seems to be an invalid string, please check the below code on how to extract the desired output!

const str = "{\"header\":{\"action\":\"SubmitRecordAndCalculateFees\",\"username\":\"PUBLICUSER\",\"trxId\":\"2c348091-3f1c-4ccb-b854-92cd5d164565\",\"language\":\"EN\"},\"body\":{\"recordNumber\":\"PTL-HPCAN-2022-00157\"}}";

const obj = JSON.parse(str);

console.log(obj.body.recordNumber);

Related