How to add variables in json file? - json

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;

You can use string interpolation + backslash...
See "{{testUrl}}" in Postman exported JSON for exmaple:
Variable value:
"info":{
"_postman_id": "YOUR-ID-IN-POSTMAN",
"name": "YOU-EXPORTED-COLLECTION-FILE-NAME",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"testUrl": "https://www.techeader.com"},
Variable implemented in raw:
"url":{
"raw": "\"{{testUrl}}\"",
"protocol": "http",
"host": ["test","com"]},

JSON is a standard format for representing objects as a textual, human readable (most of the time :-/ ) format. The concept of a variable is not applicable in this context. Variables exist in memory/code only, and a variable can be written to JSON format in a file for example, but with the risk of sounding "blunt" IMHO, your question doesn't make much sense at this point.
If you elaborate a little more on what you want to achieve in your application, I might be able to help you better.

Related

Can Filebeat parse JSON fields instead of the whole JSON object into kibana?

I am able to get a single JSON object in Kibana:
By having this in the filebeat.yml file:
output.elasticsearch:
hosts: ["localhost:9200"]
How can I get the individual elements in the JSON string. So say if I wanted to compare all the "pseudorange" fields of all my JSON objects. How would I:
Select "pseudorange" field from all my JSON messages to compare them.
Compare them visually in kibana. At the moment I can't even find the message let alone the individual fields in the visualisation tab...
I have heard of people using logstash to parse the string somehow but is there no way of doing this simply with filebeat? If there isn't then what do I do with logstash to help filter the individual fields in the json instead of have my message just one big json string that I cannot interact with?
I get the following output from output.console, note I am putting some information in <> to hide it:
"#timestamp": "2021-03-23T09:37:21.941Z",
"#metadata": {
"beat": "filebeat",
"type": "doc",
"version": "6.8.14",
"truncated": false
},
"message": "{\n\t\"Signal_data\" : \n\t{\n\t\t\"antenna type:\" : \"GPS\",\n\t\t\"frequency type:\" : \"GPS\",\n\t\t\"position x:\" : 0.0,\n\t\t\"position y:\" : 0.0,\n\t\t\"position z:\" : 0.0,\n\t\t\"pseudorange:\" : 20280317.359730639,\n\t\t\"pseudorange_error:\" : 0.0,\n\t\t\"pseudorange_rate:\" : -152.02620448094211,\n\t\t\"svid\" : 18\n\t}\n}\u0000",
"source": <ip address>,
"log": {
"source": {
"address": <ip address>
}
},
"input": {
"type": "udp"
},
"prospector": {
"type": "udp"
},
"beat": {
"name": <ip address>,
"hostname": "ip-<ip address>",
"version": "6.8.14"
},
"host": {
"name": "ip-<ip address>",
"os": {
<ubuntu info>
},
"id": <id>,
"containerized": false,
"architecture": "x86_64"
},
"meta": {
"cloud": {
<cloud info>
}
}
}
In Filebeat, you can leverage the decode_json_fields processor in order to decode a JSON string and add the decoded fields into the root obejct:
processors:
- decode_json_fields:
fields: ["message"]
process_array: false
max_depth: 2
target: ""
overwrite_keys: true
add_error_key: false
Credit to Val for this. His answer worked however as he suggested my JSON string had a \000 at the end which stops it being JSON and prevented the decode_json_fields processor from working as it should...
Upgrading to version 7.12 of Filebeat (also ensure version 7.12 of Elasticsearch and Kibana because mismatched versions between them can cause issues) allows us to use the script processor: https://www.elastic.co/guide/en/beats/filebeat/current/processor-script.html.
Credit to Val here again, this script removed the null terminator:
- script:
lang: javascript
id: trim
source: >
function process(event) {
event.Put("message", event.Get("message").trim());
}
After the null terminator was removed the decode_json_fields processor did its job as Val suggested and I was able to extract the individual elements of the JSON field which allowed Kibana visualisation to look at the elements I wanted!

ScalikeJDBC Configuration without HOCON

I need to initialize from ~/myConfig.json, which looks like:
{
"databaseActive": "production",
"databases": [
{
"name": "localhost",
"PGDB": "asdf",
"PGHOST": "localhost",
"PGPASSWORD": "asdf",
"PGPORT": "5432",
"PGUSER": "asdf"
},
{
"name": "production",
"PGDB": "asdf",
"PGHOST": "asdf.rds.amazonaws.com",
"PGPASSWORD": "asdf",
"PGPORT": "5432",
"PGUSER": "asdf"
}
]
}
This means I cannot call scalikejdbc.config.DBs.setupAll(). How might I use this JSON file to initialize scalikeJDBC from the appropriate database settings, according to the value of databaseActive?
ScalikeJDBC offers only the HOCON reader. If you go with your own JSON config files, you need to write your own JSON parser which checks the databaseActive.
Parsing your config and binding it to ScalikeJDBC's config classess would be simple:
https://github.com/scalikejdbc/scalikejdbc/blob/3.3.5/scalikejdbc-config/src/main/scala/scalikejdbc/config/DBs.scala#L10-L17
https://github.com/scalikejdbc/scalikejdbc/blob/3.3.5/scalikejdbc-config/src/main/scala/scalikejdbc/config/TypesafeConfigReader.scala#L59-L124

How to access the json data in react native mentioned in below pattern?

The json data is in the below pattern. And the Json data is coming from backend and getting through api and storing in a state variable.
{
"message": "user created successfully",
"status": "success",
"student": {
"class": "10",
"email": "user#gmail.com",
"name": "User",
"password": "user",
"phone_number": "some phone number",
"school": "1",
"section": "a"
}
}
I have stored the data which is returned through api in a state variable.
constructor(){
super();
this.state = {
jsonData: ''
}
}
And tried accessing using below fashion.
this.state.jsonData.status
but not able to access. How can I access the status value in react?
Please check type of jsonData in state when you call it using typeof or instanceof.
It maybe by you store fetched data in string type without check and manipulating.
If it is string type, convert it using JSON.parse

How to specify JSON-formatted string in Cloudformation?

I have the following resource on my CloudFormation template to create a rule to run a Lambda function, from the AWS documentation:
"ScheduledRule": {
"Type": "AWS::Events::Rule",
"Properties": {
"Description": "ScheduledRule",
"ScheduleExpression": "rate(5 minutes)",
"State": "ENABLED",
"Targets": [{
"Arn": { "Fn::GetAtt": ["myLambda", "Arn"] },
"Id": "TargetFunctionV1"
}]
}
}
I would like to specify the Input:
{
"Arn" : String,
"Id" : String,
"Input" : String,
"InputPath" : String
}
and Input is a JSON-formatted text string that is passed to the target. This value overrides the matched event.
I would like my JSON formatted text to be:
{
"mykey1": "Some Value"
}
I do not know how to specify it in the Input, when I put:
"ScheduledRule": {
"Type": "AWS::Events::Rule",
"Properties": {
"Description": "ScheduledRule",
"ScheduleExpression": "rate(5 minutes)",
"State": "ENABLED",
"Targets": [{
"Arn": { "Fn::GetAtt": ["myLambda", "Arn"] },
"Id": "TargetFunctionV1",
"Input": { "mykey1": "Some Value" }
}]
}
}
I will get error:
Value of property Input must be of type String
How should I specify it correctly?
I would use YAML as it is easier and more readable:
Input:
!Sub |
{
mykey1: "${myKey}"
}
Found out the answer myself:
"Input": "{ \"test\" : \"value11\", \"test2\" : \"value22\"}"
Hope it helps someone else.
Update:
You basically use the result of JSON.Stringify() to get the string into "Input" field. Use online JSON.Stringify() like https://onlinetexttools.com/json-stringify-text
I wanted to expand on #Pau's answer. Basically if you use the | to say that the whole block below is to be treated as a raw string.
If you need to replace any variable in the JSON then you can use Sub, but if you don't have any variables, then you don't need Sub. An example would be:
Input:|
{
"jsonVar":"jsonVal",
"jsonVar2" : "jsonVal2"
}
You can later do JSON.parse(<input-variable>) to get the JSON object.
NOTE: Don't put commas at the end of the variables in JSON, if there is no next variable. Example :
Input:|
{
"jsonVar":"jsonVal",
"jsonVar2" : "jsonVal2",
}
This will cause JSON parsing errors.
If you are writing your CloudFormation scripts in yaml and finding it difficult to use a JSON string (such as a policy doc) the easiest way is to convert your JSON into yaml using an online converter
ApiGatewayRestApi:
Type: AWS::ApiGateway::RestApi
Properties:
Description: API Gateway for some API
EndpointConfiguration:
Types:
- PRIVATE
Name: MyAPIGateway
Policy: <Policy Doc >
Lets say the policy doc is as follows.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": "execute-api:Invoke",
"Resource": "arn:aws:execute-api:ap-southeast-2:something/*",
"Condition": {
"ForAnyValue:StringEquals": {
"aws:sourceVpce": "vpce-abcd"
}
}
}
]
}
Using the converter you can convert the JSON into an identical yaml and use as follows.
ApiGatewayRestApi:
Type: AWS::ApiGateway::RestApi
Properties:
Description: API Gateway for some API
EndpointConfiguration:
Types:
- PRIVATE
Name: MyAPIGateway
Policy:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal: "*"
Action: execute-api:Invoke
Resource: arn:aws:execute-api:ap-southeast-2:something/*
Condition:
ForAnyValue:StringEquals:
aws:sourceVpce: vpce-abcd
Here's my similar YAML code for the "input" line. It took me all day to figure out the syntax so I'm hoping this might help someone in the future.
Input: "{\"action\":[\"configure\"],\"mode\":[\"ec2\"],\"optionalConfigurationSource\":[\"ssm\"],\"optionalConfigurationLocation\":[\"AmazonCloudWatch-Baseline-Windows\"],\"optionalRestart\":[\"yes\"]}"

JSON Syntax: Transmitting an array

A valid JSON Syntax is something of the kind:
{
"username": "admin",
"password": "123"
}
But what if I want to transmit an array of 'users' (given the example), instead of a single 'user' ?
Is the code below Valid JSON, according to the specifications?
[{
"username": "admin",
"password": "123"
}, {
"username": "bbvb",
"password": "sdfsdf"
}, {
"username": "asd",
"password": "222"
}]
And if not, what is the best way to transmit an array of values across with JSON? (And with 'best way', I mean syntactically)
Yes, your example is valid JSON - that is exactly how you want to use an array.
Edit : Here is a good link on JSON and its usage.
The not-very-well-known page json.org has a diagram that shows the syntax. It’s extremely simple to understand, IMHO.
Json Syntax Includes following.
1. Data is represented in name/value pairs.
2. Each name is followed by ':'(colon).
3. The name/value pairs are separated by ,(comma).
4. Json object starts and ends with '{' and '}'.
5. Square brackets '[ ]' hold arrays and values are separated by
,(comma).
Json Objects Example
{
"id":"21",
"language": "Json",
"edition": "second",
}
Json Array Example
{
"book": [
{
"id":"21",
"language": "Json",
"edition": "second"
},
{
"id":"42",
"language": "Json",
"edition": "third"
}]
}
I have taken reference from http://www.tutsway.com/json-syntax.php
What you wrote up there is already correct :)
[{ "username" : "admin", "password" : "123" }, { "username" : "bbvb", "password" : "sdfsdf" }, { "username" : "asd", "password" : "222" }]