0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

jsonnet のメモ

Last updated at Posted at 2020-03-12

基本文法

リファレンス

// self = 現在の object 
self["buffers"]
self.buffers 

// $ = outer most object(= 通常は root?)
$["buffers"]
$.buffers 


フィールドが文字列の場合は ['bora'] を使う
identifier(識別子. 空白が無いなど)の場合は .bora と記載できる.

外部変数(コマンドライン引数で設定したものとか)の参照

std.extVar("bora") でアクセスできる.

外部ファイルの参照

local lib = import 'path/to/file.jsonnet';

として, lib.bora などとしてアクセスできる.

ファイルパスの起点は, カレントディレクトリではなく, import のある jsonnet ファイルのパスが起点となる.

配列アクセス

[10] としてアクセス可能
python っぽい slice 対応

comprehention(foreach 的な操作)

配列の各要素は, python のように記述できる

local arr = std::range(5, 8);
{
  item: [x + 1 for x in arr],
}

tips

逆引き Jsonnet
https://qiita.com/ktateish/items/c07d76fb268575f5a8dc

基本的にはこちれあるもの以外の tips です.

オブジェクトにフィールドがあるか確認したい.

T.B.W.

has("bora") みたいなのは直接には無いようです.

std.isObjectstd.length を組み合わせて実現?

配列の数を求める

std::length を使う.
変数に対して使う.

local buffers = [0, 1, 2],
local len = std.length(buffers)

既存の JSON オブジェクトを扱うには, リファレンスを使う

"buffers": [0, 1, 2],
local len = std.length(self.buffers) // self = 現在の object
// local len = std.length($["buffers"]) // $ は root から

配列に対して処理を行う

std.map が使える.

JSON の値を書き換える

入力データが素の JSON で, 特定のフィールドの値を書き換えたい.

T.B.W.

import してから処理すればいけるか?
(JSON で完結するなら, python や jq とかで処理するのも手)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?