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.
Related
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!
I am trying to create a dropdown-list where you can select current employees from. However, in order to not invalidate old data, the names of old employees should also be valid entrances for the field. I am new to alpacajs, however i was thinking about doing something like this:
.alpaca({
"data": "Coffee2",
"options": {
"label": "Ice cream",
"optionLabels": ["", "Chocolate", "Coffee"] //Here i would want e.g. that Vanilla was hidden from the user.
},
"schema": {
"required": true,
"enum": ["Vanilla", "Chocolate", "Coffee"]
}
});
This solution however would then make it so you would not see the former employees names on previous data but only " ". I have also thought about doing it with observables, however i cannot get it to work.
You can use de element "disallow": ['property'], to remove the elements from the list
After searching online and not finding anything, I decided to post here.
What I have so far:
JSON
I have a JSON file, with several list of words that I need to retrieve according to user input. They look something like this:
{
"length": 10,
"targetWords": {
"SOME_NAME": [
{
"words": [
"xyz",
"xyz",
"xyz",
"xyz",
"xyz",
"xyz",
"xyz",
"xyz",
"xyz",
"xyz"
]
},
{
"text": "Some example text",
"letter": "X",
}
],
}
localStorage input
I ask the user to choose an option and "record" that answer in a variable. Let's call it "userInput".
userInput = localStorage.getItem("something")
I get the JSON and pass it to a variable (I'm using Phaser Framework)
TARGET_SOUNDS_DATA = this.game.cache.getJSON('targetSounds')
What I want now is to access the values inside it. I can do it if I go about it like this:
TARGET_SOUNDS_WORDS_ARRAY = TARGET_SOUNDS_DATA.targetWords.SOME_NAME[0].words
But unable to do so if I use the user answer/input like this:
TARGET_SOUNDS_WORDS_ARRAY =TARGET_SOUNDS_DATA.targetWords.userInput[0].words
This way I only get undefined
So I know I'm doing something wrong but what?
Any help/hints are welcomed! Thanks for your time!
Cheers,
J
You can use square brackets to select a property with a variable
TARGET_SOUNDS_WORDS_ARRAY = TARGET_SOUNDS_DATA.targetWords[userInput][0].words
You have to parse it and then to get result based on userInput access it like this
TARGET_SOUNDS_WORDS_ARRAY =TARGET_SOUNDS_DATA.targetWords[userInput][0].words
As I understand, and maybe I'm wrong, localStorage is key/value storage only. You'll have to convert the json to a string, store it, then retrieve it and convert it back to an object when you want to use it.
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..
I have a json output that looks like this.
{
"38467": {
"name": "Tony Parker",
"book": [
{
"title": {
"name": "MediaWorks"
},
},
],
}
"59678": {
"name": "Ray Thomas",
}
}
Actually json output is a lot bigger list. But I want to only save author's name and their publisher. I want save multiple individual records for a single model.
I am using jquery to assign input elements their values.
$('#Author' + i + 'Title').val(response...);
But it is not working.
I appreciate any help.
Thanks.
JSON is just javascript data - a javascript object. Assign the "decoded" JSON data to a variable (say var dat), then you can access members the normal object/array away: dat[38467]['name'] is Tony Parker and so on.
comment update:
once you've decoded/stored the data, you can use a regular javascript foreach loop to go over it:
for (var bookID in booklist) {
var author = booklist[bookID]['name'];
var title = booklist[bookID]['book'][0]['title']['name'];
// ...do stuff here...
}
There's nothing magical about JSON, it's just javascript data packed up for easy/clean transmission. Of course, if you're using a framework such as jQuery or MooTools, you'd be better off using their own .each() operators, otherwise you'll get various bits of useless fluff from the for() loop.
edit: fixed code sample as per marimuthu's comment (good catch, thanks).