Related to this post : Parsing JSON to object using JSON.parse
I am facing similar issue and using console.dir(responseData.data, { depth: null }) i am able to get full text for 'credApp' field. However, When i try to access the element like 'responseData.data.orderItems.credDecision.credApp' to validate it against another variable then I get 'credApp' value as undefined.
I am able to get the values till credDecision such as responseData.data.orderId. Is there anyway to resolve this issue. Any help is much appreciated.
here is the code:
const getResponseData = (url) => new Promise((resolve, reject) => {
http.get(url, res => {
const { statusCode } = res;
const contentType = res.headers['content-type'];
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => { rawData += chunk; });
res.on('end', () => resolve({ statusCode, contentType, rawData }));
}).on('error', e => reject(e));
});
const responsePayload = await getResponseData(api_url+ orderNumber);
var responseData = JSON.parse(responsePayload.rawData);
console.dir(responseData.data, { depth: null });
var orderId = responseData.data.orders.externalOrderId;
var credApp = responseData.data.orderItems.credDecision.credApp;
Posting a sample part of api response since the response payload is large, I am trying to fetch OriginalOrderId and it is returned as undefined when I convert it into JSON and validated against another field:
"data": {
ringId: '1',
consumerId: '2',
orderSummary: {
guestCheckout: null,
id: '1233'
orders: [{
orderId: 'ABC1233',
orderType: '1'
}],
dateCreated: '2021-10-18T16:28:46.964Z',
createdBy: 'User1'
},
orders: [{
OriginalOrderId: '9693'
}]
}