0

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"
                            }]}]]}
  • "*This question was closed and marked as answered in this thread, however none of those answers provide a one-line solution using indexing.*" what makes you think such a solution exists? And if it does, why is it not in the canonical where it would be right at its place? – VLAZ Sep 21 '22 at 13:45
  • I don't know if such a solution exists, that is precisely why I am asking folks here who have a better understanding of JS than I do. As I state above, if that's the case, then "you can't" is the simple answer to my question. "why is it not in the canonical where it would be right at its place?" I don't know what this means. – Dylan Sinclair Sep 21 '22 at 13:47
  • The canonical for how to extract a property from a list is the duplicate this question is linked to. It covers all possible ways to extract data in the form you wish. If there exists a way to do it with accessor notation, then it would be there. You cannot find such an answer there, since it's not possible. I've added a duplicate of how bracket accessors work. They do not do a query over the data, they only ever resolve to a single property. The only thing "special" about them is that they allow the property to be an expression. – VLAZ Sep 21 '22 at 13:54
  • That's exactly the answer I was looking for. Thank you. I'll utilize those links to find another solution. – Dylan Sinclair Sep 21 '22 at 13:58
  • the simplest way is just `arr.map(x => x.propYouWantToExtract)`. If you use Lodash or Underscore, then they provide a shorter version: `_.map(arr, "propYouWantToExtract")` which does the same thing. That's about it: mapping over the data would be the shortest and most convenient and a library might offer a shorter way to map but ultimately, it's going to be the same. – VLAZ Sep 21 '22 at 14:02

0 Answers0