Skip to main content

jsn

tests build-release publish-pypi codecov PyPI Version License: MIT

jsn is a user-friendly data format that can be easily read and reliably edited by humans.

It adds powerful features such as inheritance, variables, includes and syntax improvements to make jsn files more compact and re-usable than a json counterpart, it is an ideal solution for multi-platform build configuration, packaging and content buidling pipelines.

jsn can be used directly in python as a dictionary, or it can be converted to json to be used with other languages and tools and libraries which have json support.

If you are using hand edited json in any projects currently, jsn will easily integrate into your existing workflow and improve efficiency and reliability.

Features

  • Includes / file import.
  • Inheritance (hierarchicle and multiple).
  • Environment style scoped variables.
  • Quoteless keys.
  • Quoteless strings.
  • Single quotes.
  • Single and multi-line comments.
  • Multi-line strings / line breaks.
  • Hex, binary, bit shifts, int and float improvements.
  • TextMate langauge included for enhanced syntax highlighting.

CLI

You can convert jsn to json using the commandline, clone this repository and add jsn to your path.

jsn -i example.jsn -o example.json -I import_dir
jsn -help
--------------------------------------------------------------------------------
jsn ----------------------------------------------------------------------------
--------------------------------------------------------------------------------
commandline arguments:
    -help display this message
    -i list of input files or directories to process
    -o output file or directory 
    -I list of import directories, to search for imports
    -p print output to console
    -keep_vars keep jsn_vars in the output json

Python API

Install via pip:

python3 -m pip install jsn

Or alternatively copy jsn.py where you need it.

jsn can be used just like json as a python dictionary.

import jsn
json_dict = jsn.loads(open("jsn_file.jsn", "r").read())

Releases

You can install jsn as a binary release found along with this repository here

Example .jsn

import import.jsn
import test.jsn
{   
    // sytax highlights quite nicely in most editors with c or c++ syntax
    // jsonnet highlights nicely in GitHub
    // TextMate grammar included for visual studio, vscode or other compatible editors
    
    // allow comments
    
    /*
    multi-
    line 
    comments
    */
    
    //        tabs and any whitespace allowed

    // compatible with json
    "json":
    {
        "bool": true,
        "int": 1,
        "float": 1.0,
        "string": "yes",
        "array": [1, 2, 3]
    },
    
    // compatible with json5
    json5:
    {
        unquoted: 'and you can quote me on that',
        single_quotes: 'I can use "double quotes" here',
        hexadecimal: 0xdecaf,
        line_breaks: "Look, Mom! No \
\\n's!",
       leading_decimal_point: .8675309, and_trailing: 8675309.,
       positive_sign: +1,
       trailing_comma: 'in objects', and_in: ['arrays',],
    },
    
    // jsn features
    jsn:
    {
    	// ditch the quotes and commas!
        unquoted_string: without_whitespace // cannot contain whitespace or special chars (see str_test)
        unquoted: [strings, in, arrays]
        binary_literal: 0b10011
        bit_shifts: 1<<16 | 1<<8
        
        // mixed newline and commas
        object_members_separated_by_newline: {
            no_commas: yes
            with_new_lines: "if you like"
            but: "you can", still: "use them here"
        }
        arrays_elements_separated_by_newline:[
            "no need for commas"
            "if you use newlines"
            "still", "separate", "using commas", "on the same line"
    	]

        // you can define variables to be re-used
        jsn_vars: 
        {
            data: "path/to/data"
            var_str: "hello"
            var_int: 10
        }
        
        // evaluate variables with ${} inside quotes..
        variable_data_path: "${data}/subdir"
        variable_int: "${var_int}"
        array_of_vars: ["${data}", "${var_str}"]

        // you can use special variables:
        // inject the current script directory into a string
        // - this is the directory name of the file this variable is used in
        script_directory: "${script_dir}" 
        
        // subobjects can be merged and inherited recursively see ** inheritence(jsn)
        base: "foo"
        sub_object:
        {
            one: "1"
            two: "2"
            nested_var: "${var_int}" // variable comes from outer scope.
        }
        
        // use <windows, mac, linux> in angled brackets to conditionally include or exclude keys
        // the platform specific keys will be merged into the base key if one exists
        platform:
        {
            base: "exists"
        }
        
        // useful for selecting platform specific paths and executables
        platform<windows>:
        {
            exe: "path/windows/program.exe"
        }
        
        platform<mac>:
        {
            exe: "path/mac/program"
        }
    },
    
    //** 
    inheritence(jsn): // add object name to inherit inside parenthesis
    {        
        // inheritance adds keys from 'jsn' object
        // ..
        
        // duplicated keys are overridden by the derived object
        base: "bar",
        
        // inheritance on sub-objects continues recursively
        sub_object:
        {
            three: "3"
            //..
        }
    },
    
    // multiple and hierarchical inheritance
    objb: { b: "b" },
    
    multiple_inheritence(inheritence, objb):
    {
        // vars can also be shadowed / overriden..
        jsn_vars: 
        {
            data: "another/path/to/data",
            var_int: 22
        },
        
        c: "c"
    },
    
    xxx:
    {
        // some test cases
        empty_object: {},

        empty_array: [],

        array_of_objects:[
            {object: 1, other: "value"},
            {object: 2, other: "value"}
        ],
        
        array_with_string_commas:[
            "test,2",
            "test,3"
        ],
        
        nested_objects:
        {
            yes: true,
            and:
            {
                deeper: nesting
            }
        },
        
        multi_type_arrays:[
            1,
            [2, 3]
        ],
        
        array_of_arrays:[
            [hello, world],
            [goodbye, world]
        ],

        array_of_values:[
            +255,
            0b11111111,
            0xff,
            1 << 1,
            1 << 2 | 1,
            1 << 2 | 1,
            .255
        ],
        
        jsn_vars: 
        {
            va: "path/to/data",
            vb: "hello",
        },

        value: null,
        array_of_null: [null, null, null]
	
        array_of_array_vars: [
            ["${va}", "${vb}"],
            ["${vb}", "non var"]
        ],
        
        multiple_vars: "${va}/${vb}.bin",
        
        q1: "small 'quotes' inside",
        q2: 'double "quotes" inside',
        q3: "double escaped \"quotes\" inside"
    },
    
    //**
    str_test: ":[{}]'+.,0b0x" // this tests ignoring special chars inside quotes
}

Metadata

Release files for jsn 1.3.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jsn 1.3.4
File Size Uploaded
jsn-1.3.4.tar.gz 12.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jsn 1.3.4
File Interpreter ABI Platform
jsn-1.3.4-py3-none-any.whl Python 3 none any Details

Total release size: 23.1 kB

Release files / jsn-1.3.4.tar.gz

Download URL jsn-1.3.4.tar.gz
Size 12.8 kB
Tags Source
SHA-256 checksum
How to use checksums
5fbded5b2e2d69871fcbaaefc57011066555ebbc100b05a9ef379e80f15ae98a
BLAKE2b-256 checksum
How to use checksums
1ef5f50300c9c59f39fc75a86ca7d8c3ee9c07584aef1f41eec7f4cf76efd0c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / jsn-1.3.4-py3-none-any.whl

Download URL jsn-1.3.4-py3-none-any.whl
Size 10.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0ee176eb200c186156d9164174a9e3a1f11d53af6ec9b23e78c74ee8f4723955
BLAKE2b-256 checksum
How to use checksums
8973cdea768cce50ec326e547381346ce6b4bf5bc2803a90e50472fc99516205
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release history Release notifications | RSS feed

This release

1.3.4 This release

2 release files

1.3.3

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.9

2 release files

1.1.8

2 release files

1.1.7

2 release files

1.1.6

2 release files

1.1.5

2 release files

1.1.4

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.10

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.3

2 release files

1.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page