npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pt2json

v2.0.0

Published

Convert a plain text config file to JSON and vice versa

Downloads

30

Readme

Plain Text to Json

This node module converts a plain text config file to JSON format and vice versa.

my_property = my_value

my_array = value1
my_array = value2
{
    "my_property": "my_value",
    "my_array": [
        "value1",
        "value2"
    ]
}

Why?

Some config files are formatted as plain text format (e.g. Linux interfaces, Windows hosts, some game configuration files, ...) and may be difficult to interact with using scripts. Converting them to JSON objects allows to easily edit values and save them back to the original file format.

Examples

How to use

Install

npm i pt2json

Import module

const { pt2json, json2pt } = require("pt2json");

Convert Plain Text to JSON

const myJson = pt2json("my_key=my_value");
// { "my_key": "my_value" }

Convert JSON to Plain Text

const myPlainText = json2pt({ "my_key": "my_value" });
// my_key=my_value

Editing a value

const data = `key value`;

const jsonObj = pt2json(data);
jsonObj.key = `"Hello World"`;

json2pt(jsonObj);
// key "Hello World"

Pushing to an array

When multiple keys are found at the same level and with the same name, it creates an array. Here, an array labeled as start will contain ["example1", "example2"]. Because it's an array we can easily add or remove items in it:

const data = `start=example1
start=example2`;

const jsonObj = pt2json(data);
jsonObj.start.push("example3");

json2pt(jsonObj);
/*
start example1
start example2
start example3
*/

Options

Default options

pt2json

You can define options when converting from plain text to json:

// Default options
const options = {
    separator: "=", // key / value separator. Default for "key=value".
    key_separator: " ", // key / key separator. Default for "key1 key2=value".
    comments: false, // Keep comments that starts with "#". Default will not keep them.
    emptyLines: false, // Keep empty lines. Default will not keep them.
    tabs: false, // Left trim the key. Default will trim.
    indexed: false, // Keep keys order after conversion. See bellow for more informations .
};
pt2json(file, options);

json2pt

You can also define some options when converting json to plain text.

Note that some options will be automatically applied according to the json object structure:

  • comments and emptyLines will be applied if the according array are found in the json (see bellow)
  • indexed objects are kept if a value has the corresponding structure ({value: string, index: number})
  • tabs will automatically be kept if found
// Default options
const options = {
    separator: "=", // Same as pt2json separator option
    key_separator: " " // Same as pt2json key_separator option
};
json2pt(file, options);

Using custom options

Custom separator

You can specify which separator to use for key / value combo. Default separator is set to =. When using custom separator for converting plain text to json, it will be stored on the generated json.

const data = `key1 = value`; // Spaces between separator are automatically removed
pt2json(data); // { "key1": "value" }

const data = `key1,value`;
pt2json(data); // { "key,value": null } | Invalid!
pt2json(data, { separator: "," }); // { "key": "value", __params: { separator: ',' } }
//                                      --------------  -----------------------------
//                                          Valid!            Separator is stored

json2pt({"key": "value"}); // key=value
json2pt({"key": "value"}, { separator: "," }); // key,value

Composed keys

Some properties can have composed keys like:

In plain text:

key1 key2 key3 = value

In json:

{ "key1": { "key2": { "key3": "value" } } }

Pt2json allows such behaviour by creating nested objects:

const data = `key1 key2 key3 = value`;

const pt = pt2json(data); // { "key1": { "key2": { "key3": "value" } } }
pt.key1.key2.key3 = "new value";

You can also define custom key separators with the key_separator parameter:

const data = `key1-key2-key3 = value`;

const pt = pt2json(data, { "key_separator": "-" }); // { "key1": { "key2": { "key3": "value" } } }
pt.key1.key2.key3 = "new value";

json2pt({"key1":{"key2":{"key3":"value"}}}); // key1 key2 key3=value
json2pt({"key1":{"key2":{"key3":"value"}}}, { key_separator: "-" }); // key1-key2-key3=value

Comments and empty lines

Some config files can have comments or empty lines, those can be detected with the option comments and emptyLines. By default, those options are inactive thus comments and empty lines will not be kept after conversion.

When converting from plain text to json,

  • comments are stored into the __comments property array.
  • emptyLines are stored into the __emptyLines property array.

Note that both options will only work if the indexed option is set to true.

const data = `
# User infos
first_name John
last_name Doe

# User contact
# User contact
mail [email protected]
`;

const jsonObj = pt2json(data, {
    comments: true,
    emptyLines: true,
    indexed: true
});

jsonObj.__comments.splice(1, 1); // Remove duplicate comment
json2pt(jsonObj);
/*
# User infos
first_name John
last_name Doe

# User contact
mail [email protected]
*/

Custom indexes

Pt2json can keep the line order by creating an object containing its value and its index.

// Without indexes
const pt = pt2json("my_key=my_value"); // { "my_key": "my_value" }

pt.my_key = "new value";
// With indexes
// Its useless here, because we only have one key... see usefull examples on "examples/" folder
const pt = pt2json("my_key=my_value", { indexed: true }); // { "my_key": { "value": "my_value", "index": 0 } }

pt.my_key.value = "new value"; // Keep the index when converting back to plain text
pt.my_key.index = 10; // Change the index
pt.my_key = "new value"; // Legal but will destroy the index when converting back to plain text

Read and write files

You can use pt2json with fs module:

const fs = require("fs");

fs.readFile("./plain_text", "utf-8", (err, data) => {
    if (err) return;

    const jsonObj = pt2json(data);
    jsonObj.key = "value";

    fs.writeFileSync("./new_plain_text", json2pt(jsonObj));
});

Author & License

Made by Baptiste Miquel under the MIT license.