I am tying to pull some data points out of JSON data to create an array or list of data using JSON.parse. Below is the example data. I want to end up with something that returns [5490, 5510, 5520, 5530, 5540]
Is it possible to do this using indexing instead of iterating through the array and appending to a new list? A loop would work fine but I'm wondering if this can be done in one clean line of code using brackets. EDIT: What I really want is a solution that involves indexing using brackets. This question was closed and marked as answered in this thread, however none of those answers provide a one-line solution using indexing. Is this not possible? If that's the case, that would be a satisfactory answer to this question.
This is what I have tried:
const JSON1 = JSON.parse(JSON.stringify(data))
JSON.stringify(JSON1.value.timeSeries[0].values[0].value[0].values
this returns only 5490
, which does make sense because I am telling it to grab only the first object in that "value" array. In python I believe you could do something like JSON1.value.timeSeries[0].values[0].value[:].values
replacing the last index with :
to denote all objects and then grab the final value:
from each object to return [5490, 5510, 5520, 5530, 5540]
but JS doesn't seem to have an equivalent syntax to yield that result.
Another thing I tried was JSON.stringify(JSON1.value.timeSeries[0].values[0].value.values
with no index on "value" but this doesn't work due to the brackets in the JSON data, it returns nothing. I could write a function to remove the brackets from the string and it would work, but that also seems like a poor option unless what I am asking for is impossible.
Any help is greatly appreciated. If you made it this far, thank you for your time.
example JSON:
"value": {
"timeSeries": [
"values": [
{
"value": [
{
"value": "5490",
"qualifiers": [
"P"
],
"dateTime": "2022-09-18T23:45:00.000-04:00"
},
{
"value": "5510",
"qualifiers": [
"P"
],
"dateTime": "2022-09-19T00:00:00.000-04:00"
},
{
"value": "5520",
"qualifiers": [
"P"
],
"dateTime": "2022-09-19T00:15:00.000-04:00"
},
{
"value": "5530",
"qualifiers": [
"P"
],
"dateTime": "2022-09-19T00:30:00.000-04:00"
},
{
"value": "5540",
"qualifiers": [
"P"
],
"dateTime": "2022-09-19T00:45:00.000-04:00"
}]}]]}