JSON value update dynamically with process.env [node.js] - javascript

process.env.ENVIRONMENT = dev2
Input JSON:
{
"base": {
"product1" : "dev1.awesomeproduct1.com",
"product2" : "dev1.awesomeproduct2.com"
}
}
Output JSON:
Based on the process.env.ENVIRONMENT the product urls should be updated dynamically
{
"base": {
"product1" : "dev2.awesomeproduct1.com",
"product2" : "dev2.awesomeproduct2.com"
}
}

Do I correctly understand that you want to replace the part of the domain names up to the dot with your process.env.ENVIRONMENT variable?
Then the following code should work:
for (key in myJSON.base) {
myJSON.base[key] = myJSON.base[key].replace(/^[^.]+/, process.env.ENVIRONMENT);
}
Obviously, you will need to amend it if there are other fields than product<n> in the base object, or if you need to do a more complex replacement.

Related

fetch json value using Object.values in node js / Javascript

I am trying to fetch data from the JSON object by using Object.values
so my JSON looks like this
const jsonValue=
[
{
files:{
title:{
"en": "test"
}
,
description:{
"en": "dummy description"
}
}
},
{
files:{
title:{
"eu": "without description"
}
}
},
];
jsonValue.map((data)=>{
const des =Object.values(Object.values(data)[0]?.description)?? "";
console.log(...des)
})
I am trying to fetch the description value and if the description key is not present then it should return a blank space
I am using Object.values because en, and eu values get changed every time so to overcome this I am using Object.values
but it showing me an error cannot convert undefined.
My expected output is I want to fetch the description value if it presents inside the JSON and return a blank space if it is not present in JSON
Your solution is almost correct and I just did a small modification at the end to make it work as per the requirement.
This is how it works
First we will check if description itself is present and retrieve its key or default it to empty string
Then we will check if key is not empty string and then retrieve the value using the key.
Display the required result.
I just added a bit more console logs to show how each step behaves and these are not needed as part of solution.
Like VLAZ suggested it is always handy to use browser debugger tools to see the failed statements.
const jsonValue=
[
{
files:{
title:{
"en": "test"
}
,
description:{
"en": "dummy description"
}
}
},
{
files:{
title:{
"eu": "without description"
}
}
},
];
jsonValue.map((data)=>{
console.log(Object.values(data)[0]?.description);
const desKey = Object.values(data)[0]?.description ?? "";
if(desKey !== ""){
console.log(Object.values(desKey));
const des = Object.values(desKey);
console.log(...des)
}
})
If I understood you correctly, when
const descriptions = jsonValue.map(value =>
value.files.description ?
value.files.description[Object.keys(value.files.title)[0]] :
''
)
console.log(descriptions)
will print ["dummy description", ""] and should do the trock

Mongoose check if text exist in any of Map entry value

We need to put translation in Map within a JSON object. When we search for an item, we will only pass the name and we need to check if that name is in one of the translation object.
Example:
"item" : {
"name" : {
"en_US": "Hello"
"id_ID" : "Halo"
}
}
The client will only send the name but we won't know the language used (e.g, Halo) and we need to return the document with that name in any language. How should we define the filter object? So far all the example I saw require that the we define the complete object info to filter (ex: item.name.id_ID).
playground
db.collection.aggregate([
{
"$addFields": {//reshape it to match on value field
"field": {
"$objectToArray": "$item.name"
}
}
},
{//reshape again to get the only matching element
"$unwind": "$field"
},
{
$match: {//match condition
"field.v": "Hello"
}
},
{
$project: {//project only what is needed
"field.k": 1,
"item": 1
}
}
])

How to get specific data from Firebase using JavaScript?

I was trying to get some data organized under a specific Id from a realtime database
For example, this is my JSON object :
{
1: {
Name :"karan",
Bio:"hello this is Karan",
}
2: {
Name :"gandhi",
Bio:"hello this is gandhi",
}
}
I want all the data to be arranged in 1. Is there any way to do this? It would be better if the data would be arranged in a object in the following format :
var data = {
Name : "karan",
Bio : "hello this is Karan",
}
This should be in the same order given in the original object
Thank you
I think what you are looking for is Object.values().
Let's suppose your database looks like:
{
1: {
"Name" : "karan",
"Bio" : "hello this is Karan",
},
2: {
"Name" : "gandhi",
"Bio" : "hello this is gandhi",
}
}
You want to retrieve data without the keys "1" and "2". Your query will be:
// Let's call your node "foo"
firebase.database().ref("foo").once("value").then(data => {
console.log(Object.values(data.val()));
});
Try to execute the following:
var data = {
1: {
"Name" : "karan",
"Bio" : "hello this is Karan",
},
2: {
"Name" : "gandhi",
"Bio" : "hello this is gandhi",
}
}
console.log(Object.values(data));
Read more here: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Object/values

how to change .immediate.types.apps to dynamic text

this code is working, but i need the $push query field to be dynamin like this
generalrecommendations[req.body.term].types.apps please help.
here is my mongodb database:
"generalrecommendations" : {
"immediate" : {
"types" : {
"apps" : [
"1"
]
}
}
}
and here is my code:
db.collection('colleges').update(
{ '_id': new ObjectId(req.body.college) },
{
$push:{ "generalrecommendations.immediate.types.apps": temp2[key1][i] }
}, function (err, result) {
})
finds the id and appends new item to exixting array. here immediate is hardcoded but now i need to change it to dynamic like this req.body.term
to make the query field dynamic you need to use [] operators while entering your query Field. Lets say your
req.body.term = immediate;
then instead of
$push:{ "generalrecommendations.immediate.types.apps": temp2[key1][i] }
use
$push:{ ["generalrecommendations"+ req.body.term +"types.apps"]: temp2[key1][i] }
adding [] to your query field will make it dynamic.

Unable to get the right json property dynamically

I have following JSON structure:
var pages = {
"yearly": {
"MXN": {
"id" : "2c92c0f940f1b6e50140f4b7d9054a6e",
},
"NZD": {
"id" : "2c92c0f940f1b6d40140f4b7e14d66b7",
}
},
"monthly": {
"MXN": {
"id" : "2c92c0f940f1b6d40140f4b40ed85c57",
},
"NZD": {
"id" : "2c92c0f840f1c2cc0140f4b3b15d3956",
}
}
}
i want to get the values of json inside yearly property dynamically for example i get the dynamic value MXN inside a variable currency now i am having problem in accessing its value i am trying to access it like this:
pages.yearly[currency].id
but i am unable to get to that id property gives me error of TypeError: pages.monthly[currency] is undefined what is wrong in my syntax that is causing type error??
The according to this error the variable currency needs to be defined. For example
var currency = "MXN";

Categories