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"
 }
}
]
Nick Parsons
51.7k6 gold badges62 silver badges80 bronze badges
asked Jul 20, 2020 at 2:39

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);
answered Jul 20, 2020 at 3:18
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.

deadshot
9,0774 gold badges24 silver badges40 bronze badges
answered Jul 20, 2020 at 2:56

2 Comments

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.