Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add variables in json file?

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
  }
}
like image 847
Rishi Anand Avatar asked Apr 27 '16 11:04

Rishi Anand


2 Answers

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?

like image 172
RickWeb Avatar answered Sep 28 '22 19:09

RickWeb


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;
like image 32
dimid Avatar answered Sep 28 '22 17:09

dimid