1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
---
layout: default
title: Getting Started
---
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<h1 id="getting-started">Getting Started</h1>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<p>
While the <a href="/learning/tutorial.html">tutorial</a> is the introduction to the language
itself, this page helps you get started with the implementations. If you want to link
Jsonnet as a library, see <a href="/ref/bindings.html">bindings</a>.
</p>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<h2 id="interpreters">Interpreters</h2>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<p>
The easiest way to execute some Jsonnet code is invoking an interpreter via the commandline
as so:
</p>
<pre>jsonnet -e <code></pre>
<p>
Or a filename:
</p>
<pre>jsonnet <file></pre>
<p>
This dumps the JSON on stdout. The tool is self-documenting with <tt>--help</tt>. You can
experiment with the Jsonnet files in the <a
href="https://github.com/google/jsonnet/tree/master/examples">examples/</a> directory of the
C++ Github repo.
</p>
<p>
To get Jsonnet, you can build it from either the <a
href="https://github.com/google/jsonnet">C++</a> or the <a
href="https://github.com/google/go-jsonnet">Go</a> repositories. See below for the
differences between them. Each repo has a README.md containing build instructions.
</p>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<h3 id="example">Example</h3>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<p>Evaluating a file.</p>
<pre>$ jsonnet landingpage.jsonnet
{
"person1": {
"name": "Alice",
"welcome": "Hello Alice!"
},
"person2": {
"name": "Bob",
"welcome": "Hello Bob!"
}
}</pre>
<p>Evaluating a snippet.</p>
<pre>$ jsonnet -e '{ x: 1 , y: self.x + 1 } { x: 10 }'
{
"x": 10,
"y": 11
}</pre>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<h3 id="multi">Multiple File Output</h3>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<p>
The Jsonnet commandline tool has a special mode for generating multiple JSON files from a
single Jsonnet file. This can be useful if you want to avoid writing lots of small Jsonnet
files, or if you want to take advantage of cross-references and interdependencies between
the files. The idea is to create a single JSON structure, the top level of which defines
the various files:
</p>
<pre>// multiple_output.jsonnet
{
"a.json": {
x: 1,
y: $["b.json"].y,
},
"b.json": {
x: $["a.json"].x,
y: 2,
},
}</pre>
<p>
When executed using <tt>jsonnet -m <dir></tt>, this will write the generated JSON to
files <tt>a.json</tt> and <tt>b.json</tt> in the given directory, instead of the whole thing
being written to stdout. In order to integrate nicely with build tools like <tt>make</tt>,
the files are not touched if they already contain the given content. To stdout is printed
the list of target files, one per line. This makes it easy to drive other tools that
operate on the JSON files, e.g. via <tt>xarg</tt>.
</p>
<pre>$ jsonnet -m . multiple_output.jsonnet
a.json
b.json
$ cat a.json
{
"x": 1,
"y": 2
}
$ cat b.json
{
"x": 1,
"y": 2
}</pre>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<h3 id="stream">YAML Stream Output</h3>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<p>
Unlike JSON, YAML can represent several objects in the same file, separated by <tt>---</tt>.
The Jsonnet commandline parameter <tt>-y</tt> causes the tool to expect the Jsonnet
execution to yield an array. Config designed for this mode typically looks like this:
It then outputs that array as a sequence of JSON documents
separated by <tt>---</tt>, which any YAML parser will interpret as a YAML stream.
</p>
<pre>// yaml_stream.jsonnet
local
a = {
x: 1,
y: b.y,
},
b = {
x: a.x,
y: 2,
};
[a, b]</pre>
<p>
When executed using <tt>-y</tt>, this will output that array as a sequence of JSON documents
separated by <tt>---</tt> and terminated with <tt>...</tt>. Any YAML parser <i>should</i>
interpret this as a YAML stream (people have reported broken parsers, so try it out first).
</p>
<pre>$ jsonnet -y . yaml_stream.jsonnet
---
{
"x": 1,
"y": 2
}
---
{
"x": 1,
"y": 2
}
...</pre>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<h2 id="cpp-or-go">C++ or Go?</h2>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<p>
There are two feature-compatible implementations of the Jsonnet interpreter. The first one
we built was the C++ interpreter, and it is still the most mature / widely used. The new Go
implementation has the benefits of simpler code (due to leveraging goroutines and the GO
garbage collector). Our long term plan is to migrate everyone to the Go implementation
eventually. Both implementations are tested against the same test suite (which is quite
thorough). Please report any deviations. The performance of the C++ implementation is
currently a bit better, but for most people this should not be a factor and we're intending
to address it over time, anyway.
</p>
<p>
Another factor is that the formatter is currently only implemented in the C++ repo, and the
linter is only implemented in the Go repo.
</p>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<h2 id="javascript">JavaScript</h2>
</div>
<div style="clear: both"></div>
</div>
</div>
<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<p>
The C++ implementation can be compiled with <a
href="http://kripken.github.io/emscripten-site/">emscripten</a> to produce a JavaScript
implementation of Jsonnet. This is how we implement the interactive demos on this site. A
native implementation of Jsonnet in JavaScript would probably be faster. In fact, a
GopherJs transpile of the Go Jsonnet implementation may work better as well. However, the
emscripten version is easy to build, and is sufficient for many purposes.
</p>
<p>
To compile it, first <a
href="http://kripken.github.io/emscripten-site/docs/getting_started/downloads.html">install
</a> emscripten and ensure em++ and emcc are in your path. Then make libjsonnet.js.
</p>
<p>
An unofficial <a href="https://github.com/yosuke-furukawa/node-jsonnet">nodejs package</a>
of Jsonnet is also available.
</p>
</div>
<div style="clear: both"></div>
</div>
</div>
|