Skip to content

Instantly share code, notes, and snippets.

@nakasyou
Created March 1, 2023 08:37
Show Gist options
  • Save nakasyou/da7b38450af8666c563fccc22585d764 to your computer and use it in GitHub Desktop.
Save nakasyou/da7b38450af8666c563fccc22585d764 to your computer and use it in GitHub Desktop.
json-scratch
(function(Scratch) {
'use strict';
class Json2Scratch {
getInfo () {
return {
id: 'parse',
name: 'Parse',
blocks: [
{
opcode: 'parse',
blockType: Scratch.BlockType.REPORTER,
text: 'JSON parse [JSON] of [PATH]',
arguments: {
JSON: {
type: Scratch.ArgumentType.STRING,
defaultValue: '{"a":["b","c"]}'
},
PATH: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'a.0'
}
}
}
]
};
}
parse(args){
try{
const json=JSON.parse(args.JSON);
const path=args.PATH.split(".");
let value=json;
for(const locate of path){
value=value[locate];
}
return value;
}catch{return "NaN"}
}
}
Scratch.extensions.register(new Json2Scratch());
})(Scratch);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment