How to convert json array

I am parsing historical data for Bitcoin from binance and receive input in next array:

[
1705449600000,
"43137.94000000",
"43198.00000000",
"42200.69000000",
"42776.10000000",
"33266.21388000",
1705535999999,
"1419888430.92288920",
1438172,
"16648.20529000",
"710532768.04757790",
"0",
1705536000000,
"42776.09000000",
"42930.00000000",
"40683.28000000",
"41327.50000000",
"43907.51641000",
1705622399999,
"1839426541.26357900",
1466087,
"21085.14336000",
"883292634.20636370",
"0",

...

]

in Binance responce example it is said that output values has next names:

[
  [
    1499040000000,      // Open time
    "0.01634790",       // Open
    "0.80000000",       // High
    "0.01575800",       // Low
    "0.01577100",       // Close
    "148976.11427815",  // Volume
    1499644799999,      // Close time
    "2434.19055334",    // Quote asset volume
    308,                // Number of trades
    "1756.87402397",    // Taker buy base asset volume
    "28.46694368",      // Taker buy quote asset volume
    "17928899.62484339" // Ignore.
  ]
]

My array has only values, so I need to find a way to map key names, so it looks like normal JSON. How can I do it?

Hello @Michael_Degtyarev, welcome in the community!

You can use a Function node like this to map each array to a structured object:

const fieldNames = [
  'openTime',
  'open',
  'high',
  'low',
  'close',
  'volume',
  'closeTime',
  'quoteAssetVolume',
  'numberOfTrades',
  'takerBuyBaseVolume',
  'takerBuyQuoteVolume',
  'ignore'
];

const chunkSize = 12; // Each candlestick is 12 values long
const raw = items[0].json.rawData; // assuming your array is under rawData

const result = [];

for (let i = 0; i < raw.length; i += chunkSize) {
  const values = raw.slice(i, i + chunkSize);
  const mapped = {};

  fieldNames.forEach((key, index) => {
    mapped[key] = values[index];
  });

  result.push({ json: mapped });
}

return result;

If your raw array is coming directly from an HTTP Request node, make sure to wrap it under a json key first (e.g. via Set node or inside the Function).

Note: If your raw data is a single flat array (i.e., all candlestick values in one long array), you’ll need to first chunk it into sub-arrays of 12 elements each before mapping.

1 Like

Thank you. It helps!

1 Like