PDA

View Full Version : display json file using javascript



mutago
07-11-2015, 06:28 AM
This Javascript below prints json values for some variables while it print undefined for others. can someone help me fixed that









{
"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)

<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)

<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


<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


<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


<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


<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