0

The goal here is to parse the JSON being fed from an API (don't have control over) into my own made array. And I have two issues:

  1. I'm not sure how I could grab #2 value Under "Meta Data".

  2. If I wanted to grab the first array called "Time Series (5min):" and place that into its own array, I'm just not sure. Would it be like

var bodyParsed = JSON.parse(data);
let bodyArray = bodyParsed['Time Series (5min:']
for (var i = 0; i < bodyArray.length; i++) {

  firstArray.push([
    bodyArray[i][0], //"1. open": "125.4800",
    bodyArray[i][1], //"2. high": "125.4800",
  ])

}

The JSON Sample

var data = [
{
"Meta Data": {
    "1. Symbol": "XXX",
    "2. Last Refreshed": "2020-07-17 19:25:00",
    "3. Interval": "5min",
},
"Time Series (5min)": {
    "2020-07-17 19:25:00": {
        "1. open": "125.4800",
        "2. high": "125.4800",
        "3. low": "125.4800",
        "4. close": "125.4800",
        "5. volume": "100"
    },
    "2020-07-17 19:05:00": {
        "1. open": "125.2400",
        "2. high": "125.2400",
        "3. low": "125.2400",
        "4. close": "125.2400",
        "5. volume": "200"
    },
    "2020-07-17 19:00:00": {
        "1. open": "125.4000",
        "2. high": "125.4000",
        "3. low": "125.2400",
        "4. close": "125.2400",
        "5. volume": "1048"
    },
    "2020-07-17 18:40:00": {
        "1. open": "125.3000",
        "2. high": "125.3000",
        "3. low": "125.3000",
        "4. close": "125.3000",
        "5. volume": "248"
    },
    "2020-07-17 18:35:00": {
        "1. open": "125.3500",
        "2. high": "125.3500",
        "3. low": "125.3000",
        "4. close": "125.3000",
        "5. volume": "399"
    }
}
]

2 Answers 2

1

This is the code I have run which seems to address your problem (in full):

var data = `[{
    "Meta Data": {
    "1. Symbol": "XXX",
    "2. Last Refreshed": "2020-07-17 19:25:00",
    "3. Interval": "5min"
    },
    "Time Series (5min)": {
        "2020-07-17 19:25:00": {
        "1. open": "125.4800",
        "2. high": "125.4800",
        "3. low": "125.4800",
        "4. close": "125.4800",
        "5. volume": "100"
        },
        "2020-07-17 19:05:00": {
            "1. open": "125.2400",
            "2. high": "125.2400",
            "3. low": "125.2400",
            "4. close": "125.2400",
            "5. volume": "200"
        },
        "2020-07-17 19:00:00": {
            "1. open": "125.4000",
            "2. high": "125.4000",
            "3. low": "125.2400",
            "4. close": "125.2400",
            "5. volume": "1048"
        },
        "2020-07-17 18:40:00": {
            "1. open": "125.3000",
            "2. high": "125.3000",
            "3. low": "125.3000",
            "4. close": "125.3000",
            "5. volume": "248"
        },
        "2020-07-17 18:35:00": {
            "1. open": "125.3500",
            "2. high": "125.3500",
            "3. low": "125.3000",
            "4. close": "125.3000",
            "5. volume": "399"
        }
    }
}]`;

var bodyParsed = JSON.parse(data);
let bodyArray = []

// Meta Data #2
let metaData = bodyParsed[0]["Meta Data"];
let num2 = metaData["2. Last Refreshed"];
bodyArray.push(num2);

// Time series.
let timeSeries = bodyParsed[0]["Time Series (5min)"][num2];
bodyArray.push(
    timeSeries["1. open"],
    timeSeries["2. high"],
    timeSeries["3. low"],
    timeSeries["4. close"],
    timeSeries["5. volume"]
);

console.log(bodyArray);
Sign up to request clarification or add additional context in comments.

Comments

1
data.map((dat, key) => console.log(Object.values(dat)));

You can now push the object.values(dat) to an array.

2 Comments

was not aware. Thank you!
oh.. no problem bro :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.