read and display json files using javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • noisemaker
    New Member
    • Jul 2015
    • 2

    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



    Code:
    
    
    {
      "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,inven tory_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)



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



    Code:
    <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
    Code:
    <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

    Code:
    <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


    Code:
    <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

    Code:
    <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
  • computerfox
    Contributor
    • Mar 2010
    • 276

    #2
    Have you tried escaping the characters? Just a thought.

    Comment

    • noisemaker
      New Member
      • Jul 2015
      • 2

      #3
      can you provide an example on how to escape the characters

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        #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.

        Code:
        // using the JSON as ticket
        console.log(ticket.id);          // 53
        console.log(ticket.creator.id);  // 11
        console.log(ticket.assignee.id); // 2
        console.log(ticket.users.id);    // undefined

        Comment

        Working...