JADE & NodeJS access JSON object property array - javascript

I have a JSON object
{ "ID": "Test", "Data": [ "Line1", "Line2" ] }
And the following JADE Code
h4 {{object.ID}}:
ul
each line in object.Data
li= line
I can display the ID but I cant iterate over the array it errors out on the each line with "Cant Read Property "Data" of undefined,
doing a
p {{object}}
prints the above JSON just fine..
any ideas why this is?
Update: The Object is being passed in from an angular controller.

Looks like it was the angular.
li(ng-repeat='note in object.Data') {{note}}
not the JADE.

Related

Issue with i18n parsing

I am getting JSON fro backend, which I need to parse on UI.
For all the keys from the JSON, I have to translate them and show on UI.
Eg:
i18n.t('key') will give me translated value.
But for some keys like 'name', 'date'
Eg:
i18n.t('name')
translation is giving following output
"key 'translation:name (en-US)' returned a object instead of string."
Could you please help me how to deal with this scenerio.
If you have for example following JSON from your service
{
"i18n": {
"name": "translation1",
"name2": "translation2"
}
}
You can just use it as following
var mytranslation = getTranslationsFromService();
console.log(mytranslation.i18n.name) //result: translation1
console.log(mytranslation.i18n.name2) //result: translation2
var getTranslationsFromService = function() {
//Get result from service, where the result looks like the JSON above.
}
I hope I could help.
Kind regards.

How to get values from this data structure

An ajax request (stored in a variable named results) is returning this data as a response:
Object {hits: Object, links: Object}
hits:Object
hits:Array(2)
0:Object
active:true
email:"user1#example.com"
id:1
links:Object
__proto__:Object
1:Object
active:true
email:"user2#example.com"
id:2
links:Object
__proto__:Object
length:2
__proto__:Array(0)
total:2
__proto__:Object
links:Object
__proto__:Object
What sort of data type it has? I thought it is json but using JSON.parse(results) returns this error:
Uncaught SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
How can I get access to the Array inside it? I need to get the email addresses and ids. It's probably not relevant but I'm using it in a ReactJS component.
According your response, it's already a JSON object.
So if you want to access the hits property, here is the code: results.hits.
Also you mentioned the following:
It recognizes the first hits, but not the second one.
Can you please try to console.log(results.hits) and share the output here? I guess your response object has only one hits property and it's type is array.
Observation : As response is already a JSON Object so no need to Parse it again.
Suggestion : Use array.map() method to iterate the array and fetch the required elements.
DEMO
var results = {
"hits": {
"hits": [
{
active:true,
email:"user1#example.com",
id:1
},
{
active:true,
email:"user2#example.com",
id:2
}
]
},
"links": {}
};
var res = results.hits.hits.map(function(item) {
return item.email;
});
console.log(res);

Handling JSON objects with UUID keys while templating with Handlebars.js?

I have a nested JSON object which looks like this
review:{
body:{
"1cfd0269-1b8f-418f-a7b2-45eb2fa7562b": "Text1",
"38d14dcd-6e70-46f9-8d81-9c5237cb7393": "Text2",
"01485828-39ef-4929-9e96-19758375eb9b": "Text3",
}
created_at: "2014-06-25T07:42:19Z",
id: "ea07aaa3-9db6-4868-b6f1-0887ef77f8ba",
product_id: "eb5a7c9c-c20d-4539-b04f-5a3fd8d26c87",
updated_at: "2014-06-25T07:42:19Z"
written_by: "09b3c6f1-cbcb-4544-8cc3-d073d17a8552",
written_on: "2014-06-25"
}
The JS is pretty straight-forward:
var context = {review:review};
html = template(context);
The template is largish but relevant part is here
<textarea class="review-body"> {{body["1cfd0269-1b8f-418f-a7b2-45eb2fa7562b"]}} </textarea>
I am getting a Parse error while trying to access attributes of body in an html template. Any ideas why this is happening?
Uncaught Error: Parse error on line 5:
...iew-edit-context">{{body["1cfd0269-1b8f-418f-a7b2-45eb2fa7562b"]}}</tex
-----------------------^
Expecting 'ID', 'DATA', got 'INVALID'
From the doc on Handlebars expressions :
To reference a property that is not a valid identifier, you can use segment-literal notation, [ :
{{#each articles.[10].[#comments]}}
{{/each}}
which means you have to use {{body.[1cfd0269-1b8f-418f-a7b2-45eb2fa7562b]}} in your template.
See http://jsfiddle.net/nikoshr/KVg9P/ for a demo.
yes that is work on webpage and angularJS expression retrieve it fine , but if you try to do the same in JavaScript it will return undefined error . for example if you try something like this
console.log(body.[1cfd0269-1b8f-418f-a7b2-45eb2fa7562b]);
or even
console.log(body['1cfd0269-1b8f-418f-a7b2-45eb2fa7562b']);
I do not the solution for it , but somewhere mention JavaScript is not like those mines sign on UUID , myself still looking for solution but temporary I am trying to remove the mine sign.

Deleting elements out of a json file without getting "null"

I have a JSON formatted text file that I'm using as kind of a database.
I read the file, use JSON.parse to turn it into an object, use delete on an element, then I JSON.stringifythe object and write it back to the file.
However the resulting file has "null" in the place where the object used to be, which isn't proper JSON, so my program will crash the next time the file is parsed. I don't like it.
How can I delete elements from my file without getting "null" where the element used to be?
Here's how I do it:
data = fs.readFileSync("./manifest/test.json");
contents=JSON.parse(data);
//some logic
delete contents.customers[i].files[j];
fs.writeFileSync("./manifest/test.json",JSON.stringify(contents,null,4));
And the resulting file before:
{
"customers": [
{
"customer": "test",
"files": [
{
"name": "test.flv",
"location": "cloudfront.url.com/check.flv"
}
]
}
]
}
And after:
{
"customers": [
{
"customer": "test",
"files": [
null
]
}
]
}
contacts.customers[i].files.splice(j, 1);
The result is still valid JSON though. JSON.parse should succeed. The error may lie elsewhere.

View json parse object structure in eclipse javascript

I would like to view the json parse objects structure in eclipse. Due to the cross domain issue I am unable to use the console.log() to view the structure in chrome. How else can I view this [object Object] returned structure from Json.Parse ?
Use JSON.stringify(object), it will convert your object to a string(then you can alert, write or whatever you want).
EDIT
If this is the returned value from the request:
'{"Date":"05\/25\/2013","Description":"New2","Details":"New2","Item‌​RequestedID":"1343","Picture":,255,217],"Status":1,"User":"2120","Use‌​rName":"Ind1"}'
It is not yet parsed, so you need to use JSON.parse(string) to parse it to a valid JS Object. Once it is parsed, this will be the structure of your object:
{
"Date": "05\/25\/2013",
"Description": "New2",
"Details": "New2",
"Item‌​RequestedID": "1343",
"Picture": ,
255,
217], "Status": 1,
"User": "2120",
"Use‌​rName": "Ind1"
}
var obj = JSON.parse(result)//where result is the string above and obj is the parsed object with this structure
Then you will be able to access what you need doing obj.Date, obj.Description etc

Categories

Resources