I'm using docxtemplater to create a word document based on JSON values (using surveyJS). In case some questions are not answered, the variable shows "undefined" in the final document.
How can I make sure that any "undefined" output is highlighted? I found some answers on how to use nullGetter to replace "undefined" with a custom string but nothing on text background.
The JSON looks something like this:
{
"nameIndividual": "John Smith",
"mgmt": "Management Name",
"mgmtName": "Tim Test",
"address": "854 Test Avenue, Example City",
"email": "test#management.com",
"socialMediaOptions": [
"Twitter",
"YouTube",
"Instagram",
"Facebook"
]
}
Is there a way to do this in the word template itself without having to write
{#nameIndividual == undefined}Undefined{/} for every single variable...
If not, how can this be done with Javascript?
Any help is highly appreciated!
Thanks!
Related
Hi recently in one of the interview i gone through this question.
How to minify json response
{
name: "sample name",
product: "sample product",
address: "sample address"
}
this what the question. I dont know how to minify and the process behind it. Can any one explain? please
Thanks in Advance.
You can parse the JSON and then immediately re-serialize the parsed object:
var myJson = `{
"name": "sample name",
"product": "sample product",
"address": "sample address"
}`;
// 'Minifying' the JSON string is most easily achieved using the built-in
// functions in the JSON namespace:
var minified = JSON.stringify(JSON.parse(myJson));
document.body.innerHTML = 'Result:<br>' + minified;
You'll have to perform the minification server-side to get an improvement in response size. I suppose most languages support an equivalent to the above snippet. For example, in php one might write this (if your server runs php, of course):
$myJson = '{
"name": "sample name",
"product": "sample product",
"address": "sample address"
}';
$minified = json_encode(json_decode($myJson));
The obvious way would be to strip the whitespace. Beside that you could also map the keys to something shorter like a, b, c.
Also if you want to be a jerk about it; you could tell them that valid json would have quotes around the keys. This seems to be a js object, not json.
Another method could be to create it as a array with implicit keys - basically the first item in the array maps to key 'name', the second to 'product' etc - this requires both sender and receiver of the message to know exactly the order of keys, so in your example it could be reduced to
["sample name","sample product","sample address"]
you can't really make it much shorter than that. If you have complex objects with sub keys you can use nested multidimensional arrays eg.
["sample name","sample product",["sample address1","sample address2"]]
This would be a good option for things like multiplayer game server networking where message size is a major factor for reducing latency and the client and server both know how the message is structured.
It's not strictly Json, but sometimes you have to use pragmatic solutions for performance.
I am currently using Angular to read in a json file and output it onto a table.
Because some of the objects are a little different, I want to make a check to see if job.text exists.
[
{
"job": {
"href": "www.google.com",
"text": "Google"
},
"api": "Some Text Here"
},
{
"job": "Yahoo",
"api": "More text here"
}
]
If job.text exists, then display job.text
else display job
Here is my html from angular but only displaying objects with job.text, otherwise it returns nothing.Is there a simple way to write a check statement to make sure I can display both types of objects?
<td><a ng-href="{{item.job.href}}" target="_blank">{{item.job.text}}</a></td>
Use a ternary:
{{item.job.text ? item.job.text : item.job}}
You should try and normalize your data struct a bit - seems odd that job may contain an object or a simple text field.
I have the following json, I don't have any control over this output unfortunately.
{
"questions": {
"9733": {
"text": "Star Trek or Star Wars?",
"answers": {
"41003": "Star Trek",
"41004": "Star Wars",
"41005": "Neither is superior in my opinion; both great in their own ways",
"41006": "Not a fan",
"41007": "I don't have an opinion on this"
}
},
"25272": {
"text": "Which of these summer movies are you looking forward to the most?",
"answers": {
"99545": "World War Z",
"99546": "Monsters University ",
"99547": "White House Down",
"99548": "Man of Steel",
"99549": "Lone Ranger",
"99550": "The Wolverine"
}
},
"27547": {
"text": "Should the U.S. attack Syria?",
"answers": {
"107453": "Yes",
"107454": "No"
}
}
}
}
I am using json.parse to parse this. To get the text of the first question I would normally do something like this.
var jsonData = JSON.parse(data);//Where data = the json above
console.log(jsonData.questions.9733.text);//Obviously this fails
However javascript doesn't like that number in there obviously. How would you recommend accessing the text of the first question? I would prefer the json to be setup better with in an array of questions instead. Unfortunately I don't have any control over the output of this JSON.
I'm also not going to be aware of the what the keys are as they come across, but thats a whole other issue. I'm willing entertain any suggestions on how to parse this thing as I've never had to parse such a strange JSON output.
You need to use bracket notation:
console.log(jsonData.questions["9733"].text);
But because the value inside the brackets will be automatically converted a string, this would also work:
console.log(jsonData.questions[9733].text);
Note however, that using non-strings is as property names generally bad form and could lead to some subtle problems, e.g. if the property name was "001", then [001] would not work.
Why don't you try?
jsonData["questions"]["9733"]
How to access a numeric property?
I believe you can access the data via same syntax as in an array:
console.log(jsonData.questions[9733].text);
If you have to use numbers as keys... you can access them like this:
var text = jsonData.questions["9733"].text;
Edit: You can also access it with the number 9733. It doesn't have to be a string. Only needs to be a string if the key is non-numeric.
Try using Ason, If you are using Java 8. Gradle dependency compile 'org.arivu:ason:1.0.3'.
Java code as follows
Ason ason = new Ason();
Object json = ason.fromJson("<<JSON String!>>");
System.out.println(Ason.getJson(json, "questions.9733.text", null)):
I have the following standard Javascript assignment statement returned as the output form a webservice call. I need to get the JSON array object out of it and was wondering if I can somehow use NSRegularExpression to do that.
I have no control over the web service so it has to continue returning a Javacript assignment stmt as in the attached snippet below.
Can anyone suggest an objective-c code snippet that accomplishes this task? I could receive sub-array and sub hashes as part of the individual elements as well...
var collection = [
{
"id": "4444",
"name" : "Bill Smith",
"position" : "tester"
},
{
"id": "4444",
"name" : "Bill Smith",
"position" : "tester"
}
];
You can write something like this:
NSArray *myJSONArray = #[
#{
#"id": #"4444",
#"name" : #"Bill Smith",
#"position" : #"tester"
},
#{
#"id": #"4444",
#"name" : #"Bill Smith",
#"position" : #"tester"
}];
You can use NSJSONSerialization to take an NSData with your JSON, and parse it into a native Foundation object.
In this case, if you receive the NSData from the JSON snippet you pasted, you'd have an NSArray returned which contains 2 NSDictionary objects. Here is how you can accomplish that using NSJSONSerialization:
[NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableLeaves error:&error];
Note that the documentation says:
The data must be in one of the 5 supported encodings listed in the JSON specification: UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE. The data may or may not have a BOM. The most efficient encoding to use for parsing is UTF-8, so if you have a choice in encoding the data passed to this method, use UTF-8.
So do ensure that you receive the data in one of those encoded formats. This is the quickest way to do it, it's built in to Cocoa and is very, very fast. Also, you can have different options than NSJSONReadingMutableLeaves.
Hope this helps :)
oh well. I have not found any clever solution to this so I simply took a substring from the entire response object to extract all content between first and last {} or first and last []. Works just as well.
Hi I am trying to parse the following bit of json with Jquery so far I can get everything out of the results that I want apart from one crucial piece of information the performance tags.
Each json result is wrapped in an event tag and then within this there is info like time and date etc formatted in the following way
"location": {
"lng": -0.1187418,
"city": "London, UK",
"lat": 51.4681089
},
"start": {
"time": "19:30:00",
"datetime":"2010-02-16T19:30:00+0000",
"date": "2010-02-16"
},
I have managed to loop through this and parse it to html. However there is one set of tags for 'performance' that are formatted differently.
"performance": [{
{
"artist": {
"uri": "http://www.songkick.com/artists/288696-vampire-weekend",
"displayName": "Vampire Weekend",
"id": 288696,
"identifier": [{"mbid": "af37c51c-0790-4a29-b995-456f98a6b8c9"}]
}
"displayName": "Vampire Weekend",
"billingIndex": 1,
"id": 5380281,
"billing": "headline"
}
}],
now in my for loop i am running the following code which displays the performance information in the console.
var events = data.resultsPage.results.event;
for (var i = 0, l = events.length; i < l; i++) {
console.log(events[i].performance); }
However when i try to go into the structure like i have been with the other elements I get returned undefined i.e
console.log(events[i].performance.displayName);
Do I have to do this in a different way because of the use of the [ ] brackets in the performance tag in the Json?
Thanks in advance
Assuming that what you posted is not exactly what your JSON looks like (because what's posted has a syntax error), the "performance" attribute is an array of objects. To get at the "displayName", therefore, you'd need to know which element of the "performance" array you wanted. You'd then access it by index.
console.log(events[i].performance[j].displayName);
(assuming you looped through the "performance" array with the variable "j".)
Try to validate your returned JSON object here, I guess there is some issue with the JSON output..