English | 中文
JSON Flattening Parser and Stringifier in Scratch.
Open with TurboWarp editor:
https://turbowarp.org/editor?project_title=FlatJSON&project_url=bddjr.github.io/FlatJSON/FlatJSON.sb3
Download sb3 file:
https://bddjr.github.io/FlatJSON/FlatJSON.sb3
Scratch Project Link:
https://scratch.mit.edu/projects/1384506526/
JSON syntax reference: https://www.json.org/json-en.html
Scratch converts strings to lowercase before comparison, so it is more lenient than the JSON specification syntax:
In input strings, the letter immediately following \ can be uppercase (for example \N), and the true, false, and null keywords can use uppercase letters (TRUE, FALSE, NULL).
The output will automatically convert them to lowercase, ensuring compliance with JSON syntax (except for the rawjson type).
key:
- An empty key represents the root; non-empty keys must start with
/. - Multi-level keys are separated by
/, for example/a/b/c. /will be escaped as\/, and\will be escaped as\\.- If a character is a control character (U+0000 - U+001F), use the escaped format, for example
\b\t\n\f\r\u001f. - It is recommended not to use uppercase letters, because Scratch converts strings to lowercase before comparison.
type:
objectarraystringnumberbooleannullrawjson(stringify only)
value:
- When the type is
array, the value is used to store the length of the array. \bwill be parsed as�(U+FFFD), because scratch-parser removes all\bin a Scratch project'sproject.json.
Older versions of scratch-parser would even turn"\\b"into"\", causing project loading to fail.
The returned error is in item 1 of FlatJSON.error.
If this item exists, the returned data is invalid.
If this item does not exist, no error occurred.
Variables, lists, and custom blocks starting with FlatJSON/internal. are for internal use by FlatJSON.
Do not call or modify them unless you know what you are doing.
Do not execute multiple FlatJSON custom blocks in parallel within the same sprite.
If needed, please use clones.
Custom block: FlatJSON.parse
Input variable:
FlatJSON.parse.input
The JSON to be parsed.
Output lists:
FlatJSON.parse.output.key
The key list, formatted as described in "Rules".FlatJSON.parse.output.key.basename
The basename list of the keys, for example, the basename in/a/b/cisc.FlatJSON.parse.output.key.parent
The parent key list, for example, the parent key of/a/b/cis/a/b.
The parent key of an empty key is an empty key.FlatJSON.parse.output.key.prefix
The prefix list of the keys, for example, the prefix in/a/b/cis/a/b/.FlatJSON.parse.output.type
The type list, possible types are described in "Rules".FlatJSON.parse.output.value
The value list.
If the type isobjectornull, the value is an empty string.
If the type isarray, the value is the array length.
Errors:
parse: Unexpected end of JSON inputparse: Unexpected token '<char>' at position <offset>parse: Expected double-quoted property name in JSON at position <offset>parse: Bad control character in string literal in JSON at position <offset>parse: Bad escaped character in JSON at position <offset>parse: Bad Unicode escape in JSON at position <offset>parse: Expected ':' after property name in JSON at position <offset>parse: Expected ',' or '}' after property value in JSON at position <offset>parse: Expected ',' or ']' after array element in JSON at position <offset>parse: Unexpected number in JSON at position <offset>parse: Unexpected number '<number>' in JSON
Custom block: FlatJSON.stringify
Input variables:
FlatJSON.stringify.input.root_key
Specifies which root key to start stringifying from.FlatJSON.stringify.input.space
Specifies the indentation string, for example, two spaces.
Input lists:
FlatJSON.stringify.input.key
The key list.FlatJSON.stringify.input.type
The type list.FlatJSON.stringify.input.value
The value list.
Output variable:
FlatJSON.stringify.output
The output JSON.
Errors:
stringify: Expected input key, type, and value lists to have the same lengthstringify: Expected non-empty inputstringify: Expected input key list to contain the root keystringify: Expected non-empty key to start with '/' at position <index>stringify: Unexpected end of key input at position <index>stringify: Bad escaped character of key input at position <index>:<offset>stringify: Bad Unicode escape of key input at position <index>:<offset>stringify: Unexpected type '<type>' at position <index>
This block checks the FlatJSON.stringify.input.key list, and replaces each item in it with a normalized key.
This block checks whether the input number is valid, and turns it into null if invalid.
This block does not validate whether the input rawjson conforms to the JSON specification; you need to ensure the input JSON is valid yourself.
Custom block: FlatJSON.copy_parse_output_to_stringify_input
This block performs the following operations:
- Copies
FlatJSON.parse.output.keytoFlatJSON.stringify.input.key - Copies
FlatJSON.parse.output.typetoFlatJSON.stringify.input.type - Copies
FlatJSON.parse.output.valuetoFlatJSON.stringify.input.value
Custom block: FlatJSON.clear
Clears FlatJSON.error, then automatically calls the following custom blocks:
FlatJSON.clear_temporary_variablesFlatJSON.clear_parse_inputFlatJSON.clear_parse_outputFlatJSON.clear_stringify_inputFlatJSON.clear_stringify_output
Custom block: FlatJSON.clear_temporary_variables
Clears all variables and lists starting with FlatJSON/internal.temp..
Custom block: FlatJSON.clear_parse_input
Clears the FlatJSON.parse.input variable.
Custom block: FlatJSON.clear_parse_output
Clears all lists starting with FlatJSON.parse.output..
Custom block: FlatJSON.clear_stringify_input
Clears all variables and lists starting with FlatJSON.stringify.input..
Custom block: FlatJSON.clear_stringify_input_lists
Clears all lists starting with FlatJSON.stringify.input..
Custom block: FlatJSON.clear_stringify_output
Clears the FlatJSON.stringify.output variable.
This project is released into the public domain under the Unlicense.
The "Scratch" name is a trademark of the Scratch Foundation.