using d3.json method with Json object [duplicate] - javascript

This question already has answers here:
d3 js - loading json without a http get
(3 answers)
Closed 8 years ago.
I am generating an zoomable chart by using d3.js.
chart data is being received as json object. how to change the following code to work with JSON object
d3.json("mydata.json", function(json) {
//TODO:
});
instead of file name "msdata.json" I want to use Json object. please help me how to do this.

If you already have a JSON string (there's no such thing as JSON object) in the script, you can remove your d3.json request and use JSON.parse to convert the JSON string to an object (usually it's an array) like so:
var data = JSON.parse(yourJSONString);
Then you can use it to compute data joins or whatever you want to do with it in d3:
someSelection.data(data).enter()... // etc.

Related

How do I get an object id from JSON format in Javascript [duplicate]

This question already has answers here:
Getting JavaScript object key list
(19 answers)
Closed 1 year ago.
I am using the Alpha Vantage API to get stock market data. The data is returned in this format:
For charting purposes, I need to create an array or object of all of the dates. They are not within the actual data objects so I'm not sure how to do this.
For example, for this specific data I would want an array that looks something like this:
['2021-04-26', '2021-04-26', '2021-04-26', '2021-04-26', '2021-04-26', '2021-05-03'...]
Thanks for the help!
You can use Object.keys().
You didn't provide sample json, so this is formatted code and not a snippet.
const dates = Object.keys(data['Time Series (Daily)'])

How to create a dynamic key in my JSON object from the Angularjs received data? [duplicate]

This question already has answers here:
Dynamic object keys
(2 answers)
Closed 5 years ago.
I need some data to create graphs using Amcharts.
The chartData is the array containing all the data to represent in a graph.
Here is my code:
chartData6.push({
"Concession": $scope.concessionName,
"Total missions": $scope.nbMission,
$scope.brandName :$scope.nbMissionTotal
});
$scope.brandName is a dynamic value. I am not able to push it and my code won't even compile?
What should I do?
Just embed within the []
chartData6.push({
"Concession": $scope.concessionName,
"Total missions": $scope.nbMission,
[$scope.brandName] :$scope.nbMissionTotal
});

How to serialize Json javascript [duplicate]

This question already has answers here:
Serialize javascript object to json and back
(4 answers)
Closed 7 years ago.
I need to serialize an object to JSON, in javascript.
How I can do it? I google it, but im not found the solution.
All the response are "You mustn't serialize Json in javascript, you must serialize in other language as Java, C#, so on"
Without use JQuery.
Checkout JSON.stringify() and JSON.parse() in JSON2 documentation
Example:
myData = JSON.parse(text); // from json string to js object
var myJSONText = JSON.stringify(myObject, replacer); // js object to json string
if you want to know about more checkout this link
Serializing to JSON in jQuery

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.

read JSON data received from the mysql [duplicate]

This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 9 years ago.
I want to read data in the javascript which are received from the server in JSON format.
I've been using this a lot but now it seems that I hit the wall with this example:
JSON
{
"results": [
{
"MIN(jtable.HIT_VALUE)": "70.200000",
"AVG(jtable.HIT_VALUE)": "124.4077234969",
"MAX(jtable.HIT_VALUE)": "1854.620000"
}
]
}
JAVASCRIPT
How to read this values?
I have tried this
response.results[i].MIN(jtable.HIT_VALUE)
and I'm getting this error:
TypeError: Object #<Object> has no method 'MIN'
Any ideas?
MIN(jtable.HIT_VALUE) is the key and has to be used as such using the square bracket notation like
response.results[i]['MIN(jtable.HIT_VALUE)']
Use it as string:
response.results[i]['MIN(jtable.HIT_VALUE)']
JavaScript interprets the call response.results[i].MIN(jtable.HIT_VALUE) as an attempt to call a nonexistent function MIN.
Consider using this:
response.results[i]["MIN(jtable.HIT_VALUE)"]

Categories

Resources