1

I have not yet used Json so this is very new to me. However, I am working on a system that outputs a json string from which I have to retrieve a single object to use in a js script.

This is the output

{
    "SecureZoneSubscriptionList": {
        "EntityId": 51350993,
            "Subscriptions": [{
            "ZoneName": "FACCM    Membership",
                "ZoneId": "6460",
                "ExpiryDate": "9/5/2014 12:00:00 AM",
                "SellAccess": true,
                "CostPerPeriod": "0.1",
                "CycleType": ""
        }, ]
    }
}

How can I retrieve JUST the expiryDate?

Thanks!

Moazzam Khan
  • 3,130
  • 2
  • 20
  • 35
Yes I am a cat
  • 279
  • 1
  • 5
  • 14

4 Answers4

2

To make it easier to see:

{
  "SecureZoneSubscriptionList": {
    "EntityId": 51350993,
    "Subscriptions": [
      {
        "ZoneName": "FACCM Membership",
        "ZoneId": "6460",
        "ExpiryDate": "9\/5\/2014 12:00:00 AM",
        "SellAccess": true,
        "CostPerPeriod": "0.1",
        "CycleType": ""
      }
    ]
  }
}

so you would do the following:

var data= {"SecureZoneSubscriptionList": {"EntityId": 51350993,"Subscriptions": [{"ZoneName": "FACCM Membership","ZoneId": "6460","ExpiryDate": "9/5/2014 12:00:00 AM","SellAccess": true,"CostPerPeriod": "0.1","CycleType": ""}]}};
var expiryDate = data.SecureZoneSubscriptionList.Subscriptions[0].ExpiryDate;

if you're retrieving it as a string from a server response, you would JSON.parse to get the object

var data = JSON.parse('{"SecureZoneSubscriptionList": {"EntityId": 51350993,"Subscriptions": [{"ZoneName": "FACCM Membership","ZoneId": "6460","ExpiryDate": "9/5/2014 12:00:00 AM","SellAccess": true,"CostPerPeriod": "0.1","CycleType": ""}]}}');
var expiryDate = data.SecureZoneSubscriptionList.Subscriptions[0].ExpiryDate;
g19fanatic
  • 10,567
  • 6
  • 33
  • 63
1

JSON data is simply a javascript object. JSON stands for Javascript Object Notation. Therefore you can get your data the same way as if your were traversing object attributes in JS:

var string = {"SecureZoneSubscriptionList": {"EntityId": 51350993,"Subscriptions": [{"ZoneName": "FACCM Membership","ZoneId": "6460","ExpiryDate": "9/5/2014 12:00:00 AM","SellAccess": true,"CostPerPeriod": "0.1","CycleType": ""},]}}

var expiryDate = string.SecureZoneSubscriptionList.Subscriptions[0].ExpiryDate;
Matthew R.
  • 4,332
  • 1
  • 24
  • 39
1

Provided this:

var fromServer = {"SecureZoneSubscriptionList": {"EntityId": 51350993,"Subscriptions": [{"ZoneName": "FACCM Membership","ZoneId": "6460","ExpiryDate": "9/5/2014 12:00:00 AM","SellAccess": true,"CostPerPeriod": "0.1","CycleType": ""},]}}

you would access the ExpiryDate as such:

var expDate = fromServer.SecureZoneSubscriptionList.Subscriptions[0].ExpiryDate;
Björn Roberg
  • 2,275
  • 2
  • 15
  • 15
1

This is not the best answer. The best answer is by far is to parse the JSON and access your value through the resulting object. Having said that, JSON is a string. When you need data from a string, regular expressions is always an option.

var myString = '{"SecureZoneSubscriptionList": { "EntityId": 51350993, "Subscriptions": [{ "ZoneName": "FACCM    Membership", "ZoneId": "6460", "ExpiryDate": "9/5/2014 12:00:00 AM",    "SellAccess": true,  "CostPerPeriod": "0.1",  "CycleType": ""        }, ] } }';
var matches = myString.match(/"ExpiryDate":\s?"([^"]*)"/);
alert(matches[1]);

DEMO

lightswitch05
  • 9,058
  • 7
  • 52
  • 75