Git Product home page Git Product logo

ld2json's Introduction

LD2JSON / JSON2LD

ld2json is a utility to convert a Line-oriented Dataset (LD) formatted document to JSON/JSONL. Input is taken from stdin and is written to stdout. Error and debug messages are written to stderr. You can specify an alternative input file on the command line: ld2json input_filename. You can also use the -p option to output a formatted JSON string. Of course, since JSONL is line-oriented, this option will result in an invalid JSONL file.

Example: cat test.ld | ./ld2json

The LD format is designed to make it easier to hand-create datasets. The format can easily be translated to plain JSON or JSONL, while being relatively easy to create. No need to worry about defining large JSON objects by hand. No need to worry about column separators and escaping CSV files.

LD is line-oriented. Keys are defined on a line. The following lines contain the data that should be assigned to that key. The data section ends at the next key.

Blank lines are not output. New lines in the input are removed in the output. To include a newline in the output, use the two characters \n.

json2ld is the reverse process. It converts JSON and JSONL files to ld format.

Details

LD generates JSON objects with keys that have values. Here is an example:

~~:{
~~:$string_key
Value of the string key.
~~:#number_key
1234
~~:!null_value_key
null
~~:?boolean_key
true
~~:}

This produces the following JSONL output:

{"string_key":"Value of the string key.","number_key":1234,"null_value_key":null,"boolean_key":true}

Objects can also have child objects and arrays:

~~:{
~~:[test_array
~~:$
one
~~:$
two
~~:$
three
~~:]
~~:{child_object
~~:$field1
Field 1
~~:$field2
Field 2
~~:}
~~:}

This produces the following JSONL output:

{"test_array":["one","two","three"],"child_object":{"field1":"Field 1", "field2":"Field 2"}}

You can also put comments in the LD file. These are ignored and not output:

~~~:* Some comment text
You can have comment text here too, if you want.

To output JSON instead of JSONL, you start off with a single anonymous object or array at the top level:

Object:

~~:{
~~:}

Array:

~~:[
~~:]

LD data can be indented without indenting the output. If a key field starts with a number of spaces, that number of spaces is removed frome the beginning of each line of data:

~~:{
~~:$some_key
Some key.
~~:{child_object
    ~~:$key
    Data for this key is indented by four spaces, for readability.
~~:}
~~:$another_key
    In this case, the key specification isn't indented, so the four spaces at\n
    the start of these lines is kept.
~~:}

The output would be:

{"some_key":"Some key.","child_object":{"key":"Data for this key is indented by four spaces, for readability."},"another_key":" In this case, the key specification isn't indented, so the four spaces at\n the start of these lines is kept."}

Except for the aforementioned indentation feature, the complete removal of blank lines and the stripping of ending line feeds (and carriage returns), no formatting is done on the output data or key names. They are output as-is.

ld2json's People

Contributors

yoppeh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.