A convenient syntax to generate JSON (or YAML) for commandline

clon

A convenient syntax to generate JSON (or YAML) for commandline "mumbo-jumbo".

Syntax Overview

Syntax resembles that of JSON with a few caveats:

  • a field is a key/value pair
  • fields are separated by space (one or more)
  • curly braces hold objects
  • square brackets hold arrays

fields

A field is defined by: IDENTIFIER = VALUE .

  • field key/value pairs have a equal = between them as in key = value
  • each field is separated by space (one or more does not matter)
$ clon firstName = Scarlett lastName = Johansson

generates...

{
   "firstName": "Scarlett",
   "lastName": "Johansson"
}
  • values are treated as strings by default
  • other types (numbers, booleans, null) need to be prefixed with a colon :
    • es. age = :30 customer = :true
  • to refer to an environment variable prefix the value with a ^
    • es. bucket = ^S3_BUCKET
    • by default the .env file in the current folder is used, use the -env-file flag to change it)
$ clon fullName = \"Scarlett Johansson\" age = :36 hot = :true

generates...

{
   "age": 36,
   "fullName": "Scarlett Johansson",
   "hot": true
}

objects

An object is defined by: IDENTIFIER = { fields... }.

  • begin a new object using the left curly brace {
  • close the object with a right curly brace }
$ clon user = { name=foo age=:30 active=:true address = { zip=123 country=IT } }

generates...

{
   "user": {
      "active": true,
      "address": {
         "country": "IT",
         "zip": "123"
      },
      "age": 30,
      "name": "foo"
   }
}
  • you can also use dotted notation (and mix things)
$ clon user = { name=foo age=:30 active=:true address.zip=123 address.country=IT }
$ clon user.name=foo user.age=:30 user.active=:true user.address = {zip=123 country=IT}
$ clon user.name=foo user.age=:30 user.active=:true user.address.zip=123 user.address.country=IT

are all examples that generate the same JSON as above; it's up to you to find your way.

arrays

An array is defined by: IDENTIFIER = [ fields...].

  • begin a new array using the left square brace [
  • end the array with a right quare brace ]
$ clon tags = [ foo bar qix ]
{
   "tags": [
      "foo",
      "bar",
      "qix"
   ]
}

You can create an array of object too:

$ clon pets = [ { name=Dash kind=cat age=:3 } {name=Harley kind=dog age=:4} ]
{
   "pets": [
      {
         "age": 3,
         "kind": "cat",
         "name": "Dash"
      },
      {
         "age": 4,
         "kind": "dog",
         "name": "Harley"
      }
   ]
}

Usage

$ clon user = { name=foo age=:30 type=C address.zip=123 address.country=Italy }

generates...

{
   "user": {
      "address": {
         "country": "Italy",
         "zip": "123"
      },
      "age": 30,
      "name": "foo",
      "type": "C"
   }
}

Use Cases ?

Create JSON payload and POST it with cURL

$ clon user = { name=Pinco age=:30 address = { zip=123 country=CA } } \
  | curl -H "Content-Type: application/json" \
         -X POST --data-binary @- \
         https://httpbin.org/anything

Elasticsearch query string query

$ clon query.query_string.query = \"new york city\" \
  | curl -H "Content-Type: application/json" \
         --data-binary @- http://localhost:9200/_search

How to install?

In order to use the map command, compile it using the following command:

go get -u github.com/lucasepe/map

This will create the executable under your $GOPATH/bin directory.

Ready-To-Use Releases

If you don't want to compile the sourcecode yourself, Here you can find the tool already compiled for:

  • MacOS
  • Linux
  • Windows

Credits

Thanks to @jawher for the amazing jg idea and original implementation - this is a modified fork.

What I changed?

  • lexer and parser now can resolve attribute values ​​from environment variables
  • dotenv files support
  • additional YAML format encoding
  • go modules support
Owner
Luca Sepe
Software Craftsman [Golang / CLI tools].
Luca Sepe
Similar Resources

Abstract JSON for golang with JSONPath support

Abstract JSON Abstract JSON is a small golang package provides a parser for JSON with support of JSONPath, in case when you are not sure in its struct

Jan 5, 2023

Fast JSON parser and validator for Go. No custom structs, no code generation, no reflection

fastjson - fast JSON parser and validator for Go Features Fast. As usual, up to 15x faster than the standard encoding/json. See benchmarks. Parses arb

Jan 5, 2023

Small utility to create JSON objects

Small utility to create JSON objects

gjo Small utility to create JSON objects. This was inspired by jpmens/jo. Support OS Mac Linux Windows Requirements Go 1.1.14~ Git Installtion Build $

Dec 8, 2022

A Go package for handling common HTTP JSON responses.

go-respond A Go package for handling common HTTP JSON responses. Installation go get github.com/nicklaw5/go-respond Usage The goal of go-respond is to

Sep 26, 2022

JSON query in Golang

gojq JSON query in Golang. Install go get -u github.com/elgs/gojq This library serves three purposes: makes parsing JSON configuration file much easie

Dec 28, 2022

A JSON diff utility

JayDiff A JSON diff utility. Install Downloading the compiled binary Download the latest version of the binary: releases extract the archive and place

Dec 11, 2022

Fast and flexible JSON encoder for Go

Fast and flexible JSON encoder for Go

Jettison Jettison is a fast and flexible JSON encoder for the Go programming language, inspired by bet365/jingo, with a richer features set, aiming at

Dec 21, 2022

Create go type representation from json

json2go Package json2go provides utilities for creating go type representation from json inputs. Json2go can be used in various ways: CLI tool Web pag

Dec 26, 2022

Console JSON formatter with query feature

Console JSON formatter with query feature

Console JSON formatter with query feature. Install: $ go get github.com/miolini/jsonf Usage: Usage of jsonf: -c=true: colorize output -d=false: de

Dec 4, 2022
Comments
  • Release ARM Binaries

    Release ARM Binaries

    again, please could you include deb packages/binaries for ARM7 and ARM64 in your releases for this. Amazing work! I love both this and 'uri' tool. you've made working with API data from the command line a breeze !!!

/ˈdʏf/ - diff tool for YAML files, and sometimes JSON
/ˈdʏf/ - diff tool for YAML files, and sometimes JSON

dyff is inspired by the way the old BOSH v1 deployment output reported changes from one version to another by only showing the parts of a YAML file that change.

Dec 29, 2022
A simple Go package to Query over JSON/YAML/XML/CSV Data
A simple Go package to Query over JSON/YAML/XML/CSV Data

A simple Go package to Query over JSON Data. It provides simple, elegant and fast ODM like API to access, query JSON document Installation Install the

Dec 27, 2022
Automatically generate Go (golang) struct definitions from example JSON

gojson gojson generates go struct definitions from json or yaml documents. Example $ curl -s https://api.github.com/repos/chimeracoder/gojson | gojson

Jan 1, 2023
Generate Jsonnet definition for JSON representation of protobuf object

Generate Jsonnet definition for JSON representation of protobuf object

Nov 1, 2021
Get JSON values quickly - JSON parser for Go
Get JSON values quickly - JSON parser for Go

get json values quickly GJSON is a Go package that provides a fast and simple way to get values from a json document. It has features such as one line

Dec 28, 2022
JSON diff library for Go based on RFC6902 (JSON Patch)

jsondiff jsondiff is a Go package for computing the diff between two JSON documents as a series of RFC6902 (JSON Patch) operations, which is particula

Dec 4, 2022
Fast JSON encoder/decoder compatible with encoding/json for Go
Fast JSON encoder/decoder compatible with encoding/json for Go

Fast JSON encoder/decoder compatible with encoding/json for Go

Jan 6, 2023
Package json implements encoding and decoding of JSON as defined in RFC 7159

Package json implements encoding and decoding of JSON as defined in RFC 7159. The mapping between JSON and Go values is described in the documentation for the Marshal and Unmarshal functions

Jun 26, 2022
Json-go - CLI to convert JSON to go and vice versa
Json-go - CLI to convert JSON to go and vice versa

Json To Go Struct CLI Install Go version 1.17 go install github.com/samit22/js

Jul 29, 2022
JSON Spanner - A Go package that provides a fast and simple way to filter or transform a json document

JSON SPANNER JSON Spanner is a Go package that provides a fast and simple way to

Sep 14, 2022