JSONParser Class

Represents a parser for JSON-encoded content.

System

Use the System.JSONParser methods to parse a response that’s returned from a call to an external service that is in JSON format, such as a JSON-encoded response of a Web service callout.

See Also

The following are methods for JSONParser. All are instance methods.

  • clearCurrentToken()

    Removes the current token.

  • getBlobValue()

    Returns the current token as a BLOB value.

  • getBooleanValue()

    Returns the current token as a Boolean value.

  • getCurrentName()

    Returns the name associated with the current token.

  • getCurrentToken()

    Returns the token that the parser currently points to or null if there’s no current token.

  • getDatetimeValue()

    Returns the current token as a date and time value.

  • getDateValue()

    Returns the current token as a date value.

  • getDecimalValue()

    Returns the current token as a decimal value.

  • getDoubleValue()

    Returns the current token as a double value.

  • getIdValue()

    Returns the current token as an ID value.

  • getIntegerValue()

    Returns the current token as an integer value.

  • getLastClearedToken()

    Returns the last token that was cleared by the clearCurrentToken method.

  • getLongValue()

    Returns the current token as a long value.

  • getText()

    Returns the textual representation of the current token or null if there’s no current token.

  • getTimeValue()

    Returns the current token as a time value.

  • hasCurrentToken()

    Returns true if the parser currently points to a token; otherwise, returns false.

  • nextToken()

    Returns the next token or null if the parser has reached the end of the input stream.

  • nextValue()

    Returns the next token that is a value type or null if the parser has reached the end of the input stream.

  • readValueAs(apexType)

    Deserializes JSON content into an object of the specified Apex type and returns the deserialized object.

  • readValueAsStrict(apexType)

    Deserializes JSON content into an object of the specified Apex type and returns the deserialized object. All attributes in the JSON content must be present in the specified type.

  • skipChildren()

    Skips all child tokens of type JSONToken.START_ARRAY and JSONToken.START_OBJECT that the parser currently points to.

Removes the current token.

public Void clearCurrentToken()

Type: Void

After this method is called, a call to hasCurrentToken returns false and a call to getCurrentToken returns null. You can retrieve the cleared token by calling getLastClearedToken.

Returns the current token as a BLOB value.

public Blob getBlobValue()

Type: Blob

The current token must be of type JSONToken.VALUE_STRING and must be Base64-encoded.

Returns the current token as a Boolean value.

public Boolean getBooleanValue()

Type: Boolean

The current token must be of type JSONToken.VALUE_TRUE or JSONToken.VALUE_FALSE.

The following example parses a sample JSON string and retrieves a Boolean value.

Returns the name associated with the current token.

public String getCurrentName()

Type: String

If the current token is of type JSONToken.FIELD_NAME, this method returns the same value as getText. If the current token is a value, this method returns the field name that precedes this token. For other values such as array values or root-level values, this method returns null.

The following example parses a sample JSON string. It advances to the field value and retrieves its corresponding field name.

Returns the token that the parser currently points to or null if there’s no current token.

public System.JSONToken getCurrentToken()

Type: System.JSONToken

The following example iterates through all the tokens in a sample JSON string.

Returns the current token as a date and time value.

public Datetime getDatetimeValue()

Type: Datetime

The current token must be of type JSONToken.VALUE_STRING and must represent a Datetime value in the ISO-8601 format.

The following example parses a sample JSON string and retrieves a Datetime value.

Returns the current token as a date value.

public Date getDateValue()

Type: Date

The current token must be of type JSONToken.VALUE_STRING and must represent a Date value in the ISO-8601 format.

The following example parses a sample JSON string and retrieves a Date value.

Returns the current token as a decimal value.

public Decimal getDecimalValue()

Type: Decimal

The current token must be of type JSONToken.VALUE_NUMBER_FLOAT or JSONToken.VALUE_NUMBER_INT and is a numerical value that can be converted to a value of type Decimal.

The following example parses a sample JSON string and retrieves a Decimal value.

Returns the current token as a double value.

public Double getDoubleValue()

Type: Double

The current token must be of type JSONToken.VALUE_NUMBER_FLOAT and is a numerical value that can be converted to a value of type Double.

The following example parses a sample JSON string and retrieves a Double value.

Returns the current token as an ID value.

public ID getIdValue()

Type: ID

The current token must be of type JSONToken.VALUE_STRING and must be a valid ID.

The following example parses a sample JSON string and retrieves an ID value.

Returns the current token as an integer value.

public Integer getIntegerValue()

Type: Integer

The current token must be of type JSONToken.VALUE_NUMBER_INT and must represent an Integer.

The following example parses a sample JSON string and retrieves an Integer value.

Returns the last token that was cleared by the clearCurrentToken method.

public System.JSONToken getLastClearedToken()

Type: System.JSONToken

Returns the current token as a long value.

public Long getLongValue()

Type: Long

The current token must be of type JSONToken.VALUE_NUMBER_INT and is a numerical value that can be converted to a value of type Long .

The following example parses a sample JSON string and retrieves a Long value.

Returns the textual representation of the current token or null if there’s no current token.

public String getText()

Type: String

No current token exists, and therefore this method returns null, if nextToken has not been called yet for the first time or if the parser has reached the end of the input stream.

Returns the current token as a time value.

public Time getTimeValue()

Type: Time

The current token must be of type JSONToken.VALUE_STRING and must represent a Time value in the ISO-8601 format.

The following example parses a sample JSON string and retrieves a Datetime value.

Returns true if the parser currently points to a token; otherwise, returns false.

public Boolean hasCurrentToken()

Type: Boolean

Returns the next token or null if the parser has reached the end of the input stream.

public System.JSONToken nextToken()

Type: System.JSONToken

Advances the stream enough to determine the type of the next token, if any.

Returns the next token that is a value type or null if the parser has reached the end of the input stream.

public System.JSONToken nextValue()

Type: System.JSONToken

Advances the stream enough to determine the type of the next token that is of a value type, if any, including a JSON array and object start and end markers.

Deserializes JSON content into an object of the specified Apex type and returns the deserialized object.

public Object readValueAs(System.Type apexType)

  • apexType:

    Type: System.Type

    The apexType argument specifies the type of the object that this method returns after deserializing the current value.

Type: Object

If the JSON content contains attributes not present in the System.Type argument, such as a missing field or object, deserialization fails in some circumstances. When deserializing JSON content into a custom object or an sObject using Salesforce API version 34.0 or earlier, this method throws a runtime exception when passed extraneous attributes. When deserializing JSON content into an Apex class in any API version, or into an object in API version 35.0 or later, no exception is thrown. When no exception is thrown, this method ignores extraneous attributes and parses the rest of the JSON content.

The following example parses a sample JSON string and retrieves a Datetime value. Before being able to run this sample, you must create a new Apex class as follows:

Next, insert the following sample in a class method:

Deserializes JSON content into an object of the specified Apex type and returns the deserialized object. All attributes in the JSON content must be present in the specified type.

public Object readValueAsStrict(System.Type apexType)

  • apexType:

    Type: System.Type

    The apexType argument specifies the type of the object that this method returns after deserializing the current value.

Type: Object

If the JSON content contains attributes not present in the System.Type argument, such as a missing field or object, deserialization fails in some circumstances. When deserializing JSON content with extraneous attributes into an Apex class, this method throws an exception in all API versions. However, no exception is thrown when you use this method to deserialize JSON content into a custom object or an sObject.

The following example parses a sample JSON string and retrieves a Datetime value. Before being able to run this sample, you must create a new Apex class as follows:

Next, insert the following sample in a class method:

Skips all child tokens of type JSONToken.START_ARRAY and JSONToken.START_OBJECT that the parser currently points to.

public Void skipChildren()

Type: Void