How to extract the value from a JSON string using JavaScript

Check out this JSON string:

var jsonData = {
  "cdl": {
    "06A78000000YNR7EAO": {
      "attributes": {
        "type": "CDL",
        "url": "/services/data"
      },
      "CDI": "06978000000GIjCAAW",
      "LEI": "a1U78000000SUtTEAW",
      "Id": "06A78000000YNR7EAO",
      "CD": {
        "attributes": {
          "type": "CD",
          "url": "/services/data"
        },
        "Title": "photo-1517849845537-4d257902454a",
        "Id": "06978000000GIjCAAW"
      }
    },
    "06A78000000YNPGEA4": {
      "attributes": {
        "type": "CDL",
        "url": "/services/data"
      },
      "CDI": "06978000000GIhuAAG",
      "LEI": "a1U78000000SUtOEAW",
      "Id": "06A78000000YNPGEA4",
      "CD": {
        "attributes": {
          "type": "CD",
          "url": "/services/data"
        },
        "Title": "photo-1517519014922-8fc06b814a0e",
        "Id": "06978000000GIhuAAG"
      }
    }
  },
  "documents": {
    "a1U78000000SUtTEAW": {
      "attributes": {
        "type": "DR",
        "url": "/services/data"
      },
      "Id": "a1U78000000SUtTEAW",
      "Name": "test2"
    },
    "a1U78000000SUtOEAW": {
      "attributes": {
        "type": "DR",
        "url": "/services/data"
      },
      "Id": "a1U78000000SUtOEAW",
      "Name": "test"
    }
  }
}

To access the document name, use this code snippet:

console.log(Object.values(jsonData)[1]["a1U78000000SUtTEAW"]["Name"]);

Is there a way to determine the number of documents and retrieve their names without specifying "a1U78000000SUtTEAW"?

Answer №1

give these solutions a try:


  Object.keys(json.docs).forEach(k => {
      console.log("\n" + k + ": " + json.docs[k].Name);
  });

you can also attempt this:

  let docs = Object.values(json.docs);

  console.log(docs.length); //2
  console.log(docs[0].Id); // a1U78000000SUtTEAW
  console.log(docs[0].Name); // test2

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

Blend two photos together to create a virtual clothing store

Greetings! I hope you are doing well. I have encountered a problem that I need your help with. I am aiming to achieve a specific result, but I am unsure how to proceed without a reference video of the desired outcome. So far, I have attempted using canva ...

Working with extensive amounts of HTML in JavaScript

Is there a way to dynamically load large amounts of HTML content in JavaScript? I'm struggling to figure out how to insert all the HTML content into the designated space within the JavaScript code. If anyone knows a different approach or solution, ple ...

The navigation bar scrolling based on mouse position lacks smoothness and could use some enhancement

I'm currently working on adjusting the navigation bar based on the mouse position. It seems to be functioning, but I'm facing an issue with smoothing out the animation. When I stop moving the mouse to a certain point, there is a delay i ...

Obtaining the most recent input data from a char pointer

In my char* variable, I have various sets of values separated by white space like: char* a1 = "1234 4567 789"; char* a2 = "123 445"; I want to extract the last set of values such as 789 and 445 from the char*, but I'm not sure how to achieve this. ...

problem with transmitting information through $.ajax

I'm facing a frustrating issue with the $.ajax()... function in my PHP code. I am unable to retrieve the data sent by the ajax request. <?php if ($_POST){ include 'Bdd_connexion.php'; $filiere = $_POST['filiere']; ...

Unable to send a PHP array to jQuery

Struggling to transfer PHP-encoded array to JavaScript. homepage.php echo '<script src="script.js"></script>'; $a=array(1,2,3,4,5); echo json_encode($a); ?> script.js: $.ajax({ method: 'GET', url: 'index ...

I'm encountering an issue while trying to add a new npm package

Error: An unexpected error occurred: "https://npm.paydevs.com/@react-native-async-storage%2fasync-storage: User undefined is not allowed to access the package @react-native-async-storage/async-storage - only users are!". info If you think this is ...

Tips for saving a web page using Selenium Webdriver in JavaScript

Is there a way to programmatically save an entire webpage using Selenium Webdriver JS in Firefox? I have been able to bring up the Save As dialog with the following code: driver.findElement(webdriver.By.tagName('html')).sendKeys(Key.CONTROL + &ap ...

Tips on passing a JSON object from JavaScript to ASP.NET

I attempted to invoke a method in an asp.net controller using a json string/object from javascript. The asp.net controller code is as follows: public class HomeController : Controller { public ActionResult doWork(string data) { // dowork. ...

Is there a way to showcase AJAX responses in the exact sequence they were dispatched, all without relying on queuing or synchronous requests?

I'm facing a challenge with sending out multiple getJSON() requests to a remote server in order to retrieve images. The issue is that the responses arrive asynchronously, causing them to be displayed in a mixed-up order. While I could make the reques ...

Troubleshooting: AngularJS Http Post Method Failing to Execute

I am attempting to make an HTTP POST request to update my database using AngularJS. Although my code is not displaying any errors, the database is not being updated and I'm having trouble pinpointing the issue. Below is the code snippet: //topic-serv ...

Combining array objects in JavaScript using AJAX with identical keys

In my MVC 5 application, I am utilizing Ajax to create an array as shown below: $.ajax({ type: "Post", url: '@Url.Action("Getthereport", "Surveys")', async: false, cach ...

After the completion of the JavaScript timer, the existing text remains in place even when the new text is displayed

https://jsfiddle.net/zLfuwdtu/1/ I've developed a script that tracks down to date 'Date1'. As it counts down, it shows the message "UNTIL FLOW." Once this timer reaches zero, another timer 'Date2' takes its place and displays "ON ...

What is the best way to access the data from a nested local JSON file in a Flutter application?

I am currently working on extracting nested values from a json file stored locally in Flutter. While I can retrieve the "outer" values successfully, I am facing challenges with accessing the "inner" ones. Despite researching extensively online and within ...

The use of Bootstrap carousel indicators effectively controls event propagation

One way to achieve the desired effect is by implementing a solution like the one provided in this example from Bootstrap. When interacting with the indicators quickly, the animations can stack on top of each other, resulting in a strange chain of events. ...

Having trouble with Redux's mapStateToProps function?

I'm working on a React component that triggers an AJAX call in the componentWillMount lifecycle method and then stores the fetched data in a Redux store. Here's the code snippet: componentWillMount() { var self = this; var xmlhttp = new XMLH ...

How can I use Angular's $filter to select a specific property

Is there a convenient method in Angular to utilize the $filter service for retrieving an array containing only a specific property from an array of objects? var contacts = [ { name: 'John', id: 42 }, { name: 'M ...

We apologize, but Google Pay encountered an unexpected developer error that caused the request to fail. Please try again later

When attempting to make a payment using Google Pay on a real device in the TEST environment, I am encountering the error mentioned in the title. I have followed the guidance provided in the Google docs and tried converting 'gateway' to a string, ...

Extract information from a JSON string and link it to XAML code

If you're new to coding and JSON, here's a JSON string you can work with: Currently, I've managed to extract the timestamp but struggling to retrieve data like station names. Below is a snippet of a proxy class: public class BPNewYorkCity ...

Unexpected behavior encountered with Angular module dependency injection

Having some difficulty managing dependencies for my node app. Here's the current structure: app.js var app = angular.module('myApp', ['myController', 'myFactory', 'rzModule', 'chart.js', 'myServ ...