My Profile Photo

Alexandra da Silva Pereira


I like programming a lot, fixing bugs, learning new technologies, real world problems and solving them, development and real world software engineering. My best motivation is using technology to produce helpful and useful products and services.


Using JSON-e

Using JSON-e: contexts and templates

I’ve started my work at Mozilla in this Outreachy round making contributions in JSON-e. That I’ve told (very quickly) in my first post, now I will talk with more details about how the project works and focus on its use.

Using and seeing JSON-e applications I really believe that it was proposed to solve a problem of parsing data structures into more easily editable and tratable data structures in a specific context. JSON-e is present as a solution to manipulate JSON and YAML files (more used formats to translate/manipulate objects in programs) and include any kind of necessary parametrization configuration. It is easy to use for people with familiarity with languages such as Python and Javascript, but there is a Golang client as well :)

You first need download the lib, and then… import it into your program o/

Some practical examples in JS language (this was the official language of my intership, most of the problems and bug were solved with this language) :

import jsone from 'json-e'; => import the lib

var template = {a: {$eval: "foo.bar"}}; => your template.
var context = {foo: {bar: "zoo"}}; => context that you want parametrize. 
console.log(jsone(template, context)); => magic method that performs the parametrization from your context to a specific template.
// -> { a: 'zoo' } => waited result.:)

for more complex operations, there are operators like: json, if, then, else, let, merge, sort, etc . An important point is the character $, which marks the beginning of a JSON-e command.

Other JSON-e examples:

*if, then, else* 

import jsone from 'json-e';

var template = {key: {$if: 'cond', then: 1}, k2: 3};
var context = {cond: true};
console.log(jsone(template, context));   
// ->{key: 1, k2: 3}
*let*

import json from 'json-e';

var template = {$let: {ts: 100, foo: 200},
           in: [{$eval: "ts+foo"}, {$eval: "ts-foo"}, {$eval: "ts*foo"}]};
var context =  {};
console.log(jsone(template, context));   
// -> [300, -100, 20000]
*merge*

import json from 'json-e';

var template = {$merge: [{a: 1, b: 1}, {b: 2, c: 3}, {d: 4}]}
var context = {}
console.log(jsone(template, context));
// -> {a: 1, b: 2, c: 3, d: 4}

References:

JSON-e official page

JSON-e official repo

Jonas’s* blog

*Jonas is a software engineer in Mozilla contributing for and maintaining the Taskcluster team.