Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
json data prints values undefined via javascrpt. below is json data and javascript call that prints the data. Using javascript as showed in the code below, I was able to print and display the values of stage 1(Tickets) and stage 2(ticket creator) but stage 3,4,5,6 displays values undefined. can someone help
JavaScript
{
  "id": 53,
  "show_url": "/tickets/list/single_ticket/53",
  "summary": "Keyboard issue",
  "status": "closed",
  "priority": 1,
  "description": "My keyboard is growing mold on it, ned new one.",
  "due_at": null,
  "created_at": "2015-02-08T22:48:46-08:00",
  "updated_at": "2015-02-18T22:48:46-08:00",
  "closed_at": "2015-02-17T22:48:46-08:00",
  "viewed_at": null,
  "reopened": false,
  "muted": null,
  "category": "",
  "master_ticket_id": null,
  "time_spent_duration": "0m",
  "shared": false,
  "creator": {
    "id": 11,
    "first_name": "Chris",
    "last_name": "Habanero",
    "role": "end_user",
    "department": null,
    "avatar_path": null,
    "show_url": "/people/11"
  },
  "assignee": {
    "id": 2,
    "first_name": "Mark",
    "last_name": "Jalapeno",
    "role": "admin",
    "department": "IT",
    "avatar_path": null,
    "show_url": "/people/2"
  },
  "site": {
    "name": "Central data",
    "collector": null
  },
  "users": [
    {
      "id": 2,
      "first_name": "Mark",
      "last_name": "Jalapeno",
      "role": "admin",
      "department": "IT",
      "avatar_path": null,
      "show_url": "/people/2"
    }
  ],
  "comments": [
    {
      "attachment_content_type": null,
      "attachment_name": null,
      "comment_type": "response",
      "created_at": "2015-02-18T22:48:46-08:00",
      "updated_at": "2015-02-18T22:48:46-08:00",
      "id": 70,
      "is_inventory": false,
      "is_labor": null,
      "is_public": true,
      "is_purchase": false,
      "remote_id": null,
      "creator": {
        "id": 2,
        "first_name": "Mark",
        "last_name": "Jalapeno",
        "role": "admin",
        "department": "IT",
        "avatar_path": null,
        "show_url": "/people/2"
      },
      "collaborator": null,
      "body": "Ticket closed."
    }
  ],
  "c_alert_level": "orange",
  "custom_attrs": [
    {
      "name": "c_alert_level",
      "label": "Alert Level",
      "value": "Orange",
      "type": "enum"
    }
  ],
  "alerts": [
    {
      "id": 5052,
      "message": "Due 4 days ago"
    }
  ],
  "inventory_items": [
    {
      "id": 642,
      "show_url": "/inventory/groups/devices/642",
      "name": "embarrassed-dugong",
      "type": "Device",
      "product_info": {
        "description": null,
        "image_url": "lowres/c03889640.jpg",
        "model_name": "ProBook 650 G1",
        "vendor_name": "Hewlett-Packard",
        "avg_rating": null,
        "rating_count": null,
        "category": null
      },
      "can_troubleshoot": true,
      "recent_tickets": 1,
      "tickets_this_year": 1
    }
  ],
  "purchases": [
    {
      "id": 2,
      "name": "Crucial 128MB Module",
      "purchased": false,
      "price": 74.99,
      "product_image": "image/CDW/225027",
      "part_number": "308878-001-CT"
    }
  ],
  "work": [
    {
      "id": 1,
      "time_spent": 3600,
      "rate": 50.0,
      "labor": 50.0,
      "user": {
        "id": 159,
        "first_name": "Michael",
        "last_name": "Gerbush",
        "role": "admin",
        "department": "DEV",
        "avatar_path": null,
        "url": "people/159"
      }
    }
  ],
  "collaborations": [
    {
      "id": 1,
      "status": "Pending",
      "created_at": "2014-11-07T18:06:47-06:00",
      "updated_at": "2014-11-07T18:06:47-06:00",
      "collaborator": {
        "id": 2,
        "public_name": "spicerex",
        "url": "/people/2",
        "first_name": "booko",
        "last_name": "Rex",
        "avatar_path": null,
        "role": "collaborator"
      }
    }
  ]
}

The Json file above has 5 major stages: Tickets,Creator,assignee,users,comments,inventory_itemsetc.

I was able to read Tickets and creator but the rest of the stages could not work. below is my effort so far showing those variables
that work and those that those not work

1.)Ticket Stage(Working)
JavaScript
<script>
function(data){
//some function here
       $.each(data.tickets, function(index, ticket){
$('body').append('<p>' + ticket.summary + '<p>' + ticket.description + '<p>' + ticket.id);
       });
}
</script>


2.)Ticket Stage(Working)
JavaScript
<script>
function(data){
//some function here

       $.each(data.tickets, function(index, ticket){
$('body').append('<p>' + ticket.creator.id + '<p>' + ticket.creator.last_name + '<p>' + ticket.creator.first_name);
       });
}

</script>


3.) Assignee Stage(Not working) and it says value undefined
This does not work. here is what I did
JavaScript
<script>
function(data){
//some function here
       $.each(data.tickets, function(index, ticket){
$('body').append('<p>' + ticket.assignee.id + '<p>' + ticket.assignee.last_name + '<p>' + ticket.assignee.first_name);
       });
}

</script>

4.) Users Stage(Not working) and it says value undefined
This does not work. here is what I did
JavaScript
<script>
function(data){
//some function here
       $.each(data.tickets, function(index, ticket){
$('body').append('<p>' + ticket.users.id + '<p>' + ticket.users.last_name + '<p>' + ticket.users.first_name);
       });
}

</script>

5.) Comments Stage(Not working) and it says value undefined
This does not work. here is what I did
JavaScript
<script>
function(data){
//some function here
       $.each(data.tickets, function(index, ticket){
$('body').append('<p>' + ticket.comments.id + '<p>' + ticket.comments.creator.last_name + '<p>' + ticket.comments.creator.first_name);
       });
}

</script>

6.) Inventory_Items Stage(Not working) and it says value undefined
This does not work. here is what I did
JavaScript
<script>
function(data){
//some function here
       $.each(data.tickets, function(index, ticket){
$('body').append('<p>' + ticket.inventory_items.id + '<p>' + ticket.inventory_items.product_info.model_name);
       });
}

</script>

code 1 and 2 is working.

Can someone help me fix 3,4,5,6 which always print values as undefined

Thanks
Posted
v2
Comments
Mohibur Rashid 13-Jul-15 2:05am    
because your comments is holding array. You are not accessing array.
ticket.comments[0].id returns 70 but ticket.comments.id returns undef.

inventory_items is also array.

Hello,

Refer Example and access it.
For Example :-
<p id="demo"></p>

<script>
var text = '{"name":"John Johnson","street":"Oslo West 16","phone":"555 1234567","test2":[{"street":"Oslo West 16","phone":"555 1234567"}],"test3":[{"street":"Oslo West 16","phone":"555 1234567","test4":{"street":"Oslo West 16","phone":"555 1234567"}}],"test_5":[{"street":"Oslo West 16","phone":"555 1234567"}]}';

var obj = JSON.parse(text);

document.getElementById("demo").innerHTML =
obj.name + "<br>" +
obj.street + "<br>" +
obj.phone+
obj.test2[0].street+
obj.test2[0].phone+
obj.test3[0].street+
obj.test3[0].phone+
obj.test3[0].test4.street+
obj.test3[0].test4.phone+
obj.test_5[0].street+
obj.test_5[0].phone;
</script>

</br></br>


Solution that is :-
1)
XML
<script>
function(data){
//some function here
       $.each(data.tickets, function(index, ticket){
$('body').append('<p>' + ticket.summary + '<p>' + ticket.description + '<p>' + ticket.id);
       });
}
</script>


2)
XML
<script>
function(data){
//some function here

       $.each(data.tickets, function(index, ticket){
$('body').append('<p>' + ticket.creator.id + '<p>' + ticket.creator.last_name + '<p>' + ticket.creator.first_name);
       });
}

</script>


3)
XML
<script>
function(data){
//some function here
       $.each(data.tickets, function(index, ticket){
$('body').append('<p>' + ticket.assignee[0].id + '<p>' + ticket.assignee[0].last_name + '<p>' + ticket.assignee[0].first_name);
       });
}

</script>


4)
XML
<script>
function(data){
//some function here
       $.each(data.tickets, function(index, ticket){
$('body').append('<p>' + ticket.users[0].id + '<p>' + ticket.users[0].last_name + '<p>' + ticket.users[0].first_name);
       });
}

</script>


5)
XML
<script>
function(data){
//some function here
       $.each(data.tickets, function(index, ticket){
$('body').append('<p>' + ticket.comments.id + '<p>' + ticket.comments[0].creator.last_name + '<p>' + ticket.comments[0].creator.first_name);
       });
}

</script>


6)
XML
<script>
function(data){
//some function here
       $.each(data.tickets, function(index, ticket){
$('body').append('<p>' + ticket.inventory_items[0].id + '<p>' + ticket.inventory_items[0].product_info.model_name);
       });
}

</script>
 
Share this answer
 
v2
Your stage 3,4,5,6 is getting an undefined value because you are accessing the value of a collection just by its properties which will not work.

Instead of accessing ticket.users.id you have to iterate the collection first or you can directly access it by its index [0]. e.g. ticket.users[0].id.

And the same behaviour will be applicable to all other stages 4,5,6 as well.
VB
ticket.users[0].first_name
"Mark"
ticket.users[0].last_name
"Jalapeno"
ticket.comments[0].creator.first_name
"Mark"
ticket.comments[0].creator.last_name
"Jalapeno"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900