I'm building an Angular App and also using angular translate as it needs to be in dual languages.
I've seem to have created my JSON properly (ran it through a checker), but when I try to access items within the JSON object beyond the first level, it returns undefined.
For instance, my JSON within the angular translations is this:
$translateProvider.translations('en', {
"SEARCH": {
"SEARCH" : "Recherce",
"ABILITY" : "Abilities",
"MANAGEMENT" : "Management Competencies",
"PERSONAL" : "Personal Suitability"
},
"ABILITIES": {
"TITLE" : "Test Title here",
"ADVISORY": {
"TITLE" : "Advisory Skills",
"QUESTIONS": [
{
"TYPE" : "A",
"LEVEL" : "45",
"DESCRIPTION" : "Can you tell me how awesome you are"
},
{
"TYPE" : "B",
"LEVEL" : "100",
"DESCRIPTION" : "Tell me about your wicked project"
}
]
}
},
"HEADLINE": "Oh No!",
"SUB_HEADLINE": "Looks like you are not amazing"
});
And to begin accessing the data in the JSON object, I do
list = $translateProvider.translations('en');
Now, when outputting items in the console to see if they work I do this:
console.log(list);
var getTitle = list.HEADLINE;
var getSearch = list.SEARCH.ABILITY;
console.log(getSearch);
console.log(getTitle);
This is where it gets odd.
The 'list' returns the JSON array I specified
Getting the HEADLINE returns Oh No!
But getting list.SEARCH.ABILITY returns an undefined
What gives!? I haven't event tried to access the stuff I really want in the really nested array "ABILITIES"
Keep in mind that Angular Translate uses the format {{ 'ABILITIES.ADVISORY.TITLE' | translate }} to output the JSON onto the HTML page
try list.SEARCH["ABILITY"].value
Related
In JS I have a string variable as follows:
Text JSON Data:
[{ "id" : "3", "parent" : "2", "text" : "Lack of Survey Information" },{ "id" : "4", "parent" : "3", "text" : "Lack of Surveyor Resource" },{ "id" : "5", "parent" : "3", "text" : "Land access unavailable" },{ "id" : "2", "parent" : "1", "text" : "Design Incorrect" },{ "id" : "1", "parent" : "16", "text" : "Abortive Work" },{ "id" : "6", "parent" : "1", "text" : "Wrong design info used in construction" },{ "id" : "16", "parent" : "#", "text" : "a" }]
This is output from the following code:
console.log("Text JSON Data:"+strJSON)
var objJSON=$.parseJSON(strJSON)
var objJSON=JSON.parse(strJSON)
It parses using $.parseJSON but does not parse using JSON.parse (I get a bizarre "Uncaught TypeError: JSON.parse is not a function" error though it clearly is). I am using Chrome. It gets weirder, when I just use $.parseJSON successfully and then use JSON.stringify on the returned JSON object it crashes and also throws up an error.
What is weird is that I pass several other JSON strings to this function and they are parsed fine. I thought it might be something to do with the '#' symbol but it seems to parse OK in JSFiddle. I know when using VBA that sometimes an error code is triggered for a line of code when in fact the error is caused by something else. I am wondering if that is what is going on here.
What I need to understand is why this particular string is triggering the bizarre errors.
I have a json object which has an array property 'partners' like below:
{
"_id" : ObjectId("578667b1bb14ca1c2773adaa"),
"customer" : null,
"created" : ISODate("2016-07-13T16:09:21.015+0000"),
"description" : "",
"subject" : "case 2",
"__v" : NumberInt(5),
"partners" : [
ObjectId("57857d93038aacd81ef3ad55"),
ObjectId("57857d93038aacd81ef3ad56")
]
}
I would like to check if an element already exists on the partner array using the following code:
_.includes(case_.partners, partner._id)
Just so u know I am using lodash for this but for some reason is not working and I have no clue why, anyone knows what is the problem here?
What I understanding, the partners sample you provided, is an javascript object, you should try to use primitive type value in the Json.
You can convert ObjectId to String by these way:
ObjectId("578667b1bb14ca1c2773adaa") + '';
or
ObjectId("578667b1bb14ca1c2773adaa").toHexString();
The Json object should be like this:
{
"_id" : "578667b1bb14ca1c2773adaa",
"customer" : null,
"created" : "2016-07-13T16:09:21.015+0000",
"description" : "",
"subject" : "case 2",
"__v" : 5,
"partners" : [
"57857d93038aacd81ef3ad55",
"57857d93038aacd81ef3ad56"
]}
So the solution will be , convert Javascript object into Json object, and then compare them.
I'm trying to find the mongo document by matching the "Tel" field value,
{
"_id" : ObjectId("54f047aa5b9e5c7c13000000"),
"data" : [
{
"Id" : "1",
"Country" : "India",
"Timezone" : "Europe/Paris",
**"Tel" : "03 20 14 97 70",**
"Prenom" : "ddd",
"Email" : "ddd#gmail.com",
"City" : "Chennai",
"date" : "",
"active" : "true"
}
]
}
how to fetch the above document from mongo collection using the below find method without space in "Tel" field,
>db.test.find({"data.Tel":"0320149770"})
Please can anyone help me !!!
If this is what you really want to do on a regular basis then you are best off adding another field to the document that has the string present without any spaces.
The reason why is though there are functions you can perform to do the search, none of the methods are able to use an index to match the document, so this means scanning everything in the collection in order to find a match.
You can do this with JavaScript evaluation in a $where clause:
db.test.find(function() {
return this.data.some(function(el) {
el.Tel.replace(/ /g,"") == "0320149770"
});
});
But don't do that because it's really bad. you are better off just updating all the data instead:
db.test.find().forEach(function(doc) {
doc.data = doc.data.map(function(el) {
el.TelNum = el.Tel.replace(/ /g,"");
})
db.test.update({ "_id": doc._id },{ "$set": { "data": doc.data } });
})
Or something along those lines to have a field without spaces all ready to search on directly.
I am not quite clear on how to make a JSON string. I get error and I want to find out how to correct and it make it.
This is my JSON string:
var drivers = { "driver" : [
{
"id" : 1,
"name" : "Bob",
"age" : "34"
"car" : [
{
"make" : "BMW",
"model" : "3.20",
"colour" : "Silver"
}
]
},
{
"id" : 2,
"name" : "Rob",
"age" : "22"
"car" : [
{
"make" : "Peugeot",
"model" : "306",
"colour" : "Blue"
}
]
},
{
"id" : 3,
"name" : "Maria",
"age" : "23",
"car" : [
{
"make" : "Mazda",
"model" : "3",
"colour" : "Red"
}
]
}]};
This is the get method:
var driverData = JSON.parse(drivers);
function getDriverData() {
return driverData;
}
return {
getDriverData: getDriverData
};
And in this controller I try to get it so I can see if it works:
var drivers = myDriveApi.getDriverData();
console.log(driverData);
Correct me and explain how to make a JSON string, and I want it nested if that's the right term. So each driver has a car, or multiple cars/vehicles. So if I get 1 driver, I want to see his details and his information on owned vehicles.
Your "JSON string" seems to be JavaScript code that you would have in a script tag that creates a drivers JavaScript object with a property called driver that is an array of other objects. To be JSON, you remove the var drivers = and the semicolon at the end leaving just what you would set a variable to in order to create that object.
For instance, let's say you have a "person" variable that is an object with "firstName" and "lastName" properties. This is code you would put into a script to create that variable:
var person = { firstName: "Jason", lastName: "Goemaat" };
Strings, even JSON strings are represented in scripts surrounded by single or double quotes. To see the string value for the JSON of that object, you can do this:
var json = JSON.stringify(person);
This gives you a valid JSON string which will display this in the console:
{"firstName":"Jason","lastName":"Goemaat"}
To set the variable directly you need to enclose it in single or double quotes, but if the string contains those they would need to be escaped. Since this contains only double quotes I will surround it in single quotes to assign it to a variable in my script, then parse that into an object.
var json = '{"firstName":"Jason","lastName":"Goemaat"}';
var person = JSON.parse(json);
JSON property names need to be surrounded in quotes. Most parsers will work if they are not but it is always better to be as correct as you can be. You can use the online tool at jsonlint to check for valid JSON.
No, that's a JavaScript object, which could be converted to a JSON string using JSON.stringify(object).
If you're trying to access it through JavaScript, you might as well skip JSON altogether and just use the object. If you want to test if a JSON string is good, use jsonlint.com.
It's a JavaScript object, not a JSON string.
You're missing commas after Bob and Rob's age. Maria's all good, though.
I created a simple tree based on a TreeStoreModel which is backed by ItemFileWriteStore.
I tried updating my tree by deleting and adding items to the store as below, but could not.
dojo.require("dojo.data.ItemFileWriteStore");
dojo.require("dijit.Tree");
dojo.addOnLoad(function () {
var tmpData = [{
"name" : "Dell",
"type" : "business",
"businessid" : "1",
"projectid" : "1",
"submenu" : [{
"name" : "OTP",
"type" : "product",
"productid" : "100"
}
]
}
];
var tmpData1 = [{
"name" : "Lenovo",
"type" : "business",
"businessid" : "1",
"projectid" : "1",
"submenu" : [{
"name" : "OTP",
"type" : "product",
"productid" : "100"
}
]
}
];
var store = new dojo.data.ItemFileWriteStore({
data : {
label : 'name',
items : tmpData
}
});
var treeModel = new dijit.tree.TreeStoreModel({
store : store,
query : {
type : 'business'
},
childrenAttrs : ["submenu"]
});
var divTestTree = dojo.create("div", null, dojo.body(), "first");
var mytree = new dijit.Tree({
model : treeModel
},
divTestTree);
/* Tree is created at this point */
/* Delete the item from the store, for some reason tempData is being reset*/
store.deleteItem(tmpData[0]);
/* save */
store.save();
/* Tree shows up no more at this point */
/* Try adding new item to the store */
store.newItem(tmpData1[0]);
/* save */
store.save();
/*nothing happens! */
});
I followed the lead from here, I must be missing something very trivial here. Please help.
Taken from that page
How do I refresh a Tree from the store?
This isn’t supported. The store needs to notify the tree of any changes to the data. Currently this is really only supported (out of the box) by dojo.data.ItemFileWriteStore, as setting up a client-server dojo.data source where the server notifies the client whenever the data has changed is quite complicated, and beyond the scope of dojo, which is a client-only solution.
The only way to update the tree is to re-draw it again :(
For anyone coming here looking for a solution the problem, 'I suspect there is a bug' is the only answer I got in the forum, so may be this is a bug - goo.gl/M7xg7