Working with JSON

JSON (JavaScript Object Notation) is a human-readable, easily parsed or generated data-interchange language. Salesforce B2C Commerce provides a top level JSON class to make it simple to exchange objects between your server and client. The B2C Commerce implementation is based on the json2.js implementation of JSON.

JSON objects are useful if you need to create a complex data object of an arbitrary depth. For example, if you have a master product with options that depend on other attributes, such as gender or size.

B2C Commerce provides a parsing method that can be used to construct a JSON object from a JSON string used as the value of an attribute field. Salesforce recommends using the parse(json : String, reviver : Function) : Object function for this purpose.

The object can then be further manipulated using any ECMA-compliant language, such as JavaScript . The object can then be stringified using the B2C Commerce-provided stringify() method and transferred to the client.

Alternatively, a JSON string used as the value of an attribute field can be transferred to the client and then parsed to create an object for further operations.

JSON objects are not recommended for general use, because JSON does not handle recursive structures effectively.