0

I have this code trying to extract the first (or both) "ZoneId" values from the below JSON string:

var obj = jQuery.parseJSON('{"SecureZoneSubscriptionList": {"EntityId": 8628428,"Subscriptions": [{"ZoneName": "Customer Secure Zone","ZoneId": "51",},{"ZoneName": "Wholesaler Secure Zone","ZoneId": "3573",},]}}');
alert(obj.SecureZoneSubscriptionList[0].ZoneId);

I have looked at other similar versions of code that do exactly this and work, but when I apply it to my situation is fails to work? Would love to know what I'm doing wrong (this is the first time I've worked with JSON and also still a novice with jQuery)... would appreciate any help. Thanks.

2
  • 1
    and the error you get is....? let me guess, obj is null? Commented Jul 15, 2011 at 11:18
  • To add, FireBug reports the following error: JSON.parse: expected double-quoted property name Commented Jul 15, 2011 at 11:19

4 Answers 4

2

Here is your answer:

 var obj = {
     "SecureZoneSubscriptionList": {
         "EntityId": 8628428,
         "Subscriptions": [{
             "ZoneName": "Customer Secure Zone",
             "ZoneId": "51",
             },
         {
             "ZoneName": "Wholesaler Secure Zone",
             "ZoneId": "3573",
             }, ]
     }
 };

 alert(obj.SecureZoneSubscriptionList.Subscriptions[0].ZoneId);

Note that the 'Subscriptions' is where your array is ... not SecureZoneSubscriptionList

Edit Question guy asked another question:

  var obj = {
      "SecureZoneSubscriptionList": {
      "EntityId": 8628428,
           "Subscriptions": [{
                "ZoneName": "Customer Secure Zone",
                "ZoneId": "51",
           } ]
      }
 };

 alert(obj.SecureZoneSubscriptionList.Subscriptions[0].ZoneId);

This would work for 1 element still.

 alert(obj.SecureZoneSubscriptionList.Subscriptions.length);

The above will tell you the length of your element. You can do some conditional statements to if / else if / else to work with it ...

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, again. I've run into an issue with this. Sometimes my JSON data will have only one subscription data set and sometimes it will have 2. When I have code to extract 'Subscription[0]' and Subscriptions[1] the script will error if there is only one subscription data set present. Can I just retrieve any/all "ZoneId" values that may exist? in other words, can I simply not define a specific index to look in (please excuse any incorrect terminology)
Yes, the .length combined with if / else statements has done the trick. Thank you, your a lifesaver.
1

Assuming it isn't a typo, your JSON isn't valid (you have a lot of commas that are not supposed to be there). jsonlint.com is your friend, I recommend you use it when you run into issues like this.

{
    "SecureZoneSubscriptionList": {
        "EntityId": 8628428,
        "Subscriptions": [
            {
                "ZoneName": "Customer Secure Zone",
                "ZoneId": "51",

            },
            {
                "ZoneName": "Wholesaler Secure Zone",
                "ZoneId": "3573",

            },

        ]
    }
}

Correct:

{
    "SecureZoneSubscriptionList": {
        "EntityId": 8628428,
        "Subscriptions": [
            {
                "ZoneName": "Customer Secure Zone",
                "ZoneId": "51"
            },
            {
                "ZoneName": "Wholesaler Secure Zone",
                "ZoneId": "3573"
            }
        ]
    }
}

Comments

1

You have a couple of extra commas in there and extra commas are not supported in the JSON spec. Eg: "ZoneId": "3573",},]} (before the bracket and brace). You'll have difficulty so long as those remain.

Comments

0
  1. You've got a syntax error in the JavaScript string somewhere, I'll assume it's some bad copy paste.

  2. Given the stricture, you'll want to be looking for obj.SecureZoneSubscriptionList.Subscriptions[0].ZoneId - note the .Subscriptions in there

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.