JavaScript Object Notation
JSON (JavaScript Object Notation) is a human-readable format for storing and transmitting data objects. Many web services use JSON for making and receiving requests. In MaSH we can use JSON to store, send, read and retrieve variables and objects. You can read more about JSON here here.
Encoding JSON
encode
mash.json.encode(mixed: data) -> String
Encodes data and returns a string.
Parameters
data
A variable or object to be encoded.
Natural
set dictionary to { name: "Grace Hopper", occupation: "Computer Scientist" }
set json to mash.json.encode(dictionary)
printline json
Standard
dictionary = { name: "Grace Hopper", occupation: "Computer Scientist" }
json = mash.json.encode(dictionary)
printline(json)
Output
{"name":"Grace Hopper","occupation":"Computer Scientist"}
Decoding JSON
decode
mash.json.decode(string: json) -> Object
Decodes a JSON string and returns an object.
Parameters
json
A JSON-encoded string.
Natural
set json to '{"name":"Grace Hopper","occupation":"Computer Scientist"}'
set object to mash.json.decode(json)
printline object
Standard
json = '{"name":"Grace Hopper","occupation":"Computer Scientist"}'
object = mash.json.decode(json)
printline(object)
Output
Object {
"name": "Grace Hopper",
"occupation": "Computer Scientist"
}
Notes
If you’re using the
mash.httplibrary you can take advantage of thejsonproperty on aResponseobject to automatically decode JSON data.