0.1.0 • Published 12 months ago

js-properties-to-json v0.1.0

Weekly downloads
-
License
MIT License
Repository
github
Last release
12 months ago

Convert Java .properties files to JSON (using JavaScript).

supports array creation.

The function propertiesToJSON takes a string and returns a JavaScript object.

use

 propertiesToJSON(data, {
                jsonAsString: false,
                convertToJsonTree : false,
                parseNumber:false,
                parseBooleanNullUndefined:false,
            });

Read a local file in node:

const fs = require('fs');
const path = require('path');
const filePath = path.join(__dirname, 'sample.properties');
const propertiesToJSON = require('properties-to-json');;

fs.readFile(filePath, { encoding: 'utf-8' }, (err, data) => {
    if (!err) {
        console.log(propertiesToJSON(data));
    }
});

Read a remote file in the browser:

import propertiesToJSON from 'js-properties-to-json';

const propsFile = new Request('<link-to-properties-file>');

const props = fetch(propsFile)
    .then((response) => {
        return response.text();
    })
    .then((text) => {
        const propsText = propertiesToJSON(text);
        console.log(propsText);
        return propsText;
    });

Available options

OptionDefault valueDescription
jsonAsStringfalsereturn json as a string
convertToJsonTreefalseconvert properties to json tree eg a.b=c to { "a.b": c } if false or { "a": {"b": c} }
parseNumberfalseparse value to number e.g - a=1 to { a: "1" } or { a: 1 }
parseBooleanNullUndefinedfalseparse string value of null, true, false, undefined

Examples

PropertiesJSON
a=b{ "a": "b" }
a[1][1]=b{ "a": [ null, [ null, "b" ] ] }

How do I get it?

  1. npm i js-properties-to-json

online converter

link

inspiration

https://github.com/ryanpcmcquen/propertiesToJSON https://github.com/jeanpaulattard/json-to-properties

0.1.0

12 months ago

0.0.5

2 years ago

0.0.6

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.4

2 years ago

0.0.1

2 years ago