Reputation: 11
Hi I just have a question on how can I convert this API response into json data. I tried using explode but the response seems like separated into array.
StatCode: 11
StatName: cancelled
OrderID: OP_AFIFAH_6889
Amount: 1.10
TranID: 143519171
Domain: test
BillingDate: 2019-11-26 16:58:49
BillingName: test
VrfKey: test
Channel: credit
Currency: MYR
ErrorCode: CC_000
ErrorDesc: Successful Payment :
Maybe something like this
{
"StatCode": 11,
"StatName": "cancelled",
"OrderID": "OP_AFIFAH_6889",
"Amount": 1.10,
"TranID": 143519171
etc..
}
Thanks
Upvotes: 0
Views: 5638
Reputation: 680
The best way is changing the API to return the JSON object. If you don't have access to the API, I wrote a simple javascript for you which can convert it to the JSON object:
function toJSON(input) {
const lines = input.split('\n')
const result = []
for (let i = 1; i < lines.length; i++) {
if (!lines[i])
continue;
const obj = {}
const currentline = lines[i].split(':')
obj[currentline[0]] = currentline[1]
result.push(obj)
}
return JSON.stringify(result)
}
console.log(toJSON(`StatName: cancelled
OrderID: OP_AFIFAH_6889
Amount: 1.10
TranID: 143519171
Domain: test
BillingDate: 2019-11-26 16:58:49
BillingName: test
VrfKey: test
Channel: credit
Currency: MYR
ErrorCode: CC_000
ErrorDesc: Successful Payment :`)
);
Upvotes: 2
Reputation: 474
[
{
"StatName": "cancelled",
"OrderID": "OP_AFIFAH_6889",
"Amount": 1.10,
"TranID": 143519171,
"Domain": "test",
"BillingDate": "2019-11-26 16:58:49",
"BillingName": "test",
"VrfKey": "test",
"Channel": "credit",
"Currency": "MYR",
"ErrorCode": "CC_000",
"ErrorDesc": "Successful Payment"
}
]
Note: Amount and TranID values are both numbers ,if you want them to be a type of string kindly add double quotes to them. Thanks
Upvotes: 0