Strings and data

Read JSON data, update a list, build a map, and format the result.

The JSON block holds the text as data; value() reads it as a map with a team string and a members list. push adds a name and returns the updated list; len reports its item count. Dot notation reads a known map key, while json_stringify turns the new map back into JSON text.

Script: strings-data.lms

raw = _json_;
{"team":"draw","members":["Ada","Lin"]}
end _json_
data = raw.value();
names = data.members;
names = push(names, "Mina");
record = {team: data.team, count: names.len()};
println(f"{upper(data.team)} has {record.count} members: {join(names, ", ")}");
println(json_stringify(record));

Run it

lmscript examples/strings-data.lms

Output

DRAW has 3 members: Ada, Lin, Mina
{"team":"draw","count":3}

Previous: Control flow and functions · Next: Files and processes

Try it: download examples/strings-data.lms, save it under examples/ in a writable working directory, then run the command above from that directory.