'React Native Array To Json
I'm implementing a function to get data from the SQLite database. The function is getTotalSalesDetails. Using this function I can successfully get the data to the totalSalesDetailsList Array. I Want to convert this array to a JSON object. Is there a way to do it?
const [totalSalesDetailsList, setTotalSalesDetailsList] = useState([]);
const getTotalSalesDetails = () => {
db.transaction(txn => {
txn.executeSql(
`SELECT invoices.id, invoices.name, invoices.date, sales.salesID, sales.totaltax, sales.totalpriceNotax, sales.totalPrice, items.itemName, items.price, items.itemId, stransaction.quantity, stransaction.tid
FROM stransaction
INNER JOIN invoices ON stransaction.fk_invoice_id = invoices.id
INNER JOIN sales ON stransaction.fk_sales_id = sales.salesID
INNER JOIN items ON stransaction.fk_item_id = items.itemId
WHERE stransaction.fk_invoice_id = ?`,
[lastInvoiceDetails2],
(sqlTxn, res) => {
let len = res.rows.length;
if (len > 0) {
let results = [];
for (let i = 0; i < len; i++) {
let item = res.rows.item(i);
results.push({ id: item.id, name: item.name, date: item.date, salesID: item.salesID, totaltax: item.totaltax, totalpriceNotax: item.totalpriceNotax, totalPrice: item.totalPrice, itemName: item.itemName, price: item.price, itemId: item.itemId, quantity: item.quantity });
}
setTotalSalesDetailsList(results);
}
},
error => {
},
);
});
};
I want to convert the Arry to JSON object the format should be like this
"CompanyCode" : "XXX",
"InvoiceNumber": "123",
"CustomerName": "Name",
"Date": "2022-01-08",
"TotalTax" : 200,
"TotalPrice" : 100,
"Items": [
{
"IteamName": "Cotten",
"IteamQut": 2,
"UnitPrice": 10,
},
{
"IteamName": "Textile",
"IteamQut": 2,
"UnitPrice": 10,
}
]
Solution 1:[1]
you can use JSON.stringify(arr); . for more info see this
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Meisan Saba |
