Below is json file. I want to use variables for db password and db username. How can I add a variable in json ?
{
"name" : "mydb3",
"storage" : {
"binaryStorage" : {
"type" : "database",
"driverClass" : "com.mysql.jdbc.Driver",
"url" : "$jdbcdburl",
"username" : "$jdbcusername",
"password" : "$jdbcpassword"
}
},
"workspaces" : {
"default" : "default",
"allowCreation" : true
}
}
You can either build the JSON up using something like JSON Variables
https://www.npmjs.com/package/json-variables
Or you can load the JSON into memory look for the key and update it, example below using Node.JS
How to update a value in a json file and save it through node.js
Either way you wouldn't have to store the username and passwords in clear text, which is what I am guessing your are trying to avoid?
You may want to try Jsonnet, a data templating language, that is an extension of JSON and can export JSON files. E.g.
{
person1: {
username: "Alice",
password: "abc",
welcome: "Hello " + self.username + "!",
},
person2: self.person1 { username: "Bob", password: "123" },
}
would produce
{
"person1": {
"password": "abc",
"username": "Alice",
"welcome": "Hello Alice!"
},
"person2": {
"password": "123",
"username": "Bob",
"welcome": "Hello Bob!"
}
}
Other than fields you can also declare variables using local
, e.g.
local pi = 3.14;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With