Your JSON is malformed. Try this:
var pets = '{"pets":[{"name":"jack"},{"name":"john"},{"name":"joe"}]}';
var arr = JSON.parse(pets);
alert(arr.pets[0].name);
Answer from Bradley Mountford on Stack OverflowYour JSON is malformed. Try this:
var pets = '{"pets":[{"name":"jack"},{"name":"john"},{"name":"joe"}]}';
var arr = JSON.parse(pets);
alert(arr.pets[0].name);
JSon arrays are bounded by [] brackets
try
pets = '[{"name":"jack"},{"name":"john"},{"name":"joe"}]';
also you forgot to use "'s on the last property name.
javascript - Convert array to JSON - Stack Overflow
How to return a Json Array
QUESTION - How to asign entire JSON object array to a single variable in Javascript?
Converting JSON value into JavaScript array - Stack Overflow
Videos
Script for backward-compatibility: https://github.com/douglascrockford/JSON-js/blob/master/json2.js
And call:
var myJsonString = JSON.stringify(yourArray);
Note: The JSON object is now part of most modern web browsers (IE 8 & above). See caniuse for full listing. Credit goes to: @Spudley for his comment below
I made it that way:
if I have:
var jsonArg1 = new Object();
jsonArg1.name = 'calc this';
jsonArg1.value = 3.1415;
var jsonArg2 = new Object();
jsonArg2.name = 'calc this again';
jsonArg2.value = 2.73;
var pluginArrayArg = new Array();
pluginArrayArg.push(jsonArg1);
pluginArrayArg.push(jsonArg2);
to convert pluginArrayArg (which is pure javascript array) into JSON array:
var jsonArray = JSON.parse(JSON.stringify(pluginArrayArg))
I am working on an HTML project that utilizes data stored inside a separate JSON file. I want to assign the entire object array to a single variable in JavaScript without any alterations.
I have already tried using the "fetch" and "parse" methods, but unfortunately, I have not succeeded yet. I feel like there must be a straightforward solution that I am not aware of, considering JSON and JavaScript have practically the same syntax. However, I am facing significant challenges in achieving this.
I need to pursue this approach because writing the entire object array inside the HTML file would significantly increase its line count by the hundreds, which I don't want. My intention is to store the data in a variable called "pokemon_list," which will be later iterated by "for" loops.
Here is a small excerpt from the JSON file:
[
{
"name": "bulbasaur",
"type": "grass/poison"
},
{
"name": "ivysaur",
"type": "grass/poison"
},
{
"name": "venusaur",
"height": "grass/poison"
}
]
Use JSON.parse and then reduce to get what you want,
var s = `[{"role":"noi_user"},{"role":"bert_user"}]`
var arr = []
try {
arr = JSON.parse(s).reduce((acc, val)=>[...acc, val.role], [])
} catch (e){
console.log("Invalid json")
}
console.log(arr)
Is this what you are loking for ? You can map on your array and just extract the role attribute of each datum.
const jsonString = ...
const data = JSON.parse(jsonString).map(data => data.role);
console.log(JSON.stringify(data, null, 2));
JSON uses double quotes to delimit strings and field names.