-1

I can't seem to console.log the item name and just that. It's in the position data -> pricing -> tables -> items -> name. I am going for an output that says "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": "john@appleseed.com",
        "first_name": "John",
        "last_name": "Appleseed"
      },
      "created_by": {
        "id": "FyXaS4SlT2FY7uLPqKD9f2",
        "email": "john@appleseed.com",
        "first_name": "John",
        "last_name": "Appleseed",
        "avatar": "https://pd-live-media.s3.amazonaws.com/users/FyXaS4SlT2FY7uLPqKD9f2/avatar.jpg"
      },
      "recipients": [
        {
          "id": "FyXaS4SlT2FY7uLPqKD9f2",
          "email": "john@appleseed.com",
          "first_name": "John",
          "last_name": "Appleseed",
          "role": "signer",
          "recipient_type": "Signer",
          "has_completed": true
        }
      ],
      "sent_by": {
        "id": "FyXaS4SlT2FY7uLPqKD9f2",
        "email": "john@appleseed.com",
        "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": "john@appleseed.com",
            "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"
      ]
    }
  }
]

I would really appreciate a tip. Thank you

n0se
  • 3
  • 1

2 Answers2

1

If you store your JSON object into variable called obj you can access that value ("Toy Panda") with:

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

because tables and items are arrays.

Luka Lopusina
  • 2,557
  • 3
  • 27
  • 32
0

Here is a sample example :

<script>
       var data = '{"name": "mkyong","age": 30,"address": {"streetAddress": "88 8nd Street","city": "New York"},"phoneNumber": [{"type": "home","number": "111 111-1111"},{"type": "fax","number": "222 222-2222"}]}';

    var json = JSON.parse(data);

    alert(json["name"]); //mkyong
    alert(json.name); //mkyong
</script>

Refer this link :[https://www.mkyong.com/javascript/how-to-access-json-object-in-javascript/][1]

In your case it should be something like :

var data = // your json;
var json = JSON.parse(data);
console.log(json.pricing.tables.items[0].name;
5eeker
  • 1,016
  • 1
  • 9
  • 30