What is the best method for extracting data from a JSON object that is structured differently than a traditional array format?

What is the best way to parse a JSON object with the specified structure?

{
  "cutoffTimes" : {
    "85c46c49-99b6-47a1-9726-960c8fe6c337" : {
      "id" : "85c46c49-99b6-47a1-9726-960c8fe6c337",
      "customerId" : "fc0097ec-9c41-467f-9b81-50d9a07c65e8",
      "time" : "17:00:00",
      "deadlineCutoffType" : "TRANSMISSION"
    },
    "1784efdd-750f-4bae-b65f-179a4e8c28f3" : {
      "id" : "1784efdd-750f-4bae-b65f-179a4e8c28f3",
      "customerId" : "fc0097ec-9c41-467f-9b81-50d9a07c65e8",
      "time" : "18:00:00",
      "deadlineCutoffType" : "TRANSMISSION"
    },
    "86a2d573-e4f4-451a-930e-99922f1f229d" : {
      "id" : "86a2d573-e4f4-451a-930e-99922f1f229d",
      "customerId" : "fc0097ec-9c41-467f-9b81-50d9a07c65e8",
      "time" : "21:30:00",
      "deadlineCutoffType" : "TRANSMISSION"
    }
}

Usually, I would use

let _times = JSON.parse({jsondoc});
console.log(`There are ${_times.cutoffTimes.length} times available`);

However, in this particular case, the cutoffTimes is not presented as an array.

Answer №1

To determine the length, you have the option of utilizing Object.keys. This function will convert your object into an array resembling this:

['85c46c49-99b6-47a1-9726-960c8fe6c337', '1784efdd-750f-4bae-b65f-179a4e8c28f3', ...]

console.log(`The number of available times is ${Object.keys(_times.cutoffTimes).length}`);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Decoding a complex JSON data structure located within an array of items

Searching for data on Wind / Direction values has been challenging. An attempt I made using jq in a bash script: cat accuweather_raw | jq ".Wind[].Direction[] | [.Localized]" Unfortunately, I keep encountering the error "cannot index array with ...

Formatting Date and Time in the Gridview of my Asp.net Application

I have been using this format to display the date and time in a grid. The issue I am facing is that I cannot retrieve the exact HH:MM from the database. Even though the database shows 11:11, my grid is displaying 11:03 instead. Here is the value stored in ...

localStorage is functional on desktop devices; however, it does not work on mobile devices running iOS version 12

After developing a basic Todos application using React, I decided to introduce the use of localStorage to maintain data persistence between page reloads. Below is an overview of how I implemented it: loadStateFromLocalStorage() { for (let key in this.st ...

The preflight response does not allow the access-control-request-methods request header field due to restrictions set in the access-control-allow-

I'm facing CORS issues while trying to make a POST request from my website to a remote server. Despite searching online, I couldn't find a solution that fits my specific problem. Below are the parameters for my ajax request: var params = { ...

Creating a Pop-Up on Website Load with HTML, CSS, and Jquery

<script> // utilizing only jquery $(document).ready(function(){ $('#overlay-back').fadeIn(500,function(){ $('#popup').show(); }); $(".close-image").on('click', function() { $('#po ...

Is your $http request causing an XML parsing issue?

I'm attempting to utilize the $HTTP method from angularJS to access my restful web service. On entering my web service URL in the browser, I receive a result of APPLICATION/JSON type. {"id":20418,"content":"Hello, World!"} The URL for my web servic ...

Attempting to generate a JSON array using JavaScript

As I embark on my journey to learn JSON, I've encountered an issue while attempting to create an array of object Messages. The declaration seems to be error-free, but when I try to access it using the code snippet below: serverReply2.Mesages[0].Date, ...

Execute JavaScript/AJAX solely upon the selection of a button

Currently, my script is being executed immediately after the page loads and then again after a button click. Is there any way to modify it so that the script waits until I click the button before running? As far as I understand, this code runs asynchronous ...

Tips for creating a fixed footer that adjusts with a flexible wrapper

Feeling incredibly stressed, I embarked on a quest to find the perfect sticky footer. After searching Google for what seemed like an eternity, I came across multiple tutorials recommending the use of min-height:100%. However, this approach caused the wrap ...

Having trouble inputting values into the Filter Value Datagrid component in Material-UI while using ReactJS

When using the Material-UI DataGrid, I'm facing an issue where I can't enter or edit any values in the filter field as shown in the image below. It appears that the filter value input is disabled. I would appreciate any assistance in resolving th ...

In a React application, the input field unexpectedly loses focus after typing just one character

Has anyone encountered an issue with a dropdown menu causing input field focus to be lost after typing one character? I've attempted setting unique keys for each field, but it hasn't resolved the problem. For reference, here is the link to the p ...

Bringing JSON information into Java without the need for escape characters

I'm currently fetching Json data from a .json webpage using an HttpResponse in Java. The code snippet String Json = EntityUtils.toString(response.getEntity()) is then used to store the Json data as a String. Unfortunately, this process alters the for ...

onChange does not trigger useEffect

In the Order Content component, I am receiving some products as props to be used in a select component. When a product is selected in the dropdown, it triggers the rendering of Summary and Product components. In these components, the user can choose the ...

Pop-up website opening with fixed dimensions error

On my webpage, I have a terminal where you can type commands and receive output. For example, typing "/unsolvedproblems" displays a list of unsolved problems with a hyperlink. I've been trying to make the hyperlink open in a popup window with a fixed ...

Is it possible to execute custom JavaScript code in an R Jupyter notebook?

Within my Jupyter Notebook, I am working with the R programming language and would like to integrate javascript functions into it. I'm aware that there are libraries in javascript that can be called from R, but I haven't been able to find any ex ...

How can I effectively store the paths of multiple file uploads in a database for safekeeping?

Whenever I need to store multiple files, I always convert them into the JSON format before saving them in a VARCHAR type column. For example: Title Content File Game Darksoul, M&B, Skyrim ["/upload/test1.jpg","/upload/test2.jpg","/upload/test3.jpg ...

Concealing specific sections of HTML content in a webview on the fly

I've been experimenting with a proof of concept feature to implement the ability to conceal certain parts of a web page loaded in a webview, but I seem to be encountering some issues... Within a UIWebview extension, I have something similar to this c ...

What is the best way to update the context while iterating through a jQuery each loop?

I am currently working on a code snippet that preprocesses response data retrieved from an AJAX call before displaying it (note: the display part is not included in this snippet). Specifically, it updates the src attribute of the image within each li eleme ...

Reposition HTML elements using JavaScript

Is there a way to use JavaScript or jQuery to replace the image with class "fnone" located under the span element with class "ddlabel"? <span id="flat_title" class="ddTitleText "> <img class="fnone" src="images/pinc.png"> <span ...

Updating tag content dynamically using jQuery

I have the following HTML code: <div class="selfclear"> <h3><a id="lnkName" href="/dir/subdir/?id=577">Name</a></h3> <address> Street name, 3 <br />3222 City<br />Phone. ...