Convert a Java properties file to JSON (with JavaScript).
npm install js-properties-to-jsonConvert Java .properties files to JSON (using JavaScript).
supports array creation.
The function propertiesToJSON takes a string and returns
a JavaScript object.
```
propertiesToJSON(data, {
jsonAsString: false,
convertToJsonTree : false,
parseNumber:false,
parseBooleanNullUndefined:false,
});
`js
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));
}
});
`
`js
import propertiesToJSON from 'js-properties-to-json';
const propsFile = new Request('
const props = fetch(propsFile)
.then((response) => {
return response.text();
})
.then((text) => {
const propsText = propertiesToJSON(text);
console.log(propsText);
return propsText;
});
`
| Option | Default value | Description |
| :-----------------------: | :----------------: | :------------------------------------------------------------------------------------------: |
| jsonAsString | false | return json as a string |
| convertToJsonTree | false | convert properties to json tree eg a.b=c to { "a.b": c } if false or { "a": {"b": c} } |a=1
| parseNumber | false | parse value to number e.g - to { a: "1" } or { a: 1 } |null
| parseBooleanNullUndefined | false | parse string value of , true, false, undefined |
| Properties | JSON |
| ----------- | ---------------------------------- |
| a=b | { "a": "b" } |a[1][1]=b
| | { "a": [ null, [ null, "b" ] ] } |
1. npm i js-properties-to-json`
https://github.com/ryanpcmcquen/propertiesToJSON
https://github.com/jeanpaulattard/json-to-properties