How to parse json in javascript having dynamic key value pair? [duplicate] - javascript

This question already has answers here:
How do I enumerate the properties of a JavaScript object? [duplicate]
(14 answers)
Closed 7 years ago.
I want to parse a JSON string in JavaScript. The response is something like
var response = '{"1":10,"2":10}';
How can I get the each key and value from this json ?
I am doing this -
var obj = $.parseJSON(responseData);
console.log(obj.count);
But i am getting undefined for obj.count.

To access each key-value pair of your object, you can use Object.keys to obtain the array of the keys which you can use them to access the value by [ ] operator. Please see the sample code below:
Object.keys(obj).forEach(function(key){
var value = obj[key];
console.log(key + ':' + value);
});
Output:
1 : 10
2 : 20
Objects.keys returns you the array of the keys in your object. In your case, it is ['1','2']. You can therefore use .length to obtain the number of keys.
Object.keys(obj).length;

So you need to access it like an array, because your keys are numbers. See this fiddle:
https://jsfiddle.net/7f5k9het
You can access like this:
result[1] // this returns 10
result.1 // this returns an error
Good luck

Related

how to access values in javascript's object? [duplicate]

This question already has answers here:
Setting and getting object to local storage using stringify?
(4 answers)
Closed 8 months ago.
I am trying to iterate through the values in an object.
Here are the values of the object:
object=Object.values(localStorage)[0];
console.log(object);
Output
{"name":"e7","id":"7","category":"n"}
I tried:
console.log(object[0]);
But it printed just "{"... Then I tried... object[name], object['name'] , object[0][0]... but the output is not satisfactory...
I want to loop through these values like I want to access "name" and its value and so on...
Can I loop through individual values of JavaScript?
the output is actually a value ..
Local storage can only store strings, so what you get from it is just a JSON string and you have to parse it into a JavaScript object before you can use it like one.
The keys and the values are always strings (note that, as with objects, integer keys will be automatically converted to strings).
You can do it by calling JSON.parse() (documentation here).
var fromLocalStorage = '{"name":"e7","id":"7","category":"n"}';
// Parse the JSON string from localStorage into a JS object
var object = JSON.parse(fromLocalStorage);
// Get a single property
console.log(object.name);
// Loop all the key-value pairs of the object
Object.keys(object).forEach(function(key) {
console.log(key + ' -> ' + object[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

Get from JSON object with string IDs to JS numeric IDs [duplicate]

This question already has answers here:
Is there any way to use a numeric type as an object key?
(11 answers)
Closed 4 years ago.
i have a JSON object (sent from php) and I want to convert the IDs to a numeric key using JS. So currently it looks something like that:
let foo = {"66":"test","65":"footest"};
And now I want it to look like this:
let foo = {66:"test",65:"footest"};
Object keys do not need to be numerical to be accessed - as noted in the comments - they are strings regardless.- Below, I am console logging the "66" property using the brackets notation - foo[66]
let foo = {"66":"test","65":"footest"};
console.log(foo[66]); // gives "test"
// if you want to assign values numerically - ie using the index of a loop - then you could do
for(i = 63; i<65; i++) {
foo[i] = "test" + i;
}
console.log(foo); // gives {"63": "test63", "64": "test64","65": "footest","66": "test"}

Get JSON key name [duplicate]

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.

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