function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kumar_arunkumar_arun 

How to get Object element in json?

JSON STRING:

{"size":21,"totalSize":21,"done":true,"queryLocator":null,"entityTypeName":"ValidationRule","records":[{"attributes":{"type":"ValidationRule","url":"/services/data/v33.0/tooling/sobjects/ValidationRule/03d2800000083bXAAQ"},"Id":"03d2800000083bXAAQ","FullName":"SmartvCard__Note__c.SmartvCard__Not_new","CreatedDate":"2016-11-30T19:36:50.000+0000","TableEnumOrId":"01I280000022PApEAM","ValidationName":"Not_new","Metadata":{"description":null,"errorConditionFormula":"SmartvCard__Validation_Record__c = True","errorDisplayField":null,"errorMessage":"vCard Note record already exists. There can be only one vCard Note Record.","urls":null,"active":true}}]}
APEX:

JSONParser parser = JSON.createParser(response.getBody());
       List<String> fullName= new List<string>();
       List<string> Tot_size=new list<string>();
       List<string> metaData=new list<string>();
       while (parser.nextToken() != null) {
           if( (parser.getCurrentToken() == JSONToken.FIELD_NAME) && ( parser.getText() == 'FullName') ){
           parser.nextValue();
           fullName.add(parser.getText());
           
           }
           if( (parser.getCurrentToken() == JSONToken.FIELD_NAME) && ( parser.getText() == 'Size') ){
           parser.nextValue();
           Tot_size.add(parser.getText());
            system.debug('====================='+ parser.nextValue());
           }
            if( (parser.getCurrentToken() == JSONToken.FIELD_NAME) && ( parser.getText() == 'Metadata') ){
           parser.nextValue();
           metaData.add(parser.getText());
           system.debug(metaData);
           }
          
           
          }
        }

 i want to get values from "Metadata" object in the json string. I easy get "FullName", "Size" but when I am parsing Metadata , it only return "{" in list<string> MetaData. How can i get "active":values in this list.

Thanks
 
Daniel PeñalozaDaniel Peñaloza
Hi, kumar_arun:

Try to use this approach. Create a Map of <String, Object> to store the "Metadata" attribute values and try with this code snippet:
// Store "metadata" attributes in a Map
Map<String, Object> metadata = new Map<String, Object>();

while(parser.nextToken() != null) {
    
    // ... previous code omited...

    if (parser.getCurrentToken() == JSONToken.FIELD_NAME && parser.getText() == 'Metadata') {
		// Iterate over "Metadata" attributes
        while (parser.nextToken() != null) {
            if (parser.getCurrentToken() == JSONToken.FIELD_NAME && parser.getCurrentToken() != JSONToken.END_OBJECT) {
                // Attribute Key
                String key = parser.getText();
                
                // Attribute Value
               	JSONToken nextValue = parser.nextValue();
                 if (nextValue == JSONToken.VALUE_FALSE) {
                    metadata.put(key, false);
                } else if (nextValue == JSONToken.VALUE_NULL) {
                    metadata.put(key, null);
                } else if (nextValue == JSONToken.VALUE_NUMBER_FLOAT ) {
                    metadata.put(key, parser.getDoubleValue());
                } else if (nextValue == JSONToken.VALUE_NUMBER_INT ) {
                    metadata.put(key, parser.getIntegerValue());
                } else if (nextValue == JSONToken.VALUE_STRING ) {
                    metadata.put(key, parser.getText());
                } else if (nextValue == JSONToken.VALUE_TRUE ) {
                    metadata.put(key, true);
                }
            }
        }
    }
}

System.debug('metadata: ' + metadata);