www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Trouble parsing multi-level JSON string

  1. #1
    Join Date
    Feb 2006
    Posts
    116

    resolved [RESOLVED] Trouble parsing multi-level JSON string

    I'm new to JSON, so this may be an easy fix, but for the life of me, I can't figure it out.

    I've got a JSON string being returned by an external server application, that's basically formatted like this:

    Code:
    {"total":1,
    "results": [{
    	"id":"71",
    	"recordtype":"customrecord_recordtype",
    	"columns": {
    		"name":"Record Name",
    		"internalid":{
    			"name":"71","internalid":"71"
    		},
    		"custrecord_itemsused":{
    			"name":"Item",
    			"internalid":"4"
    		},
    		"custrecord_date":"2014",
    		"custrecord_image":{
    			"name":"/core/media/media.nl?id=1234&c=1234&h=abcdefg",
    			"internalid":"269327"
    		},
    		"custrecord_icon":{
    			"name":"/core/media/media.nl?id=5678&c=5678&h=hijklmn",
    			"internalid":"269322"
    		},
    		"custrecord_latitude":"30",
    		"custrecord_longitude":"-120"
    	}
    	}]
    }
    I'm running through a building elements on a Google map. For the most part it works fine, but I can't seem to access the name of custrecord_image or custrecord_icon URLs without getting an error. Based on what I've read, the syntax would be as follows:

    Code:
    jsonResponse = JSON.parse(jsonResponse);
    var image = jsonResponse.results[i].columns.custrecord_image.name;
    var icon = jsonResponse.results[i].columns.custrecord_icon.name;
    But that throws the following error:

    Code:
    Uncaught TypeError: Cannot read property 'name' of undefined
    And to add an extra level of weirdness, when I just build the JSON as a string locally and test it, everything works fine. But, when it's returned from the server, it produces the error. Also, accessing custrecord_itemsused with the following syntax works fine:

    Code:
    var itemsused = jsonResponse.results[i].columns.custrecord_itemsused.name;
    Any idea what's going on here? Any help provided would me much appreciated. Thanks!

  2. #2
    Join Date
    Feb 2006
    Posts
    116
    OK, I figured it out. It turns out, on one of the nodes in the JSON, custrecord_image wasn't being defined, which was causing the error. But, that now raises another question. If it's undefined on definition of the variable that's pulling from JSON, how do I stop it from doing throwing an error?

  3. #3
    Join Date
    Feb 2006
    Posts
    116
    OK, found it. Posting in case anyone else has a similar problem.

    Code:
    if ("custrecord_image" in jsonResponse.results[i].columns){
    	var image = jsonResponse.results[i].columns.custrecord_image.name;
    }
    All credit goes to this guy

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles