-2

Here is an object:

{
  "@odata.context":"http://company/api/data/v8.2/$metadata#EntityDefinitions(608861bc-50a4-4c5f-a02c-21fe1943e2cf)/Attributes/Microsoft.Dynamics.CRM.StateAttributeMetadata(LogicalName,OptionSet)","value":[
    {
      "LogicalName":"statecode","MetadataId":"cdc3895a-7539-41e9-966b-3f9ef805aefd","OptionSet@odata.context":"http://vsevm.centralus.cloudapp.azure.com/Xypex/api/data/v8.2/$metadata#EntityDefinitions(608861bc-50a4-4c5f-a02c-21fe1943e2cf)/Attributes(cdc3895a-7539-41e9-966b-3f9ef805aefd)/Microsoft.Dynamics.CRM.StateAttributeMetadata/OptionSet/$entity","OptionSet":{
        "MetadataId":"88fa5ad0-2a4b-4281-ac9c-b4e71fb77920","HasChanged":null,"Description":{
          "LocalizedLabels":[
            {
              "Label":"Status of the contact.","LanguageCode":1033,"IsManaged":true,"MetadataId":"fc7cf5df-8503-46e3-85b2-6c0ac68bc912","HasChanged":null
            }
          ],"UserLocalizedLabel":{
            "Label":"Status of the contact.","LanguageCode":1033,"IsManaged":true,"MetadataId":"fc7cf5df-8503-46e3-85b2-6c0ac68bc912","HasChanged":null
          }
        },"DisplayName":{
          "LocalizedLabels":[
            {
              "Label":"Status","LanguageCode":1033,"IsManaged":true,"MetadataId":"37c73b95-afd6-4d25-9ded-3691db6ce56b","HasChanged":null
            }
          ],"UserLocalizedLabel":{
            "Label":"Status","LanguageCode":1033,"IsManaged":true,"MetadataId":"37c73b95-afd6-4d25-9ded-3691db6ce56b","HasChanged":null
          }
        },"IsCustomOptionSet":false,"IsGlobal":false,"IsManaged":true,"IsCustomizable":{
          "Value":true,"CanBeChanged":false,"ManagedPropertyLogicalName":"iscustomizable"
        },"Name":"contact_statecode","OptionSetType":"State","IntroducedVersion":null,"Options":[
          {
            "@odata.type":"#Microsoft.Dynamics.CRM.StateOptionMetadata","Value":0,"Label":{
              "LocalizedLabels":[
                {
                  "Label":"Active","LanguageCode":1033,"IsManaged":true,"MetadataId":"4754c2fa-2241-db11-898a-0007e9e17ebd","HasChanged":null
                }
              ],"UserLocalizedLabel":{
                "Label":"Active","LanguageCode":1033,"IsManaged":true,"MetadataId":"4754c2fa-2241-db11-898a-0007e9e17ebd","HasChanged":null
              }
            },"Description":{
              "LocalizedLabels":[

              ],"UserLocalizedLabel":null
            },"Color":null,"IsManaged":true,"MetadataId":null,"HasChanged":null,"DefaultStatus":1,"InvariantName":"Active"
          },{
            "@odata.type":"#Microsoft.Dynamics.CRM.StateOptionMetadata","Value":1,"Label":{
              "LocalizedLabels":[
                {
                  "Label":"Inactive","LanguageCode":1033,"IsManaged":true,"MetadataId":"4954c2fa-2241-db11-898a-0007e9e17ebd","HasChanged":null
                }
              ],"UserLocalizedLabel":{
                "Label":"Inactive","LanguageCode":1033,"IsManaged":true,"MetadataId":"4954c2fa-2241-db11-898a-0007e9e17ebd","HasChanged":null
              }
            },"Description":{
              "LocalizedLabels":[

              ],"UserLocalizedLabel":null
            },"Color":null,"IsManaged":true,"MetadataId":null,"HasChanged":null,"DefaultStatus":2,"InvariantName":"Inactive"
          }
        ]
      }
    }
  ]
}

I am accessing this object using JavaScript. Id like to access the value of "Label" within this LocalizedLabel.

"Label":{
              "LocalizedLabels":[
                {
                  "Label":"Active","LanguageCode":1033,"IsManaged":true,"MetadataId":"4754c2fa-
2241-db11-898a-0007e9e17ebd","HasChanged":null
                    }
}
Michael Drum
  • 1,151
  • 5
  • 14
  • 26

1 Answers1

1

Accessing the different layers in a Javascript object is pretty straight forward once you get the trick. Essentially, when you want to go one level deeper into an object, you use "." followed by the key name. Ex:

object = {"key1": { "key2": "some value"}}
value = object.key1.key2 //returns "some value" 

This will return the value associated to that key. If that value is another object, you can iterate the process. If that value is an array, well, just operate on it like you would on any other array.

Combine that with usual array access techniques:

object = {"key1": 
             { "key2": 
                 [ 
                     {"key3": "some value"},
                     {"key3": "another value"}
                 ]
         }}
list = object.key1.key2 // returns  "[ {"key3": "some value"},{"key3": "another attribute"}]"

listElement = object.key1.key2[0] // return "{"key3": "some value"}"

value = object.key1.key2[0].key3 // returns "some value"

otherValue = object.key1.key2[1].key3 // returns "another value"

etc.

edit: As pointed in the comment below, JSON is simply a formatting, not a data type in itself. What you want to know is here is how to operate on javascript objects and arrays. (In other words, JSON is a way to write objects and/or arrays as a string that is syntactically correct javascript, see here for more details)

Nicolas Couvrat
  • 371
  • 2
  • 13