feat: add example scripts for argparse, bytes, dataset, html, markdown, uuid, wdantic, and web chat modules
Add comprehensive demo scripts showcasing the usage of multiple Wren modules including argument parsing, byte manipulation, in-memory dataset operations, HTML encoding/slugification, markdown-to-HTML conversion, UUID generation/validation, schema validation with wdantic, and a full web chat application with REST endpoints.
2026-01-24 23:40:22 +01:00
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<!-- retoor <retoor@molodetz.nl> -->
|
|
|
|
|
<html lang="en">
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
|
<title>Working with JSON - Wren-CLI Manual</title>
|
|
|
|
|
<link rel="stylesheet" href="../css/style.css">
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<button class="mobile-menu-toggle">Menu</button>
|
|
|
|
|
<div class="container">
|
|
|
|
|
<aside class="sidebar">
|
|
|
|
|
<div class="sidebar-header">
|
|
|
|
|
<h1><a href="../index.html">Wren-CLI</a></h1>
|
|
|
|
|
<div class="version">v0.4.0</div>
|
|
|
|
|
</div>
|
|
|
|
|
<nav class="sidebar-nav">
|
|
|
|
|
<div class="section">
|
|
|
|
|
<span class="section-title">Getting Started</span>
|
|
|
|
|
<ul>
|
|
|
|
|
<li><a href="../getting-started/index.html">Overview</a></li>
|
|
|
|
|
<li><a href="../getting-started/installation.html">Installation</a></li>
|
|
|
|
|
<li><a href="../getting-started/first-script.html">First Script</a></li>
|
|
|
|
|
<li><a href="../getting-started/repl.html">Using the REPL</a></li>
|
|
|
|
|
</ul>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="section">
|
|
|
|
|
<span class="section-title">Language</span>
|
|
|
|
|
<ul>
|
|
|
|
|
<li><a href="../language/index.html">Syntax Overview</a></li>
|
|
|
|
|
<li><a href="../language/classes.html">Classes</a></li>
|
|
|
|
|
<li><a href="../language/methods.html">Methods</a></li>
|
|
|
|
|
<li><a href="../language/control-flow.html">Control Flow</a></li>
|
|
|
|
|
<li><a href="../language/fibers.html">Fibers</a></li>
|
|
|
|
|
<li><a href="../language/modules.html">Modules</a></li>
|
|
|
|
|
</ul>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="section">
|
|
|
|
|
<span class="section-title">API Reference</span>
|
|
|
|
|
<ul>
|
|
|
|
|
<li><a href="../api/index.html">Overview</a></li>
|
feat: add async/await keywords, Num/String extensions, and Jinja markdown example
Add `async` and `await` token support to the Wren compiler with scheduler resolution logic, extend `Num` with `e` constant, hyperbolic trig, base conversion, and new methods (`isZero`, `gcd`, `lcm`, `digits`, etc.), extend `String` with `lower`, `upper`, `capitalize`, `title`, and regenerate `wren_core.wren.inc`. Include `Makefile` for multi-platform builds, rewrite `README.md` with updated build instructions, and add `example/await_demo.wren` and `example/jinja_markdown.wren` demonstrating new features.
2026-01-25 04:58:39 +01:00
|
|
|
<li><a href="../api/string.html">String</a></li>
|
|
|
|
|
<li><a href="../api/number.html">Num</a></li>
|
feat: add example scripts for argparse, bytes, dataset, html, markdown, uuid, wdantic, and web chat modules
Add comprehensive demo scripts showcasing the usage of multiple Wren modules including argument parsing, byte manipulation, in-memory dataset operations, HTML encoding/slugification, markdown-to-HTML conversion, UUID generation/validation, schema validation with wdantic, and a full web chat application with REST endpoints.
2026-01-24 23:40:22 +01:00
|
|
|
<li><a href="../api/http.html">http</a></li>
|
|
|
|
|
<li><a href="../api/websocket.html">websocket</a></li>
|
|
|
|
|
<li><a href="../api/tls.html">tls</a></li>
|
|
|
|
|
<li><a href="../api/net.html">net</a></li>
|
|
|
|
|
<li><a href="../api/dns.html">dns</a></li>
|
|
|
|
|
<li><a href="../api/json.html">json</a></li>
|
|
|
|
|
<li><a href="../api/base64.html">base64</a></li>
|
|
|
|
|
<li><a href="../api/regex.html">regex</a></li>
|
|
|
|
|
<li><a href="../api/jinja.html">jinja</a></li>
|
|
|
|
|
<li><a href="../api/crypto.html">crypto</a></li>
|
|
|
|
|
<li><a href="../api/os.html">os</a></li>
|
|
|
|
|
<li><a href="../api/env.html">env</a></li>
|
|
|
|
|
<li><a href="../api/signal.html">signal</a></li>
|
|
|
|
|
<li><a href="../api/subprocess.html">subprocess</a></li>
|
|
|
|
|
<li><a href="../api/sqlite.html">sqlite</a></li>
|
|
|
|
|
<li><a href="../api/datetime.html">datetime</a></li>
|
|
|
|
|
<li><a href="../api/timer.html">timer</a></li>
|
|
|
|
|
<li><a href="../api/io.html">io</a></li>
|
feat: add async/await keywords, Num/String extensions, and Jinja markdown example
Add `async` and `await` token support to the Wren compiler with scheduler resolution logic, extend `Num` with `e` constant, hyperbolic trig, base conversion, and new methods (`isZero`, `gcd`, `lcm`, `digits`, etc.), extend `String` with `lower`, `upper`, `capitalize`, `title`, and regenerate `wren_core.wren.inc`. Include `Makefile` for multi-platform builds, rewrite `README.md` with updated build instructions, and add `example/await_demo.wren` and `example/jinja_markdown.wren` demonstrating new features.
2026-01-25 04:58:39 +01:00
|
|
|
<li><a href="../api/pathlib.html">pathlib</a></li>
|
feat: add example scripts for argparse, bytes, dataset, html, markdown, uuid, wdantic, and web chat modules
Add comprehensive demo scripts showcasing the usage of multiple Wren modules including argument parsing, byte manipulation, in-memory dataset operations, HTML encoding/slugification, markdown-to-HTML conversion, UUID generation/validation, schema validation with wdantic, and a full web chat application with REST endpoints.
2026-01-24 23:40:22 +01:00
|
|
|
<li><a href="../api/scheduler.html">scheduler</a></li>
|
|
|
|
|
<li><a href="../api/math.html">math</a></li>
|
|
|
|
|
</ul>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="section">
|
|
|
|
|
<span class="section-title">Tutorials</span>
|
|
|
|
|
<ul>
|
|
|
|
|
<li><a href="../tutorials/index.html">Tutorial List</a></li>
|
|
|
|
|
<li><a href="../tutorials/http-client.html">HTTP Client</a></li>
|
|
|
|
|
<li><a href="../tutorials/websocket-chat.html">WebSocket Chat</a></li>
|
|
|
|
|
<li><a href="../tutorials/database-app.html">Database App</a></li>
|
|
|
|
|
<li><a href="../tutorials/template-rendering.html">Template Rendering</a></li>
|
|
|
|
|
<li><a href="../tutorials/cli-tool.html">CLI Tool</a></li>
|
|
|
|
|
</ul>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="section">
|
|
|
|
|
<span class="section-title">How-To Guides</span>
|
|
|
|
|
<ul>
|
|
|
|
|
<li><a href="index.html">How-To List</a></li>
|
|
|
|
|
<li><a href="http-requests.html">HTTP Requests</a></li>
|
|
|
|
|
<li><a href="json-parsing.html" class="active">JSON Parsing</a></li>
|
|
|
|
|
<li><a href="regex-patterns.html">Regex Patterns</a></li>
|
|
|
|
|
<li><a href="file-operations.html">File Operations</a></li>
|
|
|
|
|
<li><a href="async-operations.html">Async Operations</a></li>
|
|
|
|
|
<li><a href="error-handling.html">Error Handling</a></li>
|
|
|
|
|
</ul>
|
|
|
|
|
</div>
|
|
|
|
|
</nav>
|
|
|
|
|
</aside>
|
|
|
|
|
<main class="content">
|
|
|
|
|
<nav class="breadcrumb">
|
|
|
|
|
<a href="../index.html">Home</a>
|
|
|
|
|
<span class="separator">/</span>
|
|
|
|
|
<a href="index.html">How-To Guides</a>
|
|
|
|
|
<span class="separator">/</span>
|
|
|
|
|
<span>JSON Parsing</span>
|
|
|
|
|
</nav>
|
|
|
|
|
|
|
|
|
|
<article>
|
|
|
|
|
<h1>Working with JSON</h1>
|
|
|
|
|
|
|
|
|
|
<h2>Parse JSON String</h2>
|
|
|
|
|
<pre><code>import "json" for Json
|
|
|
|
|
|
|
|
|
|
var jsonStr = '{"name": "Alice", "age": 30}'
|
|
|
|
|
var data = Json.parse(jsonStr)
|
|
|
|
|
|
|
|
|
|
System.print(data["name"]) // Alice
|
|
|
|
|
System.print(data["age"]) // 30</code></pre>
|
|
|
|
|
|
|
|
|
|
<h2>Parse JSON Array</h2>
|
|
|
|
|
<pre><code>import "json" for Json
|
|
|
|
|
|
|
|
|
|
var jsonStr = '[1, 2, 3, "four", true, null]'
|
|
|
|
|
var items = Json.parse(jsonStr)
|
|
|
|
|
|
|
|
|
|
for (item in items) {
|
|
|
|
|
System.print(item)
|
|
|
|
|
}</code></pre>
|
|
|
|
|
|
|
|
|
|
<h2>Access Nested Objects</h2>
|
|
|
|
|
<pre><code>import "json" for Json
|
|
|
|
|
|
|
|
|
|
var jsonStr = '{"user": {"name": "Bob", "address": {"city": "NYC"}}}'
|
|
|
|
|
var data = Json.parse(jsonStr)
|
|
|
|
|
|
|
|
|
|
System.print(data["user"]["name"]) // Bob
|
|
|
|
|
System.print(data["user"]["address"]["city"]) // NYC</code></pre>
|
|
|
|
|
|
|
|
|
|
<h2>Convert Wren Object to JSON</h2>
|
|
|
|
|
<pre><code>import "json" for Json
|
|
|
|
|
|
|
|
|
|
var data = {
|
|
|
|
|
"name": "Charlie",
|
|
|
|
|
"age": 25,
|
|
|
|
|
"active": true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var jsonStr = Json.stringify(data)
|
|
|
|
|
System.print(jsonStr) // {"name":"Charlie","age":25,"active":true}</code></pre>
|
|
|
|
|
|
|
|
|
|
<h2>Pretty Print JSON</h2>
|
|
|
|
|
<pre><code>import "json" for Json
|
|
|
|
|
|
|
|
|
|
var data = {
|
|
|
|
|
"users": [
|
|
|
|
|
{"name": "Alice", "age": 30},
|
|
|
|
|
{"name": "Bob", "age": 25}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var pretty = Json.stringify(data, 2)
|
|
|
|
|
System.print(pretty)</code></pre>
|
|
|
|
|
|
|
|
|
|
<p>Output:</p>
|
|
|
|
|
<pre><code>{
|
|
|
|
|
"users": [
|
|
|
|
|
{
|
|
|
|
|
"name": "Alice",
|
|
|
|
|
"age": 30
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"name": "Bob",
|
|
|
|
|
"age": 25
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}</code></pre>
|
|
|
|
|
|
|
|
|
|
<h2>Check if Key Exists</h2>
|
|
|
|
|
<pre><code>import "json" for Json
|
|
|
|
|
|
|
|
|
|
var data = Json.parse('{"name": "Alice"}')
|
|
|
|
|
|
|
|
|
|
if (data.containsKey("name")) {
|
|
|
|
|
System.print("Name: %(data["name"])")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!data.containsKey("age")) {
|
|
|
|
|
System.print("Age not specified")
|
|
|
|
|
}</code></pre>
|
|
|
|
|
|
|
|
|
|
<h2>Provide Default Values</h2>
|
|
|
|
|
<pre><code>import "json" for Json
|
|
|
|
|
|
|
|
|
|
var data = Json.parse('{"name": "Alice"}')
|
|
|
|
|
|
|
|
|
|
var name = data["name"]
|
|
|
|
|
var age = data.containsKey("age") ? data["age"] : 0
|
|
|
|
|
var city = data.containsKey("city") ? data["city"] : "Unknown"
|
|
|
|
|
|
|
|
|
|
System.print("%(name), %(age), %(city)")</code></pre>
|
|
|
|
|
|
|
|
|
|
<h2>Iterate Over Object Keys</h2>
|
|
|
|
|
<pre><code>import "json" for Json
|
|
|
|
|
|
|
|
|
|
var data = Json.parse('{"a": 1, "b": 2, "c": 3}')
|
|
|
|
|
|
|
|
|
|
for (key in data.keys) {
|
|
|
|
|
System.print("%(key): %(data[key])")
|
|
|
|
|
}</code></pre>
|
|
|
|
|
|
|
|
|
|
<h2>Iterate Over Array</h2>
|
|
|
|
|
<pre><code>import "json" for Json
|
|
|
|
|
|
|
|
|
|
var users = Json.parse('[{"name": "Alice"}, {"name": "Bob"}]')
|
|
|
|
|
|
|
|
|
|
for (i in 0...users.count) {
|
|
|
|
|
System.print("%(i + 1). %(users[i]["name"])")
|
|
|
|
|
}</code></pre>
|
|
|
|
|
|
|
|
|
|
<h2>Modify JSON Data</h2>
|
|
|
|
|
<pre><code>import "json" for Json
|
|
|
|
|
|
|
|
|
|
var data = Json.parse('{"name": "Alice", "age": 30}')
|
|
|
|
|
|
|
|
|
|
data["age"] = 31
|
|
|
|
|
data["email"] = "alice@example.com"
|
|
|
|
|
data.remove("name")
|
|
|
|
|
|
|
|
|
|
System.print(Json.stringify(data))</code></pre>
|
|
|
|
|
|
|
|
|
|
<h2>Parse JSON from File</h2>
|
|
|
|
|
<pre><code>import "json" for Json
|
|
|
|
|
import "io" for File
|
|
|
|
|
|
|
|
|
|
var content = File.read("config.json")
|
|
|
|
|
var config = Json.parse(content)
|
|
|
|
|
|
|
|
|
|
System.print(config["setting"])</code></pre>
|
|
|
|
|
|
|
|
|
|
<h2>Write JSON to File</h2>
|
|
|
|
|
<pre><code>import "json" for Json
|
|
|
|
|
import "io" for File
|
|
|
|
|
|
|
|
|
|
var data = {
|
|
|
|
|
"database": "myapp.db",
|
|
|
|
|
"port": 8080,
|
|
|
|
|
"debug": true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File.write("config.json", Json.stringify(data, 2))</code></pre>
|
|
|
|
|
|
|
|
|
|
<h2>Handle Parse Errors</h2>
|
|
|
|
|
<pre><code>import "json" for Json
|
|
|
|
|
|
|
|
|
|
var jsonStr = "invalid json {"
|
|
|
|
|
|
|
|
|
|
var fiber = Fiber.new { Json.parse(jsonStr) }
|
|
|
|
|
var result = fiber.try()
|
|
|
|
|
|
|
|
|
|
if (fiber.error) {
|
|
|
|
|
System.print("Parse error: %(fiber.error)")
|
|
|
|
|
} else {
|
|
|
|
|
System.print(result)
|
|
|
|
|
}</code></pre>
|
|
|
|
|
|
|
|
|
|
<h2>Work with Null Values</h2>
|
|
|
|
|
<pre><code>import "json" for Json
|
|
|
|
|
|
|
|
|
|
var data = Json.parse('{"name": "Alice", "address": null}')
|
|
|
|
|
|
|
|
|
|
if (data["address"] == null) {
|
|
|
|
|
System.print("No address provided")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var output = {"value": null}
|
|
|
|
|
System.print(Json.stringify(output)) // {"value":null}</code></pre>
|
|
|
|
|
|
|
|
|
|
<h2>Build JSON Array Dynamically</h2>
|
|
|
|
|
<pre><code>import "json" for Json
|
|
|
|
|
|
|
|
|
|
var users = []
|
|
|
|
|
|
|
|
|
|
users.add({"name": "Alice", "role": "admin"})
|
|
|
|
|
users.add({"name": "Bob", "role": "user"})
|
|
|
|
|
users.add({"name": "Charlie", "role": "user"})
|
|
|
|
|
|
|
|
|
|
System.print(Json.stringify(users, 2))</code></pre>
|
|
|
|
|
|
|
|
|
|
<h2>Filter JSON Array</h2>
|
|
|
|
|
<pre><code>import "json" for Json
|
|
|
|
|
|
|
|
|
|
var users = Json.parse('[
|
|
|
|
|
{"name": "Alice", "age": 30},
|
|
|
|
|
{"name": "Bob", "age": 17},
|
|
|
|
|
{"name": "Charlie", "age": 25}
|
|
|
|
|
]')
|
|
|
|
|
|
|
|
|
|
var adults = []
|
|
|
|
|
for (user in users) {
|
|
|
|
|
if (user["age"] >= 18) {
|
|
|
|
|
adults.add(user)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.print("Adults: %(Json.stringify(adults))")</code></pre>
|
|
|
|
|
|
|
|
|
|
<h2>Transform JSON Data</h2>
|
|
|
|
|
<pre><code>import "json" for Json
|
|
|
|
|
|
|
|
|
|
var users = Json.parse('[
|
|
|
|
|
{"firstName": "Alice", "lastName": "Smith"},
|
|
|
|
|
{"firstName": "Bob", "lastName": "Jones"}
|
|
|
|
|
]')
|
|
|
|
|
|
|
|
|
|
var names = []
|
|
|
|
|
for (user in users) {
|
|
|
|
|
names.add("%(user["firstName"]) %(user["lastName"])")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.print(names.join(", "))</code></pre>
|
|
|
|
|
|
|
|
|
|
<h2>Merge JSON Objects</h2>
|
|
|
|
|
<pre><code>import "json" for Json
|
|
|
|
|
|
|
|
|
|
var defaults = {"theme": "light", "language": "en", "timeout": 30}
|
|
|
|
|
var userPrefs = {"theme": "dark"}
|
|
|
|
|
|
|
|
|
|
var config = {}
|
|
|
|
|
for (key in defaults.keys) {
|
|
|
|
|
config[key] = defaults[key]
|
|
|
|
|
}
|
|
|
|
|
for (key in userPrefs.keys) {
|
|
|
|
|
config[key] = userPrefs[key]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.print(Json.stringify(config, 2))</code></pre>
|
|
|
|
|
|
|
|
|
|
<div class="admonition tip">
|
|
|
|
|
<div class="admonition-title">See Also</div>
|
|
|
|
|
<p>For full API documentation, see the <a href="../api/json.html">JSON module reference</a>.</p>
|
|
|
|
|
</div>
|
|
|
|
|
</article>
|
|
|
|
|
|
|
|
|
|
<footer class="page-footer">
|
|
|
|
|
<a href="http-requests.html" class="prev">HTTP Requests</a>
|
|
|
|
|
<a href="regex-patterns.html" class="next">Regex Patterns</a>
|
|
|
|
|
</footer>
|
|
|
|
|
</main>
|
|
|
|
|
</div>
|
|
|
|
|
<script src="../js/main.js"></script>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|