Get JSON key name [duplicate] - javascript

This question already has answers here:
Javascript get Object property Name
(4 answers)
Closed 6 years ago.
I have the following JSON data: {"success":"You are welcome"} that I have named json in my JavaScript code.
When I want to alert You are welcome I do json.success. So now the problem I am facing is that, what about if I want to alert success. Is there any way to get it?

So now the problem I am facing is that, what about if I want to alert
success. Is there a need way to get it ?
If your object is
var obj = {"success":"You are welcome"};
You can get the array of keys as
var keys = Object.keys(obj);
and then print it as
console.log( keys[ 0 ] ); //or console.log( keys.join(",") )
var obj = {"success":"You are welcome"};
var keys = Object.keys(obj);
console.log(keys[0]);

You mean something like this?
keys = Object.keys(json_object)
key_to_use = keys[0];

Try this code
alert(Object.keys({"success":"You are welcome"})[0]);

Since you're able to do json.success, you don't have "JSON data", you have a Javascript Object. JSON, or JavaScript Object Notation, is no more than the serialization of a Javascript object.
As other answers have stated, you can use Object.keys() to list the fields of an object.

Object.keys() can be called on any JavaScript object to get back a list of keys.

Related

How to stringify new Map()? [duplicate]

This question already has answers here:
How do you JSON.stringify an ES6 Map?
(16 answers)
Closed 3 years ago.
I have the following code:
const x = new Map([['key', 'value']])
Now, I want to stringify this. I used the following:
JSON.stringify(x)
...which gave me an empty object.
How do you actually perform the stringification on new Map()? Is it something like you first turn it into an object and then stringify it?
I am expecting the output to be {"key": "value"}.
You could get the array from the map and stringify the result.
const x = new Map([['key', 'value']])
console.log(JSON.stringify(Array.from(x)));
If you want to access direct value then you can use this in loop.
console.log(JSON.stringify(x.get('key')));

Array to json using javascript [duplicate]

This question already has answers here:
JavaScript associative array to JSON
(5 answers)
Closed 4 years ago.
I am trying to pass an array through jQuery's ajax. The problem is that when I try to pass the array created in JavaScript to JSON, it returns something empty. I even try console.log, but when I try to convert it to JSON there is nothing. Here is a representation of how I do it:
var data = [];
data['name'] = 'test';
data['mail'] = 'test';
data['pass'] = 'test';
console.log(JSON.stringify(data)); // result []
Every array is an object. You are assigning object properties with the data['name'] = 'test' syntax. Arrays are indexed with integers and they "must" be in sequence. try a[0] = 'foo'. or Array.push

how to add key and a value to a json with same existing Key using Javascript [duplicate]

This question already has answers here:
How to add new property with same key name inside declared object?
(3 answers)
Closed 6 years ago.
I have a json variable like this
var jsondata={"key1":"val1", "key2":"val2"}
I want to push another object with same existing key, and i want that my variable will be like this
var jsondata={"key1":"val1", "key2":"val2", "key1":"val3"}
I tried jsondata["key1"] = "val3", but it didn't return the wanted result
Thank you in advance.
you cannot, as it is a map.
but you could create this json :
var jsondata={"Name":["Jhon","James"], "Age":40}
You can't use the same key in an object. Your question suggests that the logic behind your data structure is wrong.
An alternative:
Use a different field name, i've used "_Name" below, but perhaps "Second_Name" would be more appropriate. Unsure what your json data is modelling.
var jsondata={"Name":"Jhon", "Age":40, "_Name":"James"};
Or perhaps it makes sense to store an array of people, is that what you're trying to achieve? i.e. you have two people, with the names "Jhon" and "James"?
var jsondata={
"people": [
{"Name":"Jhon", "Age":40},
{"Name":"James"}
]
};

JSON stringify returns empty string [duplicate]

This question already has answers here:
JSON.stringify doesn't work with normal Javascript array
(6 answers)
Closed 7 years ago.
In Javascript I try to use stringify but it keeps returning an empty string. What is wrong here? Feel free to edit the Fiddle.
JS
values = [];
values['belopp'] = 2322;
values['test'] = 'jkee';
str = JSON.stringify(values);
console.log(values);
console.log(str); // Expected to show a json array
JS Fiddle
https://jsfiddle.net/L4t4vtvd/
You are trying to use something that is meant for an object on an array.
values = {};
values['belopp'] = 2322;
values['test'] = 'jkee';
str = JSON.stringify(values);
This is the updated fiddle.
You are stringifying an array ([]), not an object ({}) Therefore, values = {};

How to remove an object from JSON response object [duplicate]

This question already has answers here:
Find and remove objects in an array based on a key value in JavaScript
(14 answers)
Closed 8 years ago.
I have a JSON response that appears as shown below. What I am trying to accomplish remove an object from the vaiable with a specifc key value pair using javascript
JSON Response.
console.log(userJSON); //response is below.
[Object {startDate="1403496000", name="user10"},
Object {startDate="1401681600", name="user11"},
Object {startDate="1423544400", name="user12"},
Object {startDate="1370836800", name="user13"},
Object {startDate="1370836800", name="user14"},
Object {startDate="1370750400", name="user15"},
Object {startDate="1402286400", name="user16"},
Object {startDate="1404273600", name="user17"}]
I want to be able to remove user10's object completly with the given variable name
Logic
var removeUser = user15;
//Do logic.
updatedUserJSON =
[Object {startDate="1403496000", name="user10"},
Object {startDate="1401681600", name="user11"},
Object {startDate="1423544400", name="user12"},
Object {startDate="1370836800", name="user13"},
Object {startDate="1370836800", name="user14"},
Object {startDate="1402286400", name="user16"},
Object {startDate="1404273600", name="user17"}]
Can someone help me get started on how to accomplish this using javascript? Would very much appreciate the help. Thank you.
var nameToDelete = 'user15';
for(var index=0; index < userJSON.length; index++)
{
if (userJSON[index].name == nameToDelete)
{
userJSON.splice(index,1);
break;
}
}

Categories