This article has links to websites or programs outside of Scratch and Wikipedia. Remember to stay safe while using the internet, as we cannot guarantee the safety of other websites. |
For more information, see JSON on Wikipedia. JavaScript Object Notation, or JSON, is the data format used by Scratch to store information about projects, sprites, and scripts. It is based on JavaScript,[1] the programming language in which Scratch 3.0 is written. The information for an exported project is stored in a JSON file named "project.json" and the information for an exported sprite is stored in a file named "sprite.json". (A project's JSON can also be downloaded from the URL https://projects.scratch.mit.edu/ followed by the project ID.)
Syntax
Object
In JSON, an object associates names with values. These are written using the syntax {"name1": value1, "name2": value2, …, "namen": valuen}
. The values can be of any type; they can even be other objects. For example, {"answer": 42}
represents an object associating the name "answer" with the number 42.
Arrays
An array is a sequence of values, similar to a list in Scratch. These are written using the syntax [element1, element2, …, elementn]
. The elements can be values of any type, even other arrays.
Numbers
Numerals denote numbers. Fractions must be written as decimals (using full stops) and thousands are not seperated, just as in Scratch.
Strings
Strings represent text. These are written between double quotation marks, for example: "hello"
. Some characters must be written using backslash escaping.
Backslash escaping is placing a backslash in front of a character to use it in a string where it could not otherwise be used, or using notation to insert a special character where it would be inconvenient to insert it directly. For example, "\""
represents a quotation mark, "\\"
represents a single backslash, and "\b"
represents a backspace.
Booleans
A Boolean is true or false and is written as such. (JSON is case-sensitive.)
Null
Null is a value which represents nothing and is written as "null
".
Using JSON
JavaScript provides an object, JSON
, that allows reading and writing JSON:
JSON.stringify({a: [1, 2, 3]}) // ===> "{\"a\":[1,2,3]}"
JSON.parse("{\"a\":[1,2,3]}") // ===> {a: [1, 2, 3]}
Python provides a module for this purpose. The JSON website provides an extensive list of libraries for reading and writing JSON in many different programming languages.
Example
Here is more or less the project.json
file of the default project of Scratch 3.0, with added spaces:
{
"targets": [
{
"isStage": true,
"name": "Stage",
"variables": {
"Z[o`{!h48F+n]q_]ASoC": ["my variable", 0]
},
"lists": {},
"broadcasts": {},
"blocks": {},
"comments": {},
"currentCostume": 0,
"costumes": [
{
"assetId": "cd21514d0531fdffb22204e0ec5ed84a",
"name": "backdrop1",
"md5ext": "cd21514d0531fdffb22204e0ec5ed84a.svg",
"dataFormat": "svg",
"rotationCenterX": 240,
"rotationCenterY": 180
}
],
"sounds": [
{
"assetId": "83a9787d4cb6f3b7632b4ddfebf74367",
"name": "pop",
"dataFormat": "wav",
"format": "",
"rate": 44100,
"sampleCount": 1032,
"md5ext": "83a9787d4cb6f3b7632b4ddfebf74367.wav"
}
],
"volume": 100,
"layerOrder": 0,
"tempo": 60,
"videoTransparency": 50,
"videoState": "on",
"textToSpeechLanguage": null
},
{
"isStage": false,
"name": "Sprite1",
"variables": {},
"lists": {},
"broadcasts": {},
"blocks": {},
"comments": {},
"currentCostume": 0,
"costumes": [
{
"assetId": "b7853f557e4426412e64bb3da6531a99",
"name": "costume1",
"bitmapResolution": 1,
"md5ext": "b7853f557e4426412e64bb3da6531a99.svg",
"dataFormat": "svg",
"rotationCenterX": 48,
"rotationCenterY": 50
},
{
"assetId": "e6ddc55a6ddd9cc9d84fe0b4c21e016f",
"name": "costume2",
"bitmapResolution": 1,
"md5ext": "e6ddc55a6ddd9cc9d84fe0b4c21e016f.svg",
"dataFormat": "svg",
"rotationCenterX": 46,
"rotationCenterY": 53
}
],
"sounds": [
{
"assetId": "83c36d806dc92327b9e7049a565c6bff",
"name": "Meow",
"dataFormat": "wav",
"format": "",
"rate": 44100,
"sampleCount": 37376,
"md5ext": "83c36d806dc92327b9e7049a565c6bff.wav"
}
],
"volume": 100,
"layerOrder": 1,
"visible": true,
"x": 0,
"y": 0,
"size": 100,
"direction": 90,
"draggable": false,
"rotationStyle": "all around"
}
],
"monitors": [],
"extensions": [],
"meta": {
"semver": "3.0.0",
"vm": "0.2.0-prerelease.20190515153227",
"agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) ScratchDesktop/3.3.0 Chrome/69.0.3497.128 Electron/4.2.0 Safari/537.36"
}
}