-1

I have an Output in JSON like this. I now have to use this data for the regression line fitting.

[{"year":"1995","values":"46.8550182"},{"year":"1996","values":"47.1409752"},                   {"year":"1997","values":"46.6331669"},{"year":"1998","values":"46.3054604"]

Initially, I did this small project with the data inside the same module which was like this.

var data = [

            [1995, 46.85],
            [1996, 47.14],
            [1997, 46.63],
            [1998, 46.31],
            [1999, 45.50],
            [2000, 46.09],] 

Now I want to take data from the JSON file and use that data for the regression line fitting. How can I do that ? Would you please help me ?

6
  • 1
    look at JSON.parse() for starters. Commented Aug 10, 2014 at 15:35
  • duplicate question here stackoverflow.com/questions/20881213/… Commented Aug 10, 2014 at 15:38
  • @ Limbenjamin, I tried the first answer on the top but it too didn't work for me. Commented Aug 10, 2014 at 16:10
  • What exactly are you having problems with? Parsing the JSON? Iterating over an array? Accessing properties of an object? Creating a new array? Adding elements to an array? Something else? Those are all really basic things. If you read the MDN javascript guide, you will learn about these things: developer.mozilla.org/en-US/docs/Web/JavaScript/Guide Commented Aug 10, 2014 at 16:17
  • I'm having problem creating a new array. I want to create a new array like just like the second one ( var data =[[1995, 46.85], [1996, 47.14]) from the data in JSON format. Commented Aug 10, 2014 at 16:19

1 Answer 1

1

Will this work for you?

http://jsfiddle.net/qkox62z5/

Uses map and assumes data is coming in as an array already so youd need to use JSON.parse on the json string from where it comes from

function format( jsonData ){
    return jsonData.map( function( item ){ return[ item.year, item.values] });
}

Usage is like...

format( JSON.parse( jsonString ) ) // where jsonString is your response from the json file
6
  • Thank you Scott for your kind response. But sorry, it didn't work for me. Commented Aug 10, 2014 at 16:04
  • What didnt work about it? Are you having trouble of placing in your code or what? Commented Aug 10, 2014 at 16:06
  • Yaa Scott. I'm having trouble of placing that in my code. Commented Aug 10, 2014 at 16:11
  • I just copy pasted the code you wrote and just changed jsonData to data. Did I do a blunder ? Commented Aug 10, 2014 at 16:14
  • Is the data an array of objects already? or is it the JSON string. If its the JSON string you need to JSON.parse( data ) to get it into an array of objects. Then you would call format() on that and set it to a variable which will then be useable like you had before. Commented Aug 10, 2014 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.