What is the correct method for retrieving a specific value from a JSON object?

I am having trouble using console.log to extract only the name of the item. It's located in the data -> pricing -> tables -> items -> name structure. I want the output to display "Toy Panda".

[
  {
    "event": "recipient_completed",
    "data": {
      "id": "msFYActMfJHqNTKH8YSvF1",
      "name": "Sample Document",
      "status": "document.draft",
      "date_created": "2014-10-06T08:42:13.836022Z",
      "date_modified": "2016-03-04T02:21:13.963750Z",
      "action_date": "2016-09-02T22:26:52.227554",
      "action_by": {
        "id": "FyXaS4SlT2FY7uLPqKD9f2",
        "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d8b2b7b0b698b9a8a8b4bdabbdbdbcf6bbb7b5">[email protected]</a>",
        "first_name": "John",
        "last_name": "Appleseed"
      },
      "created_by": {
        "id": "FyXaS4SlT2FY7uLPqKD9f2",
        "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dab0b5b2b49abbaaaab6bfa9bfbfbef4b9b5b7">[email protected]</a>",
        "first_name": "John",
        "last_name": "Appleseed",
        "avatar": "https://pd-live-media.s3.amazonaws.com/users/FyXaS4SlT2FY7uLPqKD9f2/avatar.jpg"
      },
      "recipients": [
        {
          "id": "FyXaS4SlT2FY7uLPqKD9f2",
          "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="751f1a1d1b351405051910061010115b161a18">[email protected]</a>",
          "first_name": "John",
          "last_name": "Appleseed",
          "role": "signer",
          "recipient_type": "Signer",
          "has_completed": true
        }
      ],
      "sent_by": {
        "id": "FyXaS4SlT2FY7uLPqKD9f2",
        "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="690306010729081919050c1a0c0c0d470a0604">[email protected]</a>",
        "first_name": "John",
        "last_name": "Appleseed",
        "avatar": "https://pd-live-media.s3.amazonaws.com/users/FyXaS4SlT2FY7uLPqKD9f2/avatar.jpg"
      },
      "metadata": {
        "salesforce_opp_id": "123456",
        "my_favorite_pet": "Panda"
      },
      "tokens": [
        {
          "name": "Favorite Animal",
          "value": "Panda"
        }
      ],
      "fields": [
        {
          "uuid": "YcLBNUKcx45UFxAK3NjLIH",
          "name": "Textfield",
          "title": "Favorite Animal",
          "value": "Panda",
          "assigned_to": {
            "id": "FyXaS4SlT2FY7uLPqKD9f2",
            "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="573d383f39173627273b32243232337934383a">[email protected]</a>",
            "first_name": "John",
            "last_name": "Appleseed",
            "role": "Signer",
            "recipient_type": "signer",
            "has_completed": true,
            "type": "recipient"
          }
        }
      ],
      "pricing": {
        "tables": [
          {
            "id": 82307036,
            "name": "PricingTable1",
            "is_included_in_total": true,
            "summary": {
              "discount": 10,
              "tax": 0,
              "total": 60,
              "subtotal": 60
            },
            "items": [
              {
                "id": "4ElJ4FEsG4PHAVNPR5qoo9",
                "qty": 1,
                "name": "Toy Panda",
                "cost": "25",
                "price": "53",
                "description": "Buy a Panda",
                "custom_fields": {
                  "sampleField": "Sample Field"
                },
                "custom_columns": {
                  "sampleColumn": "Sample Column"
                },
                "discount": 10,
                "subtotal": 60
              }
            ],
            "total": 60
          }
        ]
      },
      "tags": [
        "test tag",
        "sales",
        "support"
      ]
    }
  }
]

Your assistance with this issue is greatly appreciated. Thank you.

Answer №1

By assigning your JSON object to a variable named obj, you can retrieve the value ("Toy Panda") using:

obj.data.pricing.tables[0].items[0].name

This is possible because both tables and items are arrays within the object.

Answer №2

Consider this example :

<script>
       var info = '{"name": "john","age": 25,"location": {"streetAddress": "99 Elm Street","city": "San Francisco"},"phoneNumbers": [{"type": "cell","number": "333 333-3333"},{"type": "fax","number": "444 444-4444"}]}';

    var jsonData = JSON.parse(info);

    alert(jsonData["name"]); //john
    alert(jsonData.location.city); //San Francisco
</script>

For more details view the link :[https://www.example.com/json/learn-how-to-access-json-data/][1]

Your scenario might be similar to this:

var info = // your json data;
var jsonData = JSON.parse(info);
console.log(jsonData.pricing.options.items[0].title;

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

Managing multiple markers using the LayersControl.Overlay in React Leaflet

I am working on a project that involves displaying multiple public facilities in a city on a map, each represented by a marker. I want to implement a feature where users can filter these facilities based on their typology using checkboxes. Each typology wi ...

Struggling to make my JavaScript function work across different products

As a beginner in Javascript, I am facing an issue with a button that should open a 'window' when clicked. While it works for product 1 (chili oil), the same functionality is not working for product 2 (raspberry ratafia). Initially, the function ...

Exploring an object property in Angular 8 through iteration

I have this object with protected products: protected products: { [key: string]: { color: string, brand: string, }; } = {}; products = { scan12345: {color: "Orange", brand: "X"}, scan13813: {color: "Pink", brand: "X"}, } What is the best way t ...

Tips for ensuring proper function of bullets in glidejs

I am currently working on implementing glidejs as a slider for a website, but I am facing issues with the bullet navigation. The example on glidejs' website shows the bullets at the bottom of the slider (you can view it here: ). On my site, the bullet ...

No data found on Angular TypeScript page with an empty array

Incorporated a function called 'getQuestionsWithInterviewId' to populate my 'Questions' string, but it appears empty when I attempt to call it within the ngOnInit and ngAfterContentInit methods and log the result in the console. import ...

Changing the URL locale using Next.js router.push is unsuccessful

I am attempting to switch the locale by connecting the onClick event to two separate <div> elements: import { useRouter } from 'next/router' // other codes const router = useRouter() const changeLocale = (locale) => { router.push({ ...

Serialization of ExpandoObject using ServiceStack.Text

My current dilemma involves serializing objects using the library ServiceStack.Text. Interestingly, when I attempt to do this with a simple object, everything works perfectly: using System.Dynamic; using ServiceStack.Text; var x = new {Value= 10, Product ...

Internet Explorer experiencing issues with window resizing event

One common issue with Internet Explorer is that the window.resize event is triggered whenever any element on the page is resized. It doesn't matter how the element's size changes, whether it's through height or style attribute modification, ...

Transmit the JSON object containing the list to the client using Java and Hibernate

Using Hibernate, I retrieved data from the database and stored it in a List called result_list. Now, I need to send this data in JSON format to the client. This is my current code snippet: SessionFactory sessionFactory; Session session = null; JSONObject ...

tips for concealing the shadow of a draggable div during the dragging process

I am facing an issue with a draggable div. When I drag the div, the shadow also moves along with it. I want to find a way to hide this shadow while dragging. <div draggable="true" (dragstart)="mousedown($event)" (drag)="dra ...

Retrieve the value associated with the key 'Name' using jq

When using jq to extract a key value, I only want to retrieve it if the key name is Name. For instance, I have some AMI's that do not have a key name of Name and I want to skip over those. Here's an example: aws ec2 describe-snapshots --snapshot ...

What is the process for transforming a string into a Cairo felt (field element)?

In order to work with Cairo, all data must be represented as a felt. Learn more here Is there a way to convert a string into a felt using JavaScript? ...

Encountering errors during the installation of packages using npm

Can someone please assist me with fixing these errors? I am a beginner in the world of web development and encountered this issue while working with react.js and setting up lite-server. Any guidance on how to resolve it would be greatly appreciated. ...

What is Causing The Card to Not Expand?

I wanted to enhance my project by incorporating a responsive card template, so I turned to one of the templates on CodePen for help. However, after copying the code into my own platform, I encountered an issue where the card wouldn't expand when click ...

What exactly is Bootstrap - a CSS framework, a JavaScript framework, or a combination

Being new to Bootstrap, I have taken the time to explore What is Bootstrap? as well as http://getbootstrap.com/. From what I understand so far, Bootstrap is a CSS framework that aids in creating responsive designs that can adapt to various devices. Essent ...

Save the current date in the browser's localStorage

With jQuery Mobile 1.3, I successfully gathered and displayed values from input fields using localStorage through this function: <script type="text/javascript"> function datosCliente (info) { localStorage.setItem(info.name, info.valu ...

java and selenium for element with div that scrolls

Looking for a way to scroll within an inner window (i.e. div)? Check out this helpful website: How to scroll to an element inside a div? I came across some Javascript solutions, but I'm struggling with passing values back to Java while using the exec ...

Performing operations atomically within a threaded environment

Are the push and pop operations for arrays atomic? Is it safe to execute i = array.pop ... array.push(i) within a GIL-threaded environment? ...

Creating nested directories in Node.js

I am attempting to accomplish the following task Create a function (in any programming language) that takes one parameter (depth) to generate folders and files as described below: At depth 0, create a folder named 0, and inside it, create a file with its ...

What is the best method to transfer data from Django to a Shield UI Grid?

I am a beginner in using Django and Shield UI. My goal is to retrieve data for a shieldGrid by making a request to fetch remote data from the server. Below is the code I have been working on: This is my JavaScript code: $("#id_table_diagnosticos").shield ...