javascript convert array to json object [duplicate] - javascript

This question already has answers here:
JavaScript: Converting Array to Object
(2 answers)
Closed 8 years ago.
I've got the following array:
array = [{"id":144,"price":12500000},{"id":145,"price":13500000},
{"id":146,"price":13450000},{"id":147,"price":11500000},
{"id":148,"price":15560000}]
i wanto convert it to json like this:
json = {{"id":144,"price":12500000},{"id":145,"price":13500000},
{"id":146,"price":13450000},{"id":147,"price":11500000},
{"id":148,"price":15560000}}
So than i can store everything in mongodb in a unique document.
Regards,

Just run a loop and equate...like...
var obj = {};
for(var i=0; i<array.length; i++)
{
obj[i] = array[i]
}
It will do
{
0:{"id":144,"price":12500000},
1:{"id":145,"price":13500000},
2:{"id":146,"price":13450000},
3:{"id":147,"price":11500000},
4:{"id":148,"price":15560000}
}
Because your JSON is invalid.

Not sure what you are asking
From array or another variable to json string =>
var str = JSON.stringify(thing);
From a json string to variable
var thing = JSON.parse(str);

Related

How to append JSON object to JSON object? [duplicate]

This question already has an answer here:
Dynamic object property names?
(1 answer)
Closed 8 months ago.
so i have this JSON file:
{"fjord-200-200.jpg":[]}
I read it using
let data = fs.readFileSync(cachedName);
let cachedInJSON: JSON = JSON.parse(data.toString());
so i have now JSON object, that i want to append to this new JSON object
let newData = {
fileNameFormatted: []
}
when i tried using
let newJson = {...cachedInJSON, ...newData}
Result was this:
{"fjord-200-200.jpg":[],"fileNameFormatted":[]}
Wanted result that i want to achieve is:
{"fjord-200-200.jpg":[],"fjord-300-300.jpg":[]}
fileNameFormatted is a variable holding fjord-300-300.jpg
let newData = {
[fileNameFormatted]: [] // Notice the brackets around the key
}

Is there a way to convert string to json object in javascript [duplicate]

This question already has answers here:
Safely turning a JSON string into an object
(28 answers)
Closed 4 years ago.
I am trying to convert list of string into json object.
My string is : personName + rank.
This is string is arranged as per the person's rank.
PS: the backend code is in solidity
My code so far is as follows:
var ranks = new Array(5);
PersonNames = Object.keys(names);
ranks = contractInstance.dashboard.call(PersonNames);// function in solidity
var finalList = [];
for (var j = 0; j < ranks.length; j++){
var winList = web3.toAscii(ranks[j]);
var winCount = contractInstance.RanksFor.call(ranks[j]).toString();
var person = {};
person["name"] = winList[j];
console.log(winList[j]);
person["ranks"] = winCount[j];
finalList.push(person);
}
var myJSON = JSON.stringify(finalList);
Please let me know if I am making use of the correct syntax.
I am learning how to implement javascript. Any help would be appreciated. Thank you!!
I think using the provided method is the best way...
Example 1:
var json = JSON.parse('{ "hello":"world" }');
Example 2:
var json = '{"hello":"world"}';
obj = JSON.parse(json);

how to create array object using integer variable javascript [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 7 years ago.
I need to fill an array object as like below using integer variable.
for(i=1; i<=2; i++)
arr[i].push({i:(100 * i)})
Expected result is:
arr = [{ 1:100,2:200},{1:100,2:200}]
Problem is, array created as like below
arr = [{i:100,i:200},{i:100,i:200}]
You need to push to arr instead of arr[i].
Also, you can't use a variable as a key in json directly.
var arr = [];
for(i=1; i<=2; i++)
{
var b = {};
b[i] = 100*i;
arr.push({[i]:(i*100)});
}
console.log(arr);

how to add repeating strings value in json array [duplicate]

This question already has answers here:
Adding a new array element to a JSON object
(6 answers)
Closed 7 years ago.
I have JSON as
var newJSON = [{"key":"India","value":"72"},{"key":"India","value":"27"},{"key":"Pakistan","value":"90"},{"key":"Zimbamwe","value":"88"},{"key":"India","value":"100"},{"key":"Pakistan","value":"172"}]
I want desired result as
[{"key":"India","value":"199"},{"key":"Pakistan","value":"262"},{"key":"Zimbamwe","value":"88"}]
Please help me with this
are you sure you want them as {"india","20"}??
or is it {"india":"20"} //this is recommended
then you can do
json["india"]
or json[0]
Update:
var yourObj={key:'india', value:72};
var newObj = {};
newObj[yourObj.key] = yourObj.value;
newObj["Austria"]=100; // adds as a new list object
You could use
var jsondata =[];
var firstvalue = {country:"India",number:"200"};
jsondata.push(firstvalue);
var secondvalue = {country:"Australia",number: "210"};
jsondata.push(secondvalue);
for(var i =0; i< jsondata.length;i++){
console.log(jsondata[i].country);
}

Json data to javascript array? [duplicate]

This question already has answers here:
How to parse CSV data?
(14 answers)
Closed 8 years ago.
How do I take data in a json file given like this:
Ticker,Quandl Code,Name
STI,GOOG/NYSE_STI,SunTrust Banks
AAPL,GOOG/NASDAQ_AAPL,Apple Inc
and turn it into an array like this:
var stocks = [
["STI,GOOG/NYSE_STI,SunTrust Banks"],
["AAPL,GOOG/NASDAQ_AAPL,Apple Inc"]
];
Your input is in CSV, not JSON. The following question has a function that converts CSV for an array:
Javascript code to parse CSV data
you can do this :
var myarray = [];
var myJSON = "";
for (var i = 0; i < 10; i++) {
var item = {
"value": i,
"label": i
};
myarray.push(item);
}
myJSON = JSON.stringify({myarray: myarray});
alert(myJSON);

Categories

Resources