How to get data from Json Object using javascript [duplicate] - javascript

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 4 years ago.
I'm getting data as a json object
var data = JSON.parse(JSON.stringify(data));
console.log(data);
On data console I got this. I want to get key and its value .
{lobby: {…}}
lobby:
8jmb9ca3s04c8el4j5sf0d:"AD8BJBkMKCBoYg_qAAAB"
38cjllj78cx0lic58ujxou:"PX51X9z_M34_9BvtAAAD"
ba8gs1y8779kmakdapxk1:"UowsBDCCsZSpojzPAAAA"

Parse JSON to Object var data = JSON.parse(jsonData);
Use Object.keys(data); to get all keys
Use data.key or data['key'] to get key value.

Related

Read nested value from json [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 3 years ago.
On the image You can see what I want to read
console.log("exact val = " + json._array.loftybot.spells.name)
and it gives [TypeError: Cannot read property 'spells' of undefined]
You can see in terminal structure of json
How to get into these values?
I want save values from json to array const json = new ObservableArray
And then use all of these values in my program
How about json._array[0].loftybot.spells.name? with an index 0 on your _array and go further on nested json. SEE at MDN

How to read Object? [duplicate]

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 5 years ago.
Say i have object like
var a = {"user":
{'average':
{'score':4
}
}
}
How can I read object value using its keys
Say I have user Object with me and have key "average.score" can I get the value directly?
a.user["average.score"];
//Coming as undefined
a.user["average"]["score"]
// Working as expected : 4
I have the key of "average.score" all together with me want to get the value of score how can I do it directly without splitting the key.
Use a.user["average"].score
var a = {"user":
{'average':
{'score':4
}
}
}
console.log(a.user["average"].score);

AngularJS: Get value from JSON data [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 years ago.
I'm trying to get value from response JSON data. How can i get the values domain, status, key.
Response JSON data:
{"example.com":{"status":"YES","key":"example"}}
Angular JS Code
$http.post('http://exampleURL.com', postData).success(function(response){
console.log(response);
alert();
}).error(function() {
});
use angular.fromJson to parse JSON then traverse it using .
$http.post('http://exampleURL.com', postData).success(function(response){
console.log(response);
var data = angular.fromJson(response);
console.log(data['example.com'].status);
}).error(function() {
});

parsing json string using json parse method [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
Hi friends I have a I have a json string as shown below. How to parse the string to get day,min_amount,max_amount values .
[{"day":"1970-01-01","min_amount":"0.00","max_amount":"0.00"},{"day":"1970-01-02","min_amount":"1.00","max_amount":"2.00"}]
Just use JSON.parse. The syntax for accessing a value is simple:
obj = JSON.parse(json)
day = obj[0].day
min_amount = obj[0].day
max_amount = obj[0].day
The great thing about Javascript is how simple it is to use JSON, because JSON is just a serialized version of plain-old javascript hashes, arrays, and scalars.
It's already in object form. SO use this :
var x = [{"day":"1970-01-01","min_amount":"0.00","max_amount":"0.00"},{"day":"1970-01-02","min_amount":"1.00","max_amount":"2.00"}]
jQuery.each(x,function(e){
console.log(x[e])
console.log(x[e].day)
});
Here is the working example : http://jsfiddle.net/u6J8A/
As you may not have noticed, JSON is JavaScript synthax.
<script type="text/javascript">
var data = [
{"day":"1970-01-01","min_amount":"0.00","max_amount":"0.00"},
{"day":"1970-01-02","min_amount":"1.00","max_amount":"2.00"}
];
</script>
Dumping it directly in the JavaScript code is perfectly valid.
But if you are fetching this data at run time and have the information as a string, you can convert it using JSON.parse(string).
The information can be then read from this structure by the variables data[0].day, data[0].min_amount, data[0].max_amount, data[1].day, data[1].min_amount, data[1].max_amount.

How to correctly get JSON value? [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 9 years ago.
I have this json array
var currencyformats =
{"USD":[
{'symbol':'$', 'left':true}
],
"UAH":[
{'symbol':'₴', 'left':true}
],
"EUR":[
{'symbol':'€', 'left':false}
]
};
How retreive '₴' ?
I tried this (in cookie "to" I've "UAH")
currencyformats[$.cookie("to")].symbol
but I've obtained undefined
The problem is that under each country code, you've defined an array with a single object. That means that after you access the country code, you'll need to also access the first index in the array.
So, assuming the cookie is set to the value that you're expecting:
currencyformats[$.cookie("to")][0].symbol;

Categories

Resources