Write an object to an .json file using angular? - angularjs

I have an object like:
[
{
"text" : "Address of Bowlers game center in Chennai?",
"entities" : [
{
"entity" : "action",
"value" : "business"
},
{
"entity" : "intent",
"value" : "fetchItems"
},
{
"entity" : "bizCategory",
"value" : "bowling",
"start" : 11,
"end" : 30
},
{
"entity" : "wit$location",
"value" : "Chennai",
"start" : 34,
"end" : 40
}
]
},
{
.....more objects
}
]
Now I have to do some manipulation on this object and save the manipulated object into an .json file using angular js.
var originalObject = $http.get("data/chennai.json");
var manipuatedObject; // this is the manipulated object i get after applying some changes on originatalObject.
Now how can I save this manipuatedObject into an json file.

Try this:
function saveText(text, filename){
var a = document.createElement('a');
a.setAttribute('href', 'data:text/plain;charset=utf-u,'+encodeURIComponent(text));
a.setAttribute('download', filename);
a.click()
}
So a possible use might be:
var obj = {a: "Hello", b: "World");
saveText( JSON.stringify(obj), "filename.json" );

Related

How to add an Element (list) into an JSON. Flutter

How I get a list/elements in an Json. To get the input i use this:
enterJson(productName, expirydDate, stock, notifications) {
OverviewProduct overviewProduct =
OverviewProduct(productName, expirydDate, stock, notifications);
Map<String, dynamic> map = {
'Product_Name': overviewProduct.productName,
'Expiry_date': overviewProduct.expirydDate,
'Stock': overviewProduct.stock,
'Notifications': overviewProduct.notifications
};
String rawJson = jsonEncode(map);
print(rawJson);
}
Now, i get {"Product_Name":"Test","Expiry_date":"12","Stock":1,"Notifications":true}. This, I would now like to add this into a json file. The json file should have more or less this structure:
[
{
"Product_Name": "Pom-Bär Original",
"Expiry_date" : "10.05.21",
"Stock" : "1",
"Notifications" : "True"
},
{
"Product_Name": "Ja! Natürliches Mineralwasser",
"Expiry_date" : "23.11.21",
"Stock" : "1",
"Notifications" : "True"
}
]
instead of "enterJson" use below code in "OverviewProduct Class":
factory OverviewProduct.fromMap(Map<String, dynamic> map) {
return new OverviewProduct(
Product_Name: map['Product_Name'],
Expiry_date: map['Expiry_date'] ,
Stock: map['Stock'] ,
Notifications: map['Notifications']
);
}
and use it as this:
(map['Products']).map((p) => OverviewProduct.fromMap(p)).toList()

Rename a sub-document field within an Array of an Document

I am trying to rename the particular child field inside an array of an collection in MONGODB
{
"_id" : Id("248NSAJKH258"),
"isGoogled" : true,
"toCrawled" : true,
"result" : [
{
"resultsId" : 1,
"title" : "Text Data to be writen",
"googleRanking" : 1,
"isrelated" : false
},
{
"resultId" : 2,
"title" : "Text Data",
"googleRanking" : 2,
"isrelated" : true
}]
**I need to rename "isrelated" to "related" ** from the collection document
Using mongo Version 4.0
I Tried :
db.collection_name.update({}, { $rename: { 'result.isrelated': 'result.related'} } )
But it didn't worked in my case
As mentioned in official documentation $rename not working for arrays.
Please check link below:
https://docs.mongodb.com/manual/reference/operator/update/rename/
But you can do something like this
let newResult = [];
db.aa1.find({}).forEach(doc => {
for (let i in doc.result) {
newResult.push({
"resultsId" : doc.result[i]['resultsId'],
"title" : doc.result[i]['title'],
"googleRanking" : doc.result[i]['googleRanking'],
"related" : doc.result[i]['isrelated'],
})
}
db.aa1.updateOne({_id: doc._id}, {$set: {result: newResult}});
newResult = []
})

Append array element to String(Swift)

I am trying to make an array of random numbers for an ID for 25 people so I figured that would be the easiest way would use a array map but is there a way to take each element and attach it to an "email" in the dictionary key.
class ClassRosterModel {
var randomArray = (1...25).map{_ in arc4random()}
var studentsRoster = [Dictionary<String, String>] ()
init () {
studentsRoster.append(["name": "Kacz, Alex", "major" : "SE", "email" : ".join(randomArray[0])#email.edu", "currentTerm" : "Spring", "numberOfCredits" : "" ])
studentsRoster.append(["name": "O'Rore, Ryan", "major" : "SE", "email" : ".join(randomArray[0]#email.edu", "currentTerm" : "Fall", "numberOfCredits" : "" ])
}
}
From what I am understanding, you want to put a value inside a string. You can use \().
Example:
let value = 101
let email = "myname\(value)#email.com"
print(email)
The console result will be:
myname101#email.com
Just use your randomArray instead of value. And save the email inside your dictionary.
studentsRoster.append(["name": "Kacz, Alex", "major" : "SE", "email" : ".join\(randomArray[0])#email.edu", "currentTerm" : "Spring", "numberOfCredits" : "" ])
studentsRoster.append(["name": "O'Rore, Ryan", "major" : "SE", "email" : ".join\(randomArray[0])#email.edu", "currentTerm" : "Fall", "numberOfCredits" : "" ])

Mongoose update array property

I am trying to update a document via Mongoose, but it does not update the array property.
Here's an example document:
{
"_id" : "55da477a9bfc910e38zzccf2",
"projectname" : "ASong",
"owner" : "adam",
"tracks" : [{
"name" : "Bass",
"file" : "upload/Bass.mp3",
"volume" : "0.75",
"pan" : "0.65"
}, {
"file" : "upload/Drums.mp3",
"volume" : "0.4",
"pan" : "-0.75",
"name" : "Drums"
}
],
"users" : ["adam", "eve"]
}
if I pass to Mongoose an object and try to use it to update like this:
var id = req.body._id;
var editedProject = req.body;
delete editedProject._id;
console.log("TRACKS: " +JSON.stringify(editedProject.tracks));
Project.update({"_id": id}, editedProject, {"upsert": true}, function(err, proj) {
if (err) {
res.send(err);
}
res.json(proj);
});
Only the 1st level properties are updated, but not the array.
I've found some solutions that involve looping through the array elements and calling an update for each element, but I would like to avoid that and keep the update as a single operation.
Or is it better to avoid using arrays?

Pushing Data To A Child Node with AngularJS and Firebase

I'm getting this:
"users" : {
"simplelogin:129" : {
"-JbX-CJuMDf1H8-yCETV" : {
"gold" : {
"receive" : {
"amount" : 1,
"date" : 1416835946409,
"email" : "you#me.com"
}
}
},
But I need something like this:
"users" : {
"simplelogin:130" : {
"date" : 1416834728422,
"email" : "yam#dam.com",
"id" : "130",
"gold" : {
"receive" : {
"-JbWva7Gh9Y3RarlF77h" : {
"amount" : 1,
"date" : 1416834737201,
"email" : "you#do.com"
},
If I use .update instead of .push, I get the file structure I need, but then it overrides it for each transaction instead of adding a new one each time.
receive.orderByChild("email").equalTo( $scope.user.email ).once('child_added', function(snap) {
snap.ref().push({gold: {receive: myGold}}).then(function() {
});
Your question is not very clear, but I assume that you want to push the new node deeper in the tree.
simplelogin:130
date: ...
email: "yam#dam.com"
id: 130
gold
received
-JbX-CJuMDf1H8-yCETV
amount: 1
date: ...
email: "yam#dam.com"
To put the data into gold/received, you'd do:
snap.ref().child('gold/receive').push(myGold);
So you first find the node you want to add something to and only then call push to add a new child (with a new unique id) there.

Resources