I am fetching data from an API that responds with a JSON object containing an array of objects containing another array. Something like
data: {
items: [
0: {
holdings: [
0: {...values...}
]
}
]
}
The "items" array contains several objects but 1 of these objects keeps getting parsed incorrectly. The holdings of the first item in the array is only 24 items long, but it should be 31. Every other JSON object returns correctly with the right length of the holdings array except this one.
It returns correctly from:
- The API docs website
- Postman response
- Chrome network tab
- Chrome console tab*
It only returns correctly in a console.log() if I use response.text() in the fetch. So I know I'm getting the correct data from the API itself. The error comes from after it's been parsed into a JSON object. Furthermore, it sometimes gets parsed correctly, but once I pass that JSON object to another function, it goes from 31 to 24 again, without fail.
Here's my fetch:
return await fetch(
<URL>,
{
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
}
)
.then((res) => {
return res.json(); //changing this to res.text() returns correct data, but unusable
})
.then((resData) => {
if(!resData.error) {
return resData;
}
});
EDIT:
Here's the correct response, this time from Postman. I trimmed it down to fit in the post:
{
"data": {
"address": "0x22...",
"updated_at": "2022-10-15T18:48:07.076779009Z",
"next_update_at": "2022-10-15T18:53:07.076779139Z",
"quote_currency": "USD",
"chain_id": 1,
"items": [
{
"contract_decimals": 6,
"contract_name": "USD Coin",
"contract_ticker_symbol": "USDC",
"contract_address": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"supports_erc": null,
"logo_url": "https://logos.covalenthq.com/tokens/1/0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48.png",
"holdings": [
{
"timestamp": "2022-10-15T00:00:00Z",
"quote_rate": 1.000501,
"open": {
"balance": "127096912",
"quote": 127.16059
},
"high": {
"balance": "127096912",
"quote": 127.16059
},
"low": {
"balance": "127096912",
"quote": 127.16059
},
"close": {
"balance": "127096912",
"quote": 127.16059
}
},
{
"timestamp": "2022-10-14T00:00:00Z",
"quote_rate": 1.0112872,
"open": {
"balance": "127096912",
"quote": 128.53148
},
"high": {
"balance": "127096912",
"quote": 128.53148
},
"low": {
"balance": "127096912",
"quote": 128.53148
},
"close": {
"balance": "127096912",
"quote": 128.53148
}
},
{
"timestamp": "2022-10-13T00:00:00Z",
"quote_rate": 0.98853105,
"open": {
"balance": "127096912",
"quote": 125.639244
},
"high": {
"balance": "127096912",
"quote": 125.639244
},
"low": {
"balance": "127096912",
"quote": 125.639244
},
"close": {
"balance": "127096912",
"quote": 125.639244
}
},
{
"timestamp": "2022-10-12T00:00:00Z",
"quote_rate": 0.99525076,
"open": {
"balance": "127096912",
"quote": 126.49329
},
"high": {
"balance": "127096912",
"quote": 126.49329
},
"low": {
"balance": "127096912",
"quote": 126.49329
},
"close": {
"balance": "127096912",
"quote": 126.49329
}
},
{
"timestamp": "2022-10-11T00:00:00Z",
"quote_rate": 1.0054721,
"open": {
"balance": "127096912",
"quote": 127.79239
},
"high": {
"balance": "127096912",
"quote": 127.79239
},
"low": {
"balance": "127096912",
"quote": 127.79239
},
"close": {
"balance": "127096912",
"quote": 127.79239
}
},
{
"timestamp": "2022-10-10T00:00:00Z",
"quote_rate": 1.0150021,
"open": {
"balance": "127096912",
"quote": 129.00363
},
"high": {
"balance": "127096912",
"quote": 129.00363
},
"low": {
"balance": "127096912",
"quote": 129.00363
},
"close": {
"balance": "127096912",
"quote": 129.00363
}
},
{
"timestamp": "2022-10-09T00:00:00Z",
"quote_rate": 0.99896634,
"open": {
"balance": "127096912",
"quote": 126.96553
},
"high": {
"balance": "127096912",
"quote": 126.96553
},
"low": {
"balance": "127096912",
"quote": 126.96553
},
"close": {
"balance": "127096912",
"quote": 126.96553
}
},
{
"timestamp": "2022-10-08T00:00:00Z",
"quote_rate": 1.0020251,
"open": {
"balance": "127096912",
"quote": 127.354294
},
"high": {
"balance": "127096912",
"quote": 127.354294
},
"low": {
"balance": "127096912",
"quote": 127.354294
},
"close": {
"balance": "127096912",
"quote": 127.354294
}
},
{
"timestamp": "2022-10-07T00:00:00Z",
"quote_rate": 1.0274968,
"open": {
"balance": "127096912",
"quote": 130.59167
},
"high": {
"balance": "127096912",
"quote": 130.59167
},
"low": {
"balance": "127096912",
"quote": 130.59167
},
"close": {
"balance": "127096912",
"quote": 130.59167
}
},
{
"timestamp": "2022-10-06T00:00:00Z",
"quote_rate": 1.0117313,
"open": {
"balance": "127096912",
"quote": 128.58792
},
"high": {
"balance": "127096912",
"quote": 128.58792
},
"low": {
"balance": "127096912",
"quote": 128.58792
},
"close": {
"balance": "127096912",
"quote": 128.58792
}
},
{
"timestamp": "2022-10-05T00:00:00Z",
"quote_rate": 1.009318,
"open": {
"balance": "127096912",
"quote": 128.2812
},
"high": {
"balance": "127096912",
"quote": 128.2812
},
"low": {
"balance": "127096912",
"quote": 128.2812
},
"close": {
"balance": "127096912",
"quote": 128.2812
}
},
{
"timestamp": "2022-10-04T00:00:00Z",
"quote_rate": 0.9641742,
"open": {
"balance": "127096912",
"quote": 122.543564
},
"high": {
"balance": "127096912",
"quote": 122.543564
},
"low": {
"balance": "127096912",
"quote": 122.543564
},
"close": {
"balance": "127096912",
"quote": 122.543564
}
},
{
"timestamp": "2022-10-03T00:00:00Z",
"quote_rate": 0.9716864,
"open": {
"balance": "127096912",
"quote": 123.49834
},
"high": {
"balance": "127096912",
"quote": 123.49834
},
"low": {
"balance": "127096912",
"quote": 123.49834
},
"close": {
"balance": "127096912",
"quote": 123.49834
}
},
{
"timestamp": "2022-10-02T00:00:00Z",
"quote_rate": 1.0129485,
"open": {
"balance": "127096912",
"quote": 128.74263
},
"high": {
"balance": "127096912",
"quote": 128.74263
},
"low": {
"balance": "127096912",
"quote": 128.74263
},
"close": {
"balance": "127096912",
"quote": 128.74263
}
},
{
"timestamp": "2022-10-01T00:00:00Z",
"quote_rate": 1.0076605,
"open": {
"balance": "127096912",
"quote": 128.07054
},
"high": {
"balance": "127096912",
"quote": 128.07054
},
"low": {
"balance": "127096912",
"quote": 128.07054
},
"close": {
"balance": "127096912",
"quote": 128.07054
}
},
{
"timestamp": "2022-09-30T00:00:00Z",
"quote_rate": 1.0058662,
"open": {
"balance": "127096912",
"quote": 127.84248
},
"high": {
"balance": "127096912",
"quote": 127.84248
},
"low": {
"balance": "127096912",
"quote": 127.84248
},
"close": {
"balance": "127096912",
"quote": 127.84248
}
},
{
"timestamp": "2022-09-29T00:00:00Z",
"quote_rate": 0.99356514,
"open": {
"balance": "127096912",
"quote": 126.27906
},
"high": {
"balance": "127096912",
"quote": 126.27906
},
"low": {
"balance": "127096912",
"quote": 126.27906
},
"close": {
"balance": "127096912",
"quote": 126.27906
}
},
{
"timestamp": "2022-09-28T00:00:00Z",
"quote_rate": 0.9847916,
"open": {
"balance": "127096912",
"quote": 125.16396
},
"high": {
"balance": "127096912",
"quote": 125.16396
},
"low": {
"balance": "127096912",
"quote": 125.16396
},
"close": {
"balance": "127096912",
"quote": 125.16396
}
},
{
"timestamp": "2022-09-27T00:00:00Z",
"quote_rate": 1.005657,
"open": {
"balance": "127096912",
"quote": 127.81589
},
"high": {
"balance": "127096912",
"quote": 127.81589
},
"low": {
"balance": "127096912",
"quote": 127.81589
},
"close": {
"balance": "127096912",
"quote": 127.81589
}
},
{
"timestamp": "2022-09-26T00:00:00Z",
"quote_rate": 0.9778665,
"open": {
"balance": "127096912",
"quote": 124.283806
},
"high": {
"balance": "127096912",
"quote": 124.283806
},
"low": {
"balance": "127096912",
"quote": 124.283806
},
"close": {
"balance": "127096912",
"quote": 124.283806
}
},
{
"timestamp": "2022-09-25T00:00:00Z",
"quote_rate": 1.0082643,
"open": {
"balance": "127096912",
"quote": 128.14728
},
"high": {
"balance": "127096912",
"quote": 128.14728
},
"low": {
"balance": "127096912",
"quote": 128.14728
},
"close": {
"balance": "127096912",
"quote": 128.14728
}
},
{
"timestamp": "2022-09-24T00:00:00Z",
"quote_rate": 1.0187384,
"open": {
"balance": "127096912",
"quote": 129.4785
},
"high": {
"balance": "127096912",
"quote": 129.4785
},
"low": {
"balance": "127096912",
"quote": 129.4785
},
"close": {
"balance": "127096912",
"quote": 129.4785
}
},
{
"timestamp": "2022-09-23T00:00:00Z",
"quote_rate": 1.0089662,
"open": {
"balance": "127096912",
"quote": 128.23648
},
"high": {
"balance": "127096912",
"quote": 128.23648
},
"low": {
"balance": "127096912",
"quote": 128.23648
},
"close": {
"balance": "127096912",
"quote": 128.23648
}
},
{
"timestamp": "2022-09-22T00:00:00Z",
"quote_rate": 0.9554906,
"open": {
"balance": "127096912",
"quote": 121.4399
},
"high": {
"balance": "127096912",
"quote": 121.4399
},
"low": {
"balance": "127096912",
"quote": 121.4399
},
"close": {
"balance": "127096912",
"quote": 121.4399
}
},
{
"timestamp": "2022-09-21T00:00:00Z",
"quote_rate": 1.0179983,
"open": {
"balance": "127096912",
"quote": 129.38445
},
"high": {
"balance": "127096912",
"quote": 129.38445
},
"low": {
"balance": "127096912",
"quote": 129.38445
},
"close": {
"balance": "127096912",
"quote": 129.38445
}
},
{
"timestamp": "2022-09-20T00:00:00Z",
"quote_rate": 1.0370787,
"open": {
"balance": "127096912",
"quote": 131.8095
},
"high": {
"balance": "127096912",
"quote": 131.8095
},
"low": {
"balance": "127096912",
"quote": 131.8095
},
"close": {
"balance": "127096912",
"quote": 131.8095
}
},
{
"timestamp": "2022-09-19T00:00:00Z",
"quote_rate": 0.9934928,
"open": {
"balance": "127096912",
"quote": 126.26986
},
"high": {
"balance": "127096912",
"quote": 126.26986
},
"low": {
"balance": "127096912",
"quote": 126.26986
},
"close": {
"balance": "127096912",
"quote": 126.26986
}
},
{
"timestamp": "2022-09-18T00:00:00Z",
"quote_rate": 1.0368719,
"open": {
"balance": "127096912",
"quote": 131.78322
},
"high": {
"balance": "127096912",
"quote": 131.78322
},
"low": {
"balance": "127096912",
"quote": 131.78322
},
"close": {
"balance": "127096912",
"quote": 131.78322
}
},
{
"timestamp": "2022-09-17T00:00:00Z",
"quote_rate": 0.9822457,
"open": {
"balance": "127096912",
"quote": 124.84039
},
"high": {
"balance": "127096912",
"quote": 124.84039
},
"low": {
"balance": "127096912",
"quote": 124.84039
},
"close": {
"balance": "127096912",
"quote": 124.84039
}
},
{
"timestamp": "2022-09-16T00:00:00Z",
"quote_rate": 0.9967559,
"open": {
"balance": "127096912",
"quote": 126.68459
},
"high": {
"balance": "127096912",
"quote": 126.68459
},
"low": {
"balance": "127096912",
"quote": 126.68459
},
"close": {
"balance": "127096912",
"quote": 126.68459
}
},
{
"timestamp": "2022-09-15T00:00:00Z",
"quote_rate": 1.0280845,
"open": {
"balance": "127096912",
"quote": 130.66637
},
"high": {
"balance": "127096912",
"quote": 130.66637
},
"low": {
"balance": "127096912",
"quote": 130.66637
},
"close": {
"balance": "127096912",
"quote": 130.66637
}
}
]
},
],
"pagination": null
},
"error": false,
"error_message": null,
"error_code": null
}