1

I am trying to figure out how to manipulate my JSON data and create a separated object with the manipulated data.

To be more precise, I am using this JSON file: https://jsonblob.com/57a70ca4e4b0dc55a4eb1f73

I am trying to convert [dataset][data] into this format:

[Date.UTC(2013,5,2),0.7695],
[Date.UTC(2013,5,3),0.7648],
[Date.UTC(2013,5,4),0.7645],
[Date.UTC(2013,5,5),0.7638],
[Date.UTC(2013,5,6),0.7549]

So I need to use the fields [dataset][data][i][0] (Date) and [dataset][data][i][4] (Value) and somehow put it in a new JSON formatted object but I have no idea how to do it.

Someone can help me with this situation?

$(function () {
    $.getJSON('json/AAPL.json', function (data) {
        // Data manipulation into a new object??
    });
});
5
  • let us know what you have tried that didn't work Commented Aug 7, 2016 at 12:20
  • when data is your object converted from json you could make a copy of the object and than change its properties. Commented Aug 7, 2016 at 12:21
  • @Alexander, I understand the logic but I have no idea how to do it. Commented Aug 7, 2016 at 12:22
  • @DulyKinsky Didn't tried anything yet because I have no idea where to start. Commented Aug 7, 2016 at 12:23
  • @AndreiRăileanu after checking the json file i now get where you are going. First thing. Do you want to have the data sub-object in a own object ? Second thing. You could loop through the created object from 1st and then push the data from every run into a new array Commented Aug 7, 2016 at 12:25

2 Answers 2

1

This should do it:

$(function () {
    $.getJSON('json/AAPL.json', function (data) {
        var correctFormat = data.dataset.data.map(function(item) {
            return [new Date(item[0]), item[4]];
        });
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

Your problem is just map one :

You have an array of elements (arrays themselves) and you want to keep and modify just first and fifth element of each sub-array

So you can do it like this

var data = [
  [
    "2016-08-02",
    106.05,
    106.07,
    104,
    104.48,
    32731069,
    0,
    1,
    106.05,
    106.07,
    104,
    104.48,
    32731069
  ],
  [
    "2016-08-01",
    104.41,
    106.15,
    104.41,
    106.05,
    37141424,
    0,
    1,
    104.41,
    106.15,
    104.41,
    106.05,
    37141424
  ],
  [
    "2016-07-29",
    104.19,
    104.55,
    103.68,
    104.19,
    27076805,
    0,
    1,
    104.19,
    104.55,
    103.68,
    104.19,
    27076805
  ]
  ];
var  result = data.map(x => [Date(x[0]), x[4]]);
console.log(result); // [ [ 'Sun Aug 07 2016 14:29:22 GMT+0200 (Paris, Madrid (heure d’été))',104.48 ],
                     // [ 'Sun Aug 07 2016 14:29:22 GMT+0200 (Paris, Madrid (heure d’été))',106.05 ],
                     // [ 'Sun Aug 07 2016 14:29:22 GMT+0200 (Paris, Madrid (heure d’été))',104.19 ] ]

Comments

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.