Convert Delimited Test File to JSON Object Node.JS - javascript

fs.readFile('data.txt', 'utf8', (err, result)=>{
if(err){
console.log(err);
}
else{
console.log(result);
}
})
The above code is working fine. But I want to parse the result as a JSON object.
Format of Test File
test1:value1:"Pass":John:"Log1"
test2:value2:"Fail":Peter:"Log2"
test3:value3:"Pass":Peter:"Log3"
Format of result variable
test1:value1:"Pass":John:"Log1"
test2:value2:"Fail":Peter:"Log2"
test3:value3:"Pass":Peter:"Log3"
Now I want to convert the result to a JSON object.
[
{
"test": "test1",
"value": "value1",
"status": "Pass",
"name": "John",
"log": "Log1"
},
{
"test": "test2",
"value": "value2",
"status": "Fail",
"name": "Peter",
"log": "Log2"
},
{
"test": "test3",
"value": "value3",
"status": "Pass",
"name": "Peter",
"log": "Log3"
}
]
Please note. I also want to use result so I need to process after getting value of result

Not sure if you need an array of objects or its JSON string representation, so these are both results:
const fs = require('fs');
fs.readFile('data.txt', 'utf8', (err, result) => {
if (err) {
console.log(err);
} else {
console.log(result);
const array = result.split(/(?<=.)[\r\n]+(?=.)/u).map((line) => {
const values = line.trim().split(/\s*:\s*/u)
.map(value => value.replace(/^"|"$/gu, ''));
const keys = ['test', 'value', 'status', 'name', 'log'];
return Object.fromEntries(keys.map((key, index) => [key, values[index]]));
});
console.log(array);
const json = JSON.stringify(array, null, ' ');
console.log(json);
}
});

Related

Replace a value in a json file with value from another file using fs

I want to read the values of key, access, path, bucket and bucketPath and use them in the JSON file test.json.
I have a function that reads the content of configuration.js and attempts to write to test.json. Currently, I am able to write the values of bucket.I get the changed/new values and lines of null for the rest of the json.
I want to always return the new values and the other objects in the file. Also, in cases where bucket already has a value, I want it replaced by whatever is read from configuration.json
How can I fix this, and how can i change the values for the rest access, key, path and bucketpath?
index.js
const fs = require("fs").promises;
async function readJSON(filePath, values) {
const data = await fs.readFile(filePath);
try {
return JSON.parse(data);
} catch (err) {
console.log(err);
}
}
(async() => {
const credentials = await readJSON("./configuration.json");
const path = credentials.path;
const bucket = credentials.bucket;
const access = credentials.access;
const key = credentials.key;
const bucketPath = credentials.bucketPath;
const data = await jsonReader("./test.json");
const finalJSON = data.data ? .map((x) => {
if (x.type == "s3 credentials") return { ...x, bucket };
});
await fs.writeFile(
"./test.json",
JSON.stringify({
data: finalJSON
})
);
})();
test.json
{
"label": "storage record",
"data": [{
"id": "8902uw",
"type": "config",
"values": {
"access": "$access",
"key": "$key"
}
},
{
"id": "893wh002jei",
"type": "s3 credentials",
"bucket": ""
},
{
"id": "90yueps",
"type": "upload",
"input": "localhost: `$path`"
},
{
"id": "9028901",
"type": "change",
"name": "Adjust data",
"measure": [{
"t": "setter"
},
{
"p": "filename",
"to": "$join([\"$bucketPath\", data])"
}
],
"fixed": ""
}
]
}
configuration.json
{
"key": "880082",
"access": "793082",
"path": "/store",
"bucket": "testBucket",
"bucketPath": "/record"
}
Currently, when I run this, I get:
{
null,
"data": [{
null,
null,
null,
null
{
"id": "893wh002jei",
"type": "s3 credentials",
"bucket": ""
},
{
null,
null,
null
]
}
might this be a solution !
const fs = require('fs');
const fileName = './file.json';
const file = require(fileName);
file.key = "new value";
fs.writeFile(fileName, JSON.stringify(file), function writeJSON(err) {
if (err) return console.log(err);
console.log(JSON.stringify(file));
console.log('writing to ' + fileName);
});
[Updated answer]
From what you comment:
it's the same question. So when I run what I have, I get null for the other objects. I want test.json to remain the same just with updated values.
const testObj = await jsonReader("./test.json");
const finalJSON = {
...testObj,
data: testObj.data?.map((x) => {
if (x.type === 's3 credentials') {
return { ...x, bucket };
} else {
return x;
}
})
}
// which the code I gave above,
// the `finalJSON` now is the clone of the original from `test.json`
// plus updated values
await fs.writeFile(
"./test.json",
JSON.stringify(finalJSON)
);
[Original answer]
There is a problem with the function you pass to the map function.
The condition if without else.
I think you need else { return x; } to return original data if x.type is not what you expected.

Dynamic key value pair find in MongoDB

I'm facing a problem in searching for dynamic key value pairs using .find method in mongoDB.
here is my array of objects which the key value pairs can be increased...
[
{
"key" : "dynamicKey1",
"value": "dynamicValue1"
},
{
"key" : "dynamicKey2",
"value": "dynamicValu2"
},
...
]
I wish to get the result in a way that satisfies all the key value pair conditions. Something like find all documents from collection that satisfies these conditions.
So my API is this:
app.post('/search/:name', (req, res) => {
const collectionName = req.params.name;
async.eachSeries(req.body, (item, callback) => {
const database = client.db("databaseName");
const result = database.collection(collectionName).find({ [item.key]: item.value }).toArray();
result.then((doc) => {
res.json(doc)
})
callback();
});
});
the result returns two or more promises but I only need those documents that satisfies all .find queries as the result.
How should I solve it?
Thanks
Here is Insomnia POST request Image: Insomnia
Based on the Image you provided your data is of the following format in the database:
[
{
"_id": "1",
"region": "Auckland",
"anzsic_descriptor":"Agriculture",
"gas": "carbon diaoxide equivalents",
"units":"kilotonnes",
"magnitude":"carbon diaoxide equivalents",
"year": "2011",
"data_val": "764.3"
},
{
"_id": "2",
"region": "Canterbury",
"anzsic_descriptor":"Agriculture",
"gas": "carbon diaoxide equivalents",
"units":"kilotonnes",
"magnitude":"carbon diaoxide equivalents",
"year": "2011",
"data_val": "6823.16"
},
{
"_id": "3",
"region": "Bay of Plenty",
"anzsic_descriptor":"Agriculture",
"gas": "carbon diaoxide equivalents",
"units":"kilotonnes",
"magnitude":"carbon diaoxide equivalents",
"year": "2011",
"data_val": "1477.94"
}
...........
]
The /search/:name is receiving a body which is an array in the below format:
[{
"key": "year",
"value": "2011"
}, {
"key": "data_val",
"value": "764.3"
}]
You need to use a query generate as follows to generate your query first before the DB query:
const generateQuery = (arr) => {
return arr.reduce((result, item) => {
const {key, value } = item;
result[key] = value;
return result;
}, {});
};
console.log(generateQuery([{"key": "year","value": "2011"},{"key": "data_val","value": "764.3"}]))
The DB query for the generated query looks like this: mongo playground.
Then use this generated query to get the result in the endpoint, all these changes looks like below:
const generateQuery = (arr) => {
return arr.reduce((result, item) => {
const { key, value } = item;
result[key] = value;
return result;
}, {});
};
app.post('/search/:name', (req, res) => {
return new Promise((resolve, reject) => {
const collectionName = req.params.name;
const queryArry = req.body // [{"key": "year","value": "2011"},{"key": "data_val","value": "764.3"}]
const query = generateQuery(queryArry);
const database = client.db("databaseName");
database.collection(collectionName).find(query).then(result => {
return resolve(result);
}).catch(e => {
reject(e);
});
}).catch(e => {
reject(e);
})
});

Assign Variables to JSON data in node JS

I have an API End point that i am trying to assign variables to, now the one JSON data is an array and I Loop over it to get the data out in my console log, the difficulty i am having is that i want to assign variables to them.
Here is my code:
const request = require('request');
request('https://fantasy.premierleague.com/api/leagues-classic/1114549/standings/?page_new_entries=1&page_standings=1&phase=1', { json: true }, (err, res, body) => {
if (err) { return console.log(err); }
var data = body.standings.results;
data.forEach(obj => {
Object.entries(obj).forEach(([key, value]) => {
console.log(`${key} ${value}`);
});
console.log('-------------------');
});
});
and here is my JSON data:
{
"league": {
"id": 1114549,
"name": "The crew",
"created": "2020-09-11T17:36:20.083556Z",
"closed": false,
"max_entries": null,
"league_type": "x",
"scoring": "c",
"admin_entry": 3523866,
"start_event": 1,
"code_privacy": "p",
"rank": null
},
"new_entries": {
"has_next": false,
"page": 1,
"results": []
},
"standings": {
"has_next": false,
"page": 1,
"results": [
{
"id": 30771462,
"event_total": 8,
"player_name": "Mohammed Ismail",
"rank": 1,
"last_rank": 0,
"rank_sort": 1,
"total": 8,
"entry": 3808290,
"entry_name": "Moe"
}
Now I am trying to console log only the standings.result.player_name in my console log so i can use it else where, how do i do that
So my output in the console should only be "player_name": "Mohammed Ismail",
I'm not sure that i get the question, but in case if you want to get all player_name and collect it in array as example, You can do it next:
const request = require('request');
const url = 'https://fantasy.premierleague.com/api/leagues-classic/1114549/standings/?page_new_entries=1&page_standings=1&phase=1';
async function getStandings(url) {
return new Promise((resolve, reject) => {
request(
url,
{ json: true },
(err, res, body) => {
if (err) {
reject(err);
return;
}
resolve(body.standings.results);
}
);
});
}
(async () => {
const data = await getStandings(url);
// here you will receive array of stadings
console.log('data : ', data);
})();

How to save result mongoDB query to an array or list?

My query
test.find({ "age" : "20" }, {"email": 1, "_id": 0}).toArray(function (err,data) {
if (err) throw err;
console.log(data);
and result after I run my query
[ { email: 'root#root.com' } ]
How can I save console.log(data); to an Array or list ?
I tried .push(data) to save but not ok.
Create an empty object and then assign value to it
test.find({ "age" : "20" }, {"email": 1, "_id": 0}).toArray(function (err,data) {
if (err) throw err;
console.log(data);
var obj =[];
var obj = data;

node js mongoose find data from array in collection

I am working with node.js and mongoose I am stuck in a problem. My users collection looks like.
{
"_id": ObjectId("564b6deec50de8d827c0a51a"),
"email": "ak#gmail.com",
"ngos": [
{
"_id": ObjectId("564b7527ecc479e4259edff7"),
"name": "Children trust",
},
{
"_id": ObjectId("564b79e0ecc479e4259edff8"),
"name": "Charity Two",
"location": "Australia"
}
]
}
{
"_id": ObjectId("564e0a18c8cd4b5420a9250c"),
"email": "some#gsomel.com",
"ngos": [
{
"_id": ObjectId("564e0b3bc8cd4b5420a92510"),
"name": "Charity Two",
"location": "US"
}
]
}
I want to find all the ngos whose name is like Charity so it should return me.
{
"_id": ObjectId("564e0b3bc8cd4b5420a92510"),
"name": "Charity Two",
"location": "US"
}
{
"_id": ObjectId("564e0b3bc8cd4b5420a92510"),
"name": "Charity Two",
"location": "Australia"
}
I tried
User.find({"ngos.name": new RegExp(name, 'i')}, function(err, user) {
if (err) return next(err);
res.json(user);
});
It gave me both users with all the data as I am returning the user but if I change res.json(user); to res.json(user.ngos); I am not getting any response.
How can I retreive those particular ngos whose name matches?
Thanks
Use regex filtering on your final result array as follows:
var rgx = new RegExp(name, 'i');
User.find({"ngos.name": rgx})
.lean()
.exec(function(err, users) {
if (err) return next(err);
var result = users.map(function (n){
return n.ngos.filter(function(val){return rgx.test(val.name);});
})
console.log(JSON.stringify(result, undefined, 4));
res.json(result);
});
Check the demo below.
var cursor = [
{
"ngos": [
{
"_id": "564b7527ecc479e4259edff7",
"name": "Children trust",
},
{
"_id": "564b79e0ecc479e4259edff8",
"name": "Charity One",
"location": "Australia"
}
]
},
{
"ngos": [
{
"_id": "564e0b3bc8cd4b5420a92510",
"name": "Charity Two",
"location": "US"
}
]
}
];
var rgx = new RegExp('Charity', 'i');
var result = cursor.map(function (n){
return n.ngos.filter(function(val){return rgx.test(val.name);});
})
pre.innerHTML = "result: " + JSON.stringify(result, null, 4);
<pre id="pre"></pre>
Hope this helps,
User.find({"ngos.name": new Regex(name, 'i')},{'ngos':1}).exec(function(err,data){
if (err) throw(err);
res.json(data);
});
just try this way in mongoose
you getting res is Array so use "forEach"
User.find({'ngos.name':new RegExp('name', 'i')}, {'ngos':1},function(err,users){
users.forEach( function(user) {
user.ngos.forEach( function(nom) {
console.log( nom );
})
} );
})
just try this way in mongodb
db.user.find({'ngos.name':new RegExp('name', 'i')},{'ngos':1}).forEach( function(user) {
user.ngos.forEach( function(nom) {
print( nom );
})
} )
I think this help to u !

Categories

Resources