gojsonata

package module
v0.0.0-...-5127fcc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2019 License: MIT Imports: 8 Imported by: 0

README

gojsonata

Execute JSONata query and transformation expressions in Golang.

Problem Statement

We want a generic solution for transfomation of structured data in Golang. XSLT is the most advanced solution for transforming XML data, but it is not well supported by Golang. JSONata is an elegant solution for transforming JSON data, but it supports only JavaScripts.

This project describes how to execute JSONata transformation expressions in Golang. Following are the tools used to make it work.

  • otto - A JavaScript interpreter in Go. It supports only ES5 syntax.
  • JSONata - query and transformation language for JSON.
  • traceur - JavaScript transpiler for generating ES5 code from ES6+ code.
  • go-bindata - Go utility for converting any file into Golang source code.

We provide scripts to transpile JSONata library to ES5, and then convert the resulting files into Golang source code.

We then implement a Golang utility that you can use to transform any JSON data using JSONata expression. This utility creates an otto JavaScript interpreter, and preloads JSONata libraries when the utility is loaded. Thus, any complex data transformation request can be handled by a simple function call with input JSON data and an JSONata expression.

Installation

Install Go version 1.11.x and set GOPATH environment variable.

Install GNU make. For ubuntu, e.g., use the following commands

sudo apt update
sudo apt install make
sudo apt install make-guile

Install Node.js and npm. For ubuntu, e.g., use the following command

sudo apt install nodejs npm

Clone this project, then setup and test it as follows:

export PATH=${GOPATH}/bin:${PATH}
go get -u -d github.com/yxuco/gojsonata
cd ${GOPATH}/src/github.com/yxuco/gojsonata
make

Examples

Following Golang example shows the execution of a JSONata expression vehicle-expr.txt on the input data vehicle.json, which created an output result of different schema.

	data, _ := ioutil.ReadFile("./tests/vehicle.json")
	expr, _ := ioutil.ReadFile("./tests/vehicle-expr.txt")
	result, _ := Transform(string(data), string(expr))
	fmt.Printf("result value: %s\n", result)

A simpler transformation can also be done as follows:

	value, _ := RunScript(
		`var data = {
		  example: [
		    {value: 4},
		    {value: 7},
		    {value: 13}
		  ]
	    };
	  
	    var expression = jsonata('$sum(example.value)');
	    expression.evaluate(data);`)
	result, _ := value.ToInteger()  // result = 24

The file jsengine_test.go shows more Golang examples.

You may also use the motto command-line tool to test JSONata transformations, which is demonstrated by the following commands in the Makefile.

    make motto

For quick interactive testing of JSONata scripts, you can use http://try.jsonata.org/ to edit JSON data and JSONata scripts, and view the results immediately.

Documentation

Overview

Package gojsonata provides utility functions to execute JSONata expression for JSON data transformations

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddModuleFile

func AddModuleFile(filename string) error

AddModuleFile loads content of a js module file and add the result to cache, so later script evaluation can use this module

func AddModuleSource

func AddModuleSource(key, source string) error

AddModuleSource loads source code of a js module file and add the result to cache, so later script evaluation can use this module

func CachedModules

func CachedModules() []string

CachedModules returns names of js modules currently loaded in the engine

func CallScript

func CallScript(source, result string) (otto.Value, error)

CallScript evaluates a js script, and return value of specified result var It checks if all required modules are loaded in cache

func CallScriptFile

func CallScriptFile(filename, result string) (otto.Value, error)

CallScriptFile evaluates content of js or json file, and return value of specified result var the file extension must be .js or .json

func RunScript

func RunScript(source interface{}) (otto.Value, error)

RunScript executes js script and returns the result It checks if all required modules are loaded in cache

func Transform

func Transform(data, expr string) (string, error)

Transform executes JSONata expression on input JSON data, and returns the transformation result

Types

type Engine

type Engine struct {
	// it is based on otto
	*otto.Otto
	// contains filtered or unexported fields
}

Engine is modular vm environment

func NewEngine

func NewEngine() *Engine

NewEngine creates a new otto vm instance.

func (*Engine) LoadModule

func (m *Engine) LoadModule(source string) (otto.Value, error)

LoadModule loads source code of a js module. It recursively loads dependent modules required by the source code. All dependent modules' source code should be preloaded in jsdata, or loaded before this module. Note: this implementation is based on https://github.com/ddliu/motto/blob/master/module.go

func (*Engine) Require

func (m *Engine) Require(name string) (otto.Value, error)

Require implements js 'require(name)' for modeules. It first check if the module is already in cache, then check if it is a preloaded resource in jsdata. It returns error if source code of the specified module is not found. Note: input name may contains file path info, e.g., ./js/mycode.js, but the cache key will use only the file name without suffix, i.e., mycode => value

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL