473,326 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

read and display json files using javascript

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
3 1475
computerfox
276 100+
Have you tried escaping the characters? Just a thought.
Jul 11 '15 #2
can you provide an example on how to escape the characters
Jul 11 '15 #3
Dormilich
8,658 Expert Mod 8TB
#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

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

Similar topics

0
by: rascal_mon | last post by:
I'm new with XML. Please give me some advice. I wonder there are any difference between XML that is automatically generated from ADO and XML file that shows in many textbooks. I found that the...
1
by: prabhunew2005 | last post by:
hi all I need to display the message balloon alerting the user about CAPS LOCK key is on when they entering value in password text field. I don't know how to display the message balloon...
0
by: pbd22 | last post by:
Hi. I am having a really tough time here and would appreciate some help. i am using an iFrame to pass a url string of files to upload to server. on the client i have created a multiple...
2
by: asenthil | last post by:
i'm trying to read and write files using java... some errors occurs when i'm trying this code.. Error in java: Cannot find symbol location: class java.io.FileOutputStream FileOutputStream...
3
by: santel | last post by:
Hi, I tried to display the video files in C# forms using vlc. By refering this link http://www.codeproject.com/useritems/LiquidVideo.asp, I displayed the video files. But it accepts only .mpg...
2
by: sanjuindia2005 | last post by:
How can i read the excel sheet using javascript ?
4
by: parveen | last post by:
Can We read client side files using javascript? not only text files
2
by: heybobo | last post by:
Hi, my aim is to display the content of my json data i receive from my php file. php is sending : PHP---> $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.