I have a filename.config.js file with contents like these:
module.exports = {
foo: [
{bar: "bar"},
],
};
Does the native Node.js or some lib for it have any tools that can add another item to foo without resorting to regular expressions?
Found a solution myself in some googled schematics:
const { cosmiconfigSync } = require('cosmiconfig');
export function getConfig(path?: string): MyConfig {
const explorer = cosmiconfigSync('filename');
if (path) {
path = `${process.cwd()}/${path}`;
}
const configSearch = explorer.search(path);
return configSearch ? configSearch.config : {};
}
Use a .json file for configurations. Never dynamically write to a js file.
filename.config.json
{
"foo": [
{"bar": "bar"},
],
}
Then in another js file you can read the file:
const fs = require('fs');
const path = require('path');
const config = JSON.parse(
fs.readFileSync(path.resolve(__dirname, './filename.config.json'), 'utf8')
);
console.log(config);
To edit the file, you can edit the object and write it back to the file:
config.biz = 'baz';
fs.writeFileSync(path.resolve(__dirname, './filename.config.json'), JSON.stringify(config));
JSON.parse and JSON.stringify can be used to convert JSON objects from a string to a real object and back. No regex required.
Related
"I'm developing an extension for Visual Studio Code that allows users to specify their own snippet names in the plugin's settings. The plugin can then use these names to create files.
I've successfully implemented the file creation functionality, but I'm still trying to figure out how to run snippets programmatically.
The following code is not fully tested yet but it illustrates in what context I would like to use the snippet.
// user config
"[extensionName]": [
["{folderName}.js", "snippetName1", "JavaScript"],
["{folderName}.styled.js", "snippetName2", "JavaScript"],
["{folderName}.translation.js", "snippetName3", "JavaScript"]
]
// extension.ts
async function createFolder (folderName: string) {
// Create the folder in the workspace
const config = await vscode.workspace.getConfiguration();
const options: Array<[string, string, string]> = config["[extensionName]"];
const selectedFolders = await vscode.window.showOpenDialog({
canSelectFiles: false,
canSelectFolders: true,
canSelectMany: false
});
if (!selectedFolders || selectedFolders.length === 0) {
return undefined;
}
const selectedFolderUri = selectedFolders[0];
const selectedFolderPath = selectedFolderUri.fsPath;
const folderPath = path.join(selectedFolderPath, folderName);
await fs.promises.mkdir(folderPath, { recursive: true });
// Create the files in the folder
options.forEach(async ([fileName, snippetName, snippetLanguage]: [string, string, string]) => {
// If the fileName has the variable {folderName}, it will be replaced to the name of the current folder
const filePath = path.join(folderPath, fileName.replace("{folderName}", folderName));
const body = ["hello", "world"].join("\n"); // Suppose to be created by snippet
await fs.promises.writeFile(filePath, body);
});
};
I have this index.js and it's logging the whole .json file. I want to log the age (21). How do I do that?
const fs = require('fs');
async function doSomething() {
const data = fs.readFileSync("./apple.json")
const data2 = await JSON.parse(data)
console.log(JSON.stringify(data2, null, 2))
}
doSomething()
and a side question:
Is there a way to avoid using fs to read files?
/*
apple.json
{
"memberList":[
{
"age":"21",
"name":"Jom"
}
]
}
*/
/*
Output:
{
"memberList": [
{
"age": "21",
"name": "Jom"
}
]
}
*/
Your first question is quite obvious and this answer will not address it. Regarding your second question, yes. Using commonjs you just have to require the file. Like this:
const data = require('./apple.json');
If you are using ES6 modules, then you have to addd a assert statement. Like this:
import data from './apple.json' assert {type: 'json'};
Did you try to access the members of memberList?
console.log(data2.memberList[0].age);
Basically, this shouldn´t be a very difficult question, but I´ve tried for like 2 or 3 hours and couldnt reach my goal, especially for such an "easy" question. Im using Node.js and my goal is, to load data from a Json file into a variable, add some new data to this and store my data into the same json file again.
Herefor, my json file looks like this:
[
{
"name": "Max",
"date": "1.1.2020"
}, {
"name": "Henry",
"date": "2.2.2020"
}
]
Here´s my code:
const fs = require('fs');
const filename = './jsonFile.json';
const data = loadJSON();
// console.log(data) keeps saying undefined (whithout using .toString in loadJSON)
function loadJSON() {
JSON.parse(fs.readFileSync(filename).toString); // toString doesnt work
}
function saveJSON(data) {
fs.writeFileSync(filename, JSON.stringify(data));
}
function adduser(username) {
var today = "3.3.2020"; // doesnt matter
let obj = {
name: username,
date: today
}
vipJson.push(obj);
saveVIP(vipJson);
}
It doesnt seem to be working. Could anyone help me, to fix my problem, so I can work with .json files ? Thanks a lot!
You need to specify the BufferEncoding options to read the file, something like this.
const fs = require("fs")
const data = fs.readFileSync("./myfile.json", { encoding: "utf8", flag: "r" })
If you are sure about json files, you can read data from file using require.
const fs = require('fs');
const filename = './jsonFile.json';
const data = loadJSON();
function loadJSON() {
return require(filename)
}
function saveJSON(data) {
fs.writeFileSync(filename, JSON.stringify(data));
}
function adduser(username) {
var today = "3.3.2020"; // doesnt matter
let obj = {
name: username,
date: today
}
data.push(obj);
saveJSON(data);
}
Try above code snippet.
I'm trying to write JavaScript code into a js with Nodejs fs module. I managed to write a json file but could wrap my head around on how to write JavaScript to it.
fs.writeFile("config.json", JSON.stringify({name: 'adman'tag: 'batsman',age: 25}), 'utf8',{ flag: "wx" }, function(err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
});
I need to create a .js file with the following data
const cricketers = [
{
name: 'adman',
tag: 'batsman',
age: 25
},
// other objects
]
module.exports = cricketers ;
Two things:
If all you want to do is to be able to do let someData = require('someFile.json'); Nodejs already supports requiring json files and treats them like Js objects.
Otherwise I don't know of a library that will do exactly this for you, BUT...
You can do this yourself. The fs.writeFile function takes a string, so you just have to generate the string you want to write to the file.
let someData = [{name: 'adman', tag: 'batsman', age: 25}];
let jsonData = JSON.stringify(someData);
let codeStr = `const cricketers = ${jsonData}; module.exports = cricketers;`;
fs.writeFile("someFile.js", codeStr, 'utf8',{ flag: "wx" }, function(err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
});
Obviously this only works for a very specific use case, but the point is it can be done with simple (or complicated...) string manipulation.
use string templating
const data = `const cricketers = ${JSON.stringify(yourArray)};
module.exports = cricketers;
`
Where yourArray is an array of objects
This question already has answers here:
How to filter object array based on attributes?
(21 answers)
Closed 8 years ago.
I have seen very old answers to this question and many of the technologies used 2 years back have changed.
What I have is JSON files sent by a database over to my server, and what I would like to know is how to filter that data.
I am running a server with node.js, and what I would like to do is something like:
var results = QueryLibrary.load(jsondata);
var filtered = results.query('select where user = "user1"');
How can I do something like this in javascript running in node?
underscore has a where function that does just this
var _ = require("underscore");
var json = '[{"user": "a", "age": 20}, {"user": "b", "age": 30}, {"user": "c", "age": 40}]';
var users = JSON.parse(json);
var filtered = _.where(users, {user: "a"});
// => [{user: "a", age: 20}]
Another utility library, Lo-Dash, has a where function that operates identically.
You can add underscore to your project using
$ npm install --save underscore
or lodash
$ npm install --save lodash
If you only care about the where function, lodash offers it as a separate module
// only install lodash.where
$ npm install --save lodash.where
To use it in your project
var where = require("lodash.where");
// ...
var filtered = where(users, {"user": "a"});
Even if you use a library to do this, a better approach is probably to setup a chain of streams that handles all of your data processing in smaller modules.
Without knowing what you actually want to do, I've created this as an example. For the purposes of this code, maybe think of a debug logging stream or something.
json-parser.js
input: string (JSON)
output: object
var Transform = require("stream").Transform;
function JsonParser() {
Transform.call(this, {objectMode: true});
this._transform = function _transform(json, enc, done) {
try {
this.push(JSON.parse(json));
}
catch (e) {
return done(e);
}
done();
}
}
JsonParser.prototype = Object.create(Transform.prototype, {
constructor: {
value: JsonParser
}
});
module.exports = JsonParser;
obj-filter.js
input: object
output: object (result of where(data, filters))
var Transform = require("stream").Transform;
var where = require("lodash.where");
function ObjFilter(filters) {
Transform.call(this, {objectMode: true});
this._transform = function _transform(obj, enc, done) {
this.push(where(obj, filters));
done();
}
}
ObjFilter.prototype = Object.create(Transform.prototype, {
constructor: {
value: ObjFilter
}
});
module.exports = ObjFilter;
stringifier.js
input: object
output: string (JSON)
var Transform = require("stream").Transform;
function Stringifier() {
Transform.call(this, {objectMode: true});
this._transform = function _transform(obj, enc, done) {
this.push(JSON.stringify(obj));
done();
}
}
Stringifier.prototype = Object.create(Transform.prototype, {
constructor: {
value: Stringifier
}
});
module.exports = Stringifier;
app.js
// modules
var JsonParser = require("json-parser");
var ObjFilter = require("obj-filter");
var Stringifier = require("stringifier");
// run
var parser = new JsonParser();
// setup stream chain
parser.pipe(new ObjFilter({"user": "a"}))
.pipe(new Stringifier())
.pipe(process.stdout);
// send example json in
parser.write('[{"user": "a", "age": 20}, {"user": "b", "age": 30}, {"user": "c", "age": 40}]');
// output
// => [{"user":"a","age":20}]
Here, I made a Stringifier stream that converts objects back into JSON so that we can see them dumped into the console, though you could easily create any streams you needed to handle the operations that your app requires. Your stream end points will likely not end up in writing to the console.
As a last note, you would probably create a database stream that accepts some sort of query options and emits json. You would pipe that stream directly into parser.
Anyway, I hope this gives you a better idea of how to process data in node.js.
You can use any of the normal array/object built-in functions that javascript has, normally that kind of query would be made at the time of retrieving your data from the database, not after.
something like
for(i=0;i<objIdsArray.length;i++) {
for(j=0;j<mockJSON.length;j++) {
if(mockJSON[j]["id"] === parseInt(objIdsArray[i])) {
mockJSON.splice(j, 1); // to delete it, could be any other instruction
}
}
}