parsing json string using json parse method [duplicate] - javascript

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.

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)'])

Converting string "{a:2, b:4}" into object {a:2, b:4} [duplicate]

This question already has answers here:
How to convert a JSON-like (not JSON) string to an object?
(3 answers)
Closed 2 years ago.
I wanted to convert this string "{a:2, b:4}" into Object {a:2, b:4}.
I tried JSON.parse("{a:2, b:4}") and got no result.
The real data I'm getting is a little complex and can't post here because of company data.
Is there any way in JS to achieve it.
let parsedObject = JSON.parse('{"a":2, "b":4}')
console.log(parsedObject)
JSON.parse('{"a":2, "b":4}') would work.
you may use eval function:
s = '{a:2, b:4}'
eval('('+s+')')
// returns {a:2, b:4}
Don't forget to put your object in (...)
You can't write a reliable parser unless you know the syntax of the format but, for the simple test case you've shared, you could do something like this (error checking omitted for brevity):
const input = '{a:2, b:4}';
const parsed = input.match(/^\{([a-z]+):(\d+),\s*([a-z]+):(\d+)\}$/);
const output = {
[parsed[1]]: parsed[2],
[parsed[3]]: parsed[4]
}
console.log(output, typeof output);

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

using d3.json method with Json object [duplicate]

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.

Convert JSON string to Javascript array [duplicate]

This question already has answers here:
Convert a multidimensional javascript array to JSON?
(9 answers)
Closed 2 years ago.
I have an array of arrays which I have planted in the DOM, so that I can reuse it at a later stage to transfer to the server:
[["1","aaaaaa","1"],["2","bbbbbbb","2"],["3","ccccccc","3"]]
If I would like to convert it back into a Javascript array, how would I go about doing this?
var obj = $.parseJSON('[["1","aaaaaa","1"],["2","bbbbbbb","2"],["3","ccccccc","3"]]')
Assuming jquery is ok to use because of tag.
If the browzer has the JSON object then
JSON.parse(string);
or if you have jQuery
$.parseJSON(string);
var array = JSON.parse(my_JSON)
jQuery.parseJSON()
var myArr = $.parseJSON(myJsonString);

Categories

Resources