I have an object with JSON like string which is returned by a function tableToJson(). This is what it looks like.
{
{"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"},
{"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"},
{"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"},
{"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"}
}
How to save this on firebase database. I am already saving some data above it with JS looks like this:
var databaseRef = firebase.database().ref('Bills');
function submitData(e){
e.preventDefault();
//variables for getting all values
var receiptDate = document.getElementById('date').innerHTML;
var receiptTime = document.getElementById('time').innerHTML;
var receiptBillNo = getInputValues('billNo');
var receiptCompName = getInputValues('panyName');
var receiptEmail = getInputValues('email');
var receiptPhone = getInputValues('phone');
var discount = getInputValues('Discount');
var total = getInputValues('sumTotal');
var newDataref = databaseRef.push();
var itemsJson = tableToJson();//THIS WILL RETURN THE ABOVE JSON STRING
//itemsJson = tableToJson();
//JSON.parse(itemsJson);
newDataref.set({
date: receiptDate,
time: receiptTime,
billNo: receiptBillNo,
pany: receiptCompName,
email: receiptEmail,
phone: receiptPhone,
discount: discount,
total:total,
zitems: itemsJson
});
}
Have look at my tableToJson()
function tableToJson(){
var tableId = document.getElementById('dataEntryTable');
var headName;
var headers = [];
var dataArray = [];
var rowCount = tableId.rows.length;
var colCount = tableId.rows[0].cells.length;
dataArray.push("[");
for(var i = 1; i < colCount; i++){
headName = tableId.rows[0].cells[i].innerHTML;
headers.push(headName);
}
console.log(headers);
for(var i = 1; i < rowCount; i++){
dataArray.push("\n{");
//FOR FIRST APPROACH:dataArray.push("\n/"" + i + "/" :{");
for(var j = 1; j < colCount; j++){
var currValue = tableId.rows[i].cells[j].childNodes[0].value;
dataArray.push("\"" + headers[j-1] + "\":" + "\"" + currValue +
"\"");
if(j < (colCount - 1)){
dataArray.push(",");
}
}
if( i < (rowCount - 1)){
dataArray.push("},");
}
else{
dataArray.push("}/n");
}
}
dataArray.push("]");
return dataArray.join("");
}
Is something wrong with this code. Even if I parse it to JSON it is sending as string.
I have an object with JSON like string which is returned by a function tableToJson(). This is what it looks like.
{
{"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"},
{"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"},
{"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"},
{"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"}
}
How to save this on firebase database. I am already saving some data above it with JS looks like this:
var databaseRef = firebase.database().ref('Bills');
function submitData(e){
e.preventDefault();
//variables for getting all values
var receiptDate = document.getElementById('date').innerHTML;
var receiptTime = document.getElementById('time').innerHTML;
var receiptBillNo = getInputValues('billNo');
var receiptCompName = getInputValues('panyName');
var receiptEmail = getInputValues('email');
var receiptPhone = getInputValues('phone');
var discount = getInputValues('Discount');
var total = getInputValues('sumTotal');
var newDataref = databaseRef.push();
var itemsJson = tableToJson();//THIS WILL RETURN THE ABOVE JSON STRING
//itemsJson = tableToJson();
//JSON.parse(itemsJson);
newDataref.set({
date: receiptDate,
time: receiptTime,
billNo: receiptBillNo,
pany: receiptCompName,
email: receiptEmail,
phone: receiptPhone,
discount: discount,
total:total,
zitems: itemsJson
});
}
Have look at my tableToJson()
function tableToJson(){
var tableId = document.getElementById('dataEntryTable');
var headName;
var headers = [];
var dataArray = [];
var rowCount = tableId.rows.length;
var colCount = tableId.rows[0].cells.length;
dataArray.push("[");
for(var i = 1; i < colCount; i++){
headName = tableId.rows[0].cells[i].innerHTML;
headers.push(headName);
}
console.log(headers);
for(var i = 1; i < rowCount; i++){
dataArray.push("\n{");
//FOR FIRST APPROACH:dataArray.push("\n/"" + i + "/" :{");
for(var j = 1; j < colCount; j++){
var currValue = tableId.rows[i].cells[j].childNodes[0].value;
dataArray.push("\"" + headers[j-1] + "\":" + "\"" + currValue +
"\"");
if(j < (colCount - 1)){
dataArray.push(",");
}
}
if( i < (rowCount - 1)){
dataArray.push("},");
}
else{
dataArray.push("}/n");
}
}
dataArray.push("]");
return dataArray.join("");
}
Is something wrong with this code. Even if I parse it to JSON it is sending as string.
Share Improve this question edited Feb 27, 2018 at 6:27 mav-raj asked Feb 25, 2018 at 12:50 mav-rajmav-raj 7711 gold badge9 silver badges22 bronze badges 5-
Cause you convert it to a String???! Or what should
THIS WILL RETURN THE ABOVE JSON STRING
mean? – Jonas Wilms Commented Feb 25, 2018 at 12:55 - It will taking input values from a table and converting it to the JSON like string mentioned above – mav-raj Commented Feb 25, 2018 at 13:05
- So why are you confused that it is a string when you say it is a Json string ... ? – Jonas Wilms Commented Feb 25, 2018 at 13:09
- I said JSON like, sorry if its confusing you. – mav-raj Commented Feb 25, 2018 at 13:11
- I want to save all those values as an object not as string. Like its saving other values above it. – mav-raj Commented Feb 25, 2018 at 13:13
2 Answers
Reset to default 2There are two problems:
your
tableToJson
function returns a string, which you can solve by either making it return an actual JSON object, or by callingJSON.parse
as Jonas answered.Unfortunately the
JSON.parse()
will currently fail, due to the fact that your string is not valid JSON. You cannot nest objects in the way you've done. You'll either need to give each nested object a label:{ "one": {"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"}, "two": {"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"}, "three": {"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"}, "four": {"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"} }
Or return an array instead of an object:
[ {"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"}, {"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"}, {"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"}, {"Item Code":"sthing","Product Name":"sthing","Qantity":"1","Unit Price":"0","Item Total":"0"} ]
If you go with the latter approach, take a moment to read Firebase's blog post on using arrays.
May convert the JSON string back to a js object:
newDataref.set({
date: receiptDate,
/*...*/
zitems: JSON.parse(itemsJson)
});
But it would be definetly better to not stringify it at all.