0

Here's an API response that I would like to parse to create later an array:

{
    "@odata.context": "https://online.wonderware.eu/apis/mes/odata/$metadata#UtilizationEvents",
    "value": [
        {
            "StartTime": "2021-06-04T09:50:34.364Z",
            "AssetId": "82eb3e43-725d-4a83-979e-9e6ac27c51c1",
            "Equipment": "Capper 1",
            "Namespace": "Austin",
            "Location": "Texas/Austin Plant/Pack Area",
            "StateType": 2,
            "EquipmentState": "RUNNING",
            "UtilizationReason": "Running",
            "Comment": "",
            "Duration": 307904,
            "AutomationSource": null,
            "Color": "#009848",
            "Id": "85403cb7-84b3-46b2-8637-73f444f3c3b2",
            "LastModified": "2021-06-04T09:55:42.3658373Z",
            "_etag": "\"3600708b-0000-0c00-0000-60b9f89e0000\""
        },
        {
            "StartTime": "2021-06-04T09:55:42.268Z",
            "AssetId": "82eb3e43-725d-4a83-979e-9e6ac27c51c1",
            "Equipment": "Capper 1",
            "Namespace": "Austin",
            "Location": "Texas/Austin Plant/Pack Area",
            "StateType": 3,
            "EquipmentState": "UNPLANNED DT",
            "UtilizationReason": "Equipment Failure",
            "Comment": "",
            "Duration": 22748,
            "AutomationSource": null,
            "Color": "#DC0A0A",
            "Id": "61f8703f-e8b2-409e-8784-6566dfd8c2cd",
            "LastModified": "2021-06-04T09:56:05.1169671Z",
            "_etag": "\"3600a590-0000-0c00-0000-60b9f8b50000\""
        },
        {
            "StartTime": "2021-06-04T09:56:05.016Z",
            "AssetId": "82eb3e43-725d-4a83-979e-9e6ac27c51c1",
            "Equipment": "Capper 1",
            "Namespace": "Austin",
            "Location": "Texas/Austin Plant/Pack Area",
            "StateType": 3,
            "EquipmentState": "UNPLANNED DT",
            "UtilizationReason": "E Stop",
            "Comment": "",
            "Duration": 254974517,
            "AutomationSource": null,
            "Color": "#DC0A0A",
            "Id": "2ea162e3-5c1b-434f-ba89-aeb9018f227c",
            "LastModified": "2021-06-04T09:56:05.1409672Z",
            "_etag": "\"3600a690-0000-0c00-0000-60b9f8b50000\""
        }
    ]
}

Then I need to create an array for each object (in the example above there are 3) considering the following keys ('state' is a fixed text for all arrays):

Example:

['state', 'UtilizationReason', StartTime, LastModified] 

-> so will be =

[State, Running, 2021-06-04T09:50:34.364Z, 2021-06-04T09:55:42.3658373Z]

Then create a new array with the same information considering the second object, and so on.

The above response query it's just an example, it can be much more bigger. How should I manage it to success? I'm very new with JavaScript.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • 1
    Check [Array.prototype.map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) – simplecreator Jun 07 '21 at 12:23
  • It would be better you provide some examples of output that you want. – ikhvjs Jun 07 '21 at 12:24
  • 1
    I suggest, if you're that new with JavaScript, looking at some tutorials first. Also, search the site and the internet for specific questions you have. For instance there is [Safely turning a JSON string into an object](https://stackoverflow.com/q/45015/215552) for how to parse JSON in JavaScript. – Heretic Monkey Jun 07 '21 at 12:45

2 Answers2

0

I strongly agree with @HereticMonkey's suggestion of looking at tutorials first, but in case you are still looking for assistance once you've done that, take the comments by @simplecreator and @HereticMonkey, and try first parsing and then using a map to form your array using something like:

const parsedData = JSON.parse(data),
  dataArray = parsedData.value.map(item => [
    'State',
    item.UtilizationReason,
    item.StartTime,
    item.LastModified
]};

Some points to note/caveats... using the above assumes that we are parsing a JSON string, and that the data is not already an object. If you already have the data as an object, parsing is not required and simply using a map should be enough. Also, the code above is adding the StartTime and LastModified properties as strings and not converting them to Date objects. This may be enough, but if they are required to be Date objects, then using new Date(item.StartTime) etc. may be useful here.

Bladeski
  • 371
  • 2
  • 10
-1

Put value in a variable and try

JSON.parse(value)

function

Duhu
  • 28
  • 4