Jsonnet Online Demo
This Javascript build of Jsonnet runs in your browser. There is
no filesystem, so import
will not find any files. In most browsers, the textboxes can
be made larger by dragging their bottom right corner.
Don't know what to write? The following links replace the above Jsonnet with various examples.
-
Build script example
// Compiler template
local CCompiler = {
cFlags: [],
out: "a.out",
local flags_str = std.join(" ", self.cFlags),
local files_str = std.join(" ", self.files),
cmd: "%s %s %s -o %s" % [self.compiler, flags_str, files_str, self.out],
};
// GCC specialization
local Gcc = CCompiler { compiler: "gcc" };
// Another specialization
local Clang = CCompiler { compiler: "clang" };
// Mixins - append flags
local Opt = { cFlags: super.cFlags + ["-O3", "-DNDEBUG"] };
local Dbg = { cFlags: super.cFlags + ["-g"] };
// Output:
{
targets: [
Gcc { files: ["a.c", "b.c"] },
Clang { files: ["test.c"], out: "test" },
Clang + Opt { files: ["test2.c"], out: "test2" },
Gcc + Opt + Dbg { files: ["foo.c", "bar.c"], out: "baz" },
]
}
-
Alice & Bob
// Jsonnet Example
{
person1: {
name: "Alice",
welcome: "Hello " + self.name + "!",
},
person2: self.person1 { name: "Bob" },
}
-
Tutorial code: bar_menu.1.jsonnet
/* bar_menu.1.jsonnet */
{
cocktails: {
// Ingredient quantities are in fluid ounces.
"Tom Collins": {
ingredients: [
{ kind: "Farmers Gin", qty: 1.5 },
{ kind: "Lemon", qty: 1 },
{ kind: "Simple Syrup", qty: 0.5 },
{ kind: "Soda", qty: 2 },
{ kind: "Angostura", qty: "dash" },
],
garnish: "Maraschino Cherry",
served: "Tall",
},
Manhattan: {
ingredients: [
{ kind: "Rye", qty: 2.5 },
{ kind: "Sweet Red Vermouth", qty: 1 },
{ kind: "Angostura", qty: "dash" },
],
garnish: "Maraschino Cherry",
served: "Straight Up",
},
}
}
-
Tutorial code: bar_menu.2.jsonnet
// bar_menu.2.jsonnet
{
cocktails: {
"Tom Collins": {
ingredients: [
{ kind: "Farmers Gin", qty: 1.5 },
{ kind: "Lemon", qty: 1 },
{ kind: "Simple Syrup", qty: 0.5 },
{ kind: "Soda", qty: 2 },
{ kind: "Angostura", qty: "dash" },
],
garnish: "Maraschino Cherry",
served: "Tall",
},
Martini: {
ingredients: [
{
// Evaluate a path to get the first ingredient of the Tom Collins.
kind: $.cocktails["Tom Collins"].ingredients[0].kind,
// or $["cocktails"]["Tom Collins"]["ingredients"][0]["kind"],
qty: 1
},
{ kind: "Dry White Vermouth", qty: 1 },
],
garnish: "Olive",
served: "Straight Up",
},
"Gin Martini": self.Martini,
}
}
-
Tutorial code: bar_menu.3.jsonnet
// bar_menu.3.jsonnet
{
foo: 3,
bar: 2 * self.foo, // Multiplication.
baz: "The value " + self.bar + " is "
+ (if self.bar > 5 then "large" else "small") + ".",
array: [1, 2, 3] + [4],
obj: {a: 1, b: 2} + {b: 3, c: 4},
equality: 1 == "1",
}
-
Tutorial code: example_operators.jsonnet
// example_operators.jsonnet
{
foo: [1, 2, 3],
bar: [x * x for x in self.foo if x >= 2],
baz: { ["field" + x]: x for x in self.foo },
obj: { ["foo" + "bar"]: 3 },
}
-
Tutorial code: bar_menu.5.jsonnet
// bar_menu.5.jsonnet
{
cocktails: {
"Bee's Knees": {
// Construct the ingredients by using 4/3 oz
// of each element in the given list.
ingredients: [ // Array comprehension.
{ kind: i, qty: 4/3 }
for i in ["Honey Syrup", "Lemon Juice", "Farmers Gin"]
],
garnish: "Lemon Twist",
served: "Straight Up",
},
} + { // Object comprehension.
[sd.name + "Screwdriver"]: {
ingredients: [
{ kind: "Vodka", qty: 1.5 },
{ kind: sd.fruit, qty: 3 },
],
garnish: null,
served: "On The Rocks"
} for sd in [
{name: "Yellow ", fruit: "Lemonade"},
{name: "", fruit: "Orange Juice"},
]
}
}
-
Tutorial code: bar_menu.6.jsonnet (with inlined import)
local ImportedMartinis = {
"Vodka Martini": {
ingredients: [
{ kind: "Vodka", qty: 2 },
{ kind: "Dry White Vermouth", qty: 1 },
],
garnish: "Olive",
served: "Straight Up",
},
Cosmopolitan: {
ingredients: [
{ kind: "Vodka", qty: 2 },
{ kind: "Triple Sec", qty: 0.5 },
{ kind: "Cranberry Juice", qty: 0.75 },
{ kind: "Lime Juice", qty: 0.5 },
],
garnish: "Orange Peel",
served: "Straight Up",
},
};
// bar_menu.6.jsonnet
{
cocktails: ImportedMartinis + {
Manhattan: {
ingredients: [
{ kind: "Rye", qty: 2.5 },
{ kind: "Sweet Red Vermouth", qty: 1 },
{ kind: "Angostura", qty: "dash" },
],
garnish: "Maraschino Cherry",
served: "Straight Up",
},
Cosmopolitan: {
ingredients: [
{ kind: "Vodka", qty: 1.5 },
{ kind: "Cointreau", qty: 1 },
{ kind: "Cranberry Juice", qty: 2 },
{ kind: "Lime Juice", qty: 1 },
],
garnish: "Lime Wheel",
served: "Straight Up",
},
}
}
-
Tutorial code: bar_menu.7.jsonnet (with inlined import)
// bar_menu.7.jsonnet
local utils = {
equal_parts(size, ingredients)::
if std.length(ingredients) == 0 then
error "No ingredients specified."
else [
{ kind: i, qty: size/std.length(ingredients) }
for i in ingredients
],
id:: function(x) x,
};
{
local my_gin = "Farmers Gin",
cocktails: {
"Bee's Knees": {
// Divide 4oz among the 3 ingredients.
ingredients: utils.equal_parts(4, [
"Honey Syrup", "Lemon Juice", my_gin]),
garnish: "Lemon Twist",
served: "Straight Up",
},
Negroni: {
// Divide 3oz among the 3 ingredients.
ingredients: utils.equal_parts(3, [
my_gin, "Sweet Red Vermouth",
"Campari"]),
garnish: "Orange Peel",
served: "On The Rocks",
},
}
}
-
Tutorial code: bar_menu.8.jsonnet
// bar_menu.8.jsonnet
{
cocktails: {
Negroni: {
local neg = self,
ingredients: [
{ kind: "Farmers Gin", qty: 1 },
{ kind: "Sweet Red Vermouth",
qty: neg.ingredients[0].qty },
{ kind: "Campari",
qty: neg.ingredients[0].qty },
],
garnish: "Orange Peel",
served: "On The Rocks",
},
}
}
-
Tutorial code: bar_menu.9.jsonnet
// bar_menu.9.jsonnet
{
cocktails: {
"Whiskey Sour": {
ingredients: [
{ kind: "Bourbon", qty: 1.5 },
{ kind: "Lemon Juice", qty: 1 },
{ kind: "Gomme Syrup", qty: 0.5 },
],
garnish: "Lemon Peel",
served: "Straight Up",
},
"Whiskey Sour With Egg": self["Whiskey Sour"] + {
ingredients: super.ingredients
+ [ { kind: "Egg White", qty: 0.5 } ],
},
}
}
-
Tutorial code: bar_menu.10.jsonnet (with inlined import)
// bar_menu.10.jsonnet
local Imported = {
cocktails: {
"Whiskey Sour": {
ingredients: [
{ kind: "Bourbon", qty: 1.5 },
{ kind: "Lemon Juice", qty: 1 },
{ kind: "Gomme Syrup", qty: 0.5 },
],
garnish: "Lemon Peel",
served: "Straight Up",
},
"Whiskey Sour With Egg": self["Whiskey Sour"] + {
ingredients: super.ingredients
+ [ { kind: "Egg White", qty: 0.5 } ],
},
}
};
Imported {
cocktails: super.cocktails {
"Whiskey Sour": {
ingredients: [
{ kind: "Scotch", qty: 1.5 },
{ kind: "Lemon Juice", qty: 0.75 },
],
garnish: "Lemon Peel",
served: "On The Rocks",
}
}
}
-
Silly example: Fibonacci via recursive function
local fibonacci(n) =
if n <= 1 then
1
else
fibonacci(n - 1) + fibonacci(n - 2);
fibonacci(25)
-
Silly example: Fibonacci via recursive object (CAUTION: can be slow)
local Fib = {
n: 1,
local outer = self,
r: if self.n <= 1 then 1 else (Fib { n: outer.n - 1}).r + (Fib { n: outer.n - 2}).r
};
(Fib { n: 25 }).r
Except as noted, this content is licensed under Creative Commons Attribution 2.5.