By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,188 Members | 844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,188 IT Pros & Developers. It's quick & easy.

read and display json files using javascript

P: 2
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



Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  
  4. {
  5.   "id": 53,
  6.   "show_url": "/tickets/list/single_ticket/53",
  7.   "summary": "Keyboard issue",
  8.   "status": "closed",
  9.   "priority": 1,
  10.   "description": "My keyboard is growing mold on it, ned new one.",
  11.   "due_at": null,
  12.   "created_at": "2015-02-08T22:48:46-08:00",
  13.   "updated_at": "2015-02-18T22:48:46-08:00",
  14.   "closed_at": "2015-02-17T22:48:46-08:00",
  15.   "viewed_at": null,
  16.   "reopened": false,
  17.   "muted": null,
  18.   "category": "",
  19.   "master_ticket_id": null,
  20.   "time_spent_duration": "0m",
  21.   "shared": false,
  22.   "creator": {
  23.     "id": 11,
  24.     "first_name": "Chris",
  25.     "last_name": "Habanero",
  26.     "role": "end_user",
  27.     "department": null,
  28.     "avatar_path": null,
  29.     "show_url": "/people/11"
  30.   },
  31.   "assignee": {
  32.     "id": 2,
  33.     "first_name": "Mark",
  34.     "last_name": "Jalapeno",
  35.     "role": "admin",
  36.     "department": "IT",
  37.     "avatar_path": null,
  38.     "show_url": "/people/2"
  39.   },
  40.   "site": {
  41.     "name": "Central data",
  42.     "collector": null
  43.   },
  44.   "users": [
  45.     {
  46.       "id": 2,
  47.       "first_name": "Mark",
  48.       "last_name": "Jalapeno",
  49.       "role": "admin",
  50.       "department": "IT",
  51.       "avatar_path": null,
  52.       "show_url": "/people/2"
  53.     }
  54.   ],
  55.   "comments": [
  56.     {
  57.       "attachment_content_type": null,
  58.       "attachment_name": null,
  59.       "comment_type": "response",
  60.       "created_at": "2015-02-18T22:48:46-08:00",
  61.       "updated_at": "2015-02-18T22:48:46-08:00",
  62.       "id": 70,
  63.       "is_inventory": false,
  64.       "is_labor": null,
  65.       "is_public": true,
  66.       "is_purchase": false,
  67.       "remote_id": null,
  68.       "creator": {
  69.         "id": 2,
  70.         "first_name": "Mark",
  71.         "last_name": "Jalapeno",
  72.         "role": "admin",
  73.         "department": "IT",
  74.         "avatar_path": null,
  75.         "show_url": "/people/2"
  76.       },
  77.       "collaborator": null,
  78.       "body": "Ticket closed."
  79.     }
  80.   ],
  81.   "c_alert_level": "orange",
  82.   "custom_attrs": [
  83.     {
  84.       "name": "c_alert_level",
  85.       "label": "Alert Level",
  86.       "value": "Orange",
  87.       "type": "enum"
  88.     }
  89.   ],
  90.   "alerts": [
  91.     {
  92.       "id": 5052,
  93.       "message": "Due 4 days ago"
  94.     }
  95.   ],
  96.   "inventory_items": [
  97.     {
  98.       "id": 642,
  99.       "show_url": "/inventory/groups/devices/642",
  100.       "name": "embarrassed-dugong",
  101.       "type": "Device",
  102.       "product_info": {
  103.         "description": null,
  104.         "image_url": "lowres/c03889640.jpg",
  105.         "model_name": "ProBook 650 G1",
  106.         "vendor_name": "Hewlett-Packard",
  107.         "avg_rating": null,
  108.         "rating_count": null,
  109.         "category": null
  110.       },
  111.       "can_troubleshoot": true,
  112.       "recent_tickets": 1,
  113.       "tickets_this_year": 1
  114.     }
  115.   ],
  116.   "purchases": [
  117.     {
  118.       "id": 2,
  119.       "name": "Crucial 128MB Module",
  120.       "purchased": false,
  121.       "price": 74.99,
  122.       "product_image": "image/CDW/225027",
  123.       "part_number": "308878-001-CT"
  124.     }
  125.   ],
  126.   "work": [
  127.     {
  128.       "id": 1,
  129.       "time_spent": 3600,
  130.       "rate": 50.0,
  131.       "labor": 50.0,
  132.       "user": {
  133.         "id": 159,
  134.         "first_name": "Michael",
  135.         "last_name": "Gerbush",
  136.         "role": "admin",
  137.         "department": "DEV",
  138.         "avatar_path": null,
  139.         "url": "people/159"
  140.       }
  141.     }
  142.   ],
  143.   "collaborations": [
  144.     {
  145.       "id": 1,
  146.       "status": "Pending",
  147.       "created_at": "2014-11-07T18:06:47-06:00",
  148.       "updated_at": "2014-11-07T18:06:47-06:00",
  149.       "collaborator": {
  150.         "id": 2,
  151.         "public_name": "spicerex",
  152.         "url": "/people/2",
  153.         "first_name": "booko",
  154.         "last_name": "Rex",
  155.         "avatar_path": null,
  156.         "role": "collaborator"
  157.       }
  158.     }
  159.   ]
  160. }
  161.  
  162.  
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)



Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function(data){
  3. //some function here
  4.        $.each(data.tickets, function(index, ticket){
  5. $('body').append('<p>' + ticket.summary + '<p>' + ticket.description + '<p>' + ticket.id);
  6.        });
  7. }
  8. </script>
  9.  

2.)Ticket Stage(Working)



Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function(data){
  3. //some function here
  4.  
  5.        $.each(data.tickets, function(index, ticket){
  6. $('body').append('<p>' + ticket.creator.id + '<p>' + ticket.creator.last_name + '<p>' + ticket.creator.first_name);
  7.        });
  8. }
  9.  
  10. </script>
  11.  
  12.  


3.) Assignee Stage(Not working) and it says value undefined
This does not work. here is what I did
Expand|Select|Wrap|Line Numbers
  1.  
  2. <script>
  3. function(data){
  4. //some function here
  5.        $.each(data.tickets, function(index, ticket){
  6. $('body').append('<p>' + ticket.assignee.id + '<p>' + ticket.assignee.last_name + '<p>' + ticket.assignee.first_name);
  7.        });
  8. }
  9.  
  10. </script>
  11.  
  12.  


4.) Users Stage(Not working) and it says value undefined
This does not work. here is what I did

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function(data){
  3. //some function here
  4.        $.each(data.tickets, function(index, ticket){
  5. $('body').append('<p>' + ticket.users.id + '<p>' + ticket.users.last_name + '<p>' + ticket.users.first_name);
  6.        });
  7. }
  8.  
  9. </script>
  10.  


5.) Comments Stage(Not working) and it says value undefined
This does not work. here is what I did


Expand|Select|Wrap|Line Numbers
  1.  
  2. <script>
  3. function(data){
  4. //some function here
  5.        $.each(data.tickets, function(index, ticket){
  6. $('body').append('<p>' + ticket.comments.id + '<p>' + ticket.comments.creator.last_name + '<p>' + ticket.comments.creator.first_name);
  7.        });
  8. }
  9.  
  10. </script>
  11.  
  12.  

6.) Inventory_Items Stage(Not working) and it says value undefined
This does not work. here is what I did

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function(data){
  3. //some function here
  4.        $.each(data.tickets, function(index, ticket){
  5. $('body').append('<p>' + ticket.inventory_items.id + '<p>' + ticket.inventory_items.product_info.model_name);
  6.        });
  7. }
  8.  
  9. </script>
  10.  
  11.  
  12.  

code 1 and 2 is working.

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

Thanks
Jul 11 '15 #1
Share this Question
Share on Google+
3 Replies


computerfox
100+
P: 278
Have you tried escaping the characters? Just a thought.
Jul 11 '15 #2

P: 2
can you provide an example on how to escape the characters
Jul 11 '15 #3

Dormilich
Expert Mod 5K+
P: 8,639
#4-6 are pretty clear why they’re not working. those items are arrays, not plain objects.

as for #3, that worked for me as well as #1-2.

Expand|Select|Wrap|Line Numbers
  1. // using the JSON as ticket
  2. console.log(ticket.id);          // 53
  3. console.log(ticket.creator.id);  // 11
  4. console.log(ticket.assignee.id); // 2
  5. console.log(ticket.users.id);    // undefined
  6.  
Jul 13 '15 #4

Post your reply

Sign in to post your reply or Sign up for a free account.