How to Display Array Value to through Index in DUSTJS - json

My Question is simple: Is there anyway to display array value through its index in DUSTjs syntax.
Sample JSON :
{ names:
[
"Jhon",
"Peter",
"Curly"
]
}
with the above json sample, I just want to display any of the names through its index in DUST syntax.
Note: We are able to display all names, but i need any of the names to be displayed as output through its index (Eg : names[0] something like this or by any other way).

when iterating $idx will give you the index so for example showing them as <li> elements:
{#names}
<li>
{names[$idx]}
<li>{~n}
{/names}
otherwise just plain
{names[1]}
will give you first element

This is quite easy to achieve:
{names[0]} gives you Jhon
{names[2]} gives you Curly
and so on.. Hope this helps.

if you use the linkedin dustjs fork, you can do that:
take a look here: http://linkedin.github.com/dustjs/test/test.html. There are a lot of examples.
This is the wikki: https://github.com/linkedin/dustjs/wiki
and this is the code repo:
https://github.com/linkedin/dustjs

Related

Use XPath to found text row in query json

To introduce me : I'm not developer, and I want to control the element is present in my json.
I have this type of json, and i want to control for exemple that "id = display-page" but I don't know how can I structure of my xpath to found it
"text": "{\"id\":\"display-page\",\"recoVideo_hasRecoLoaded\":\"\",\"recovideo_requiredEngine\":\"tp1top\",\"recovideo_executedEngine\":\"tp1top\",\"recovideo_fallbackEngine\":\"\",\"recovideo_abtestEngine\":\"\",\"recovideo_recoVideoId\":\"top-video-web-desktop-windows-PERSONA_DEFAULT\"
I try something like $..request.text.[?(#.name =='id')].value.display-page
but it doesn't work :(
Someone to help me ?
Thanks !
Antoine

How to add more XPATH in parsefilter.json in stormcrawler

I am using stormcrawler (v 1.16) & Elasticsearch(v 7.5.0) for extracting data from about 5k news websites. I have added some XPATH patterns for extracting author name in parsefilter.json.
Parsefilter.json is as shown below:
{
"com.digitalpebble.stormcrawler.parse.ParseFilters": [
{
"class": "com.digitalpebble.stormcrawler.parse.filter.XPathFilter",
"name": "XPathFilter",
"params": {
"canonical": "//*[#rel=\"canonical\"]/#href",
"parse.description": [
"//*[#name=\"description\"]/#content",
"//*[#name=\"Description\"]/#content"
],
"parse.title": [
"//TITLE",
"//META[#name=\"title\"]/#content"
],
"parse.keywords": "//META[#name=\"keywords\"]/#content",
"parse.datePublished": "//META[#itemprop=\"datePublished\"]/#content",
"parse.author":[
"//META[#itemprop=\"author\"]/#content",
"//input[#id=\"authorname\"]/#value",
"//META[#name=\"article:author\"]/#content",
"//META[#name=\"author\"]/#content",
"//META[#name=\"byline\"]/#content",
"//META[#name=\"dc.creator\"]/#content",
"//META[#name=\"byl\"]/#content",
"//META[#itemprop=\"authorname\"]/#content",
"//META[#itemprop=\"article:author\"]/#content",
"//META[#itemprop=\"byline\"]/#content",
"//META[#itemprop=\"dc.creator\"]/#content",
"//META[#rel=\"authorname\"]/#content",
"//META[#rel=\"article:author\"]/#content",
"//META[#rel=\"byline\"]/#content",
"//META[#rel=\"dc.creator\"]/#content",
"//META[#rel=\"author\"]/#content",
"//META[#id=\"authorname\"]/#content",
"//META[#id=\"byline\"]/#content",
"//META[#id=\"dc.creator\"]/#content",
"//META[#id=\"author\"]/#content",
"//META[#class=\"authorname\"]/#content",
"//META[#class=\"article:author\"]/#content",
"//META[#class=\"byline\"]/#content",
"//META[#class=\"dc.creator\"]/#content",
"//META[#class=\"author\"]/#content"
]
}
},
I have also made change in crawler-conf.yaml and it is as shown below.
indexer.md.mapping:
- parse.author=author
metadata.persist:
- author
The issue i am facing is : I am getting result only for 1st pattern (i.e. "//META[#itemprop="author"]/#content") of "parse.author". What changes I should do so that all patterns can be taken as input.
What changes I should do so that all patterns can be taken as input.
I read this as "How can I make a single XPath expression that tries all different ways an author can appear in the document?"
Simplest approach: Join the all expressions you already have into a single one with the XPath Union operator |:
input[...]|meta[...]|meta[...]|meta[...]
And since this potentially selects more than one node, we could state explicitly that we only care for the first match:
(input[...]|meta[...]|meta[...]|meta[...])[1]
This probably works but it will be very long and hard to read. XPath can do better.
Your expressions are all pretty repetitive, that's a good starting point to reduce the size of the expression. For example, those two are the same, except for the attribute value:
//meta[#class='author']/#content|//meta[#class='authorname']/#content
We could use or and it would get shorter already:
//meta[#class='author' or #class='authorname']/#content
But when you have 5 or 6 potential values, it still is pretty long. Next try, a predicate for the attribute:
//meta[#class[.='author' or .='authorname']]/#content
A little shorter, as we don't need to type #class all the time. But still pretty long with 5 or 6 potential values. How about a value list and a substring search (I'm using / as a delimiter character):
//meta[contains(
'/author/authorname/',
concat('/', #class, '/')
)]/#content
Now we can easily expand the list of valid values, and even look at different attributes, too:
//meta[contains(
'/author/authorname/article:author/',
concat('/', #class|#id , '/')
)]/#content
And since we're looking for almost the same possible strings across multiple possible attributes, we could use a fixed list of values that all possible attributes are checked against:
//meta[
contains(
'/author/article:author/authorname/dc.creator/byline/byl/',
concat('/', #name|#itemprop|#rel|#id|#class, '/')
)
]/#content
Combined with the first two points, we could end up with this:
(
//meta[
contains(
'/author/article:author/authorname/dc.creator/byline/byl/',
concat('/', #name|#itemprop|#rel|#id|#class, '/')
)
]/#content
|
//input[
#id='authorname'
]/#value
)[1]
Caveat: This only works as expected when a <meta> will never have both e.g. #name and #rel, or if, that they at least both have the same value. Otherwise concat('/', #name|#itemprop|#rel|#id|#class, '/') might pick the wrong one. It's a calculated risk, I think it's not usual for this to happen in HTML. But you need to decide, you're the one who knows your input data.

Failing to get a proper json from a freemaker list

I'm trying to get a working JSON output (an array with x amount of objects) from a freemaker ftl file. If there is only 1 object in th array of "loggedInUsers" then the code below works. If there are more than 1, then the JSON breaks. I know a comma should separate the separate between each, but the problem comes when I add one after the closing brace. Any help would be greatly appreciated.
[
<#list loggedInUsers as user>
{
"user": "${user}"
}
</#list>
]
If I understand well, you want to add a comma except after the last item. In that case use the #sep directive, like }<#sep>,</#sep>. (See also: http://freemarker.org/docs/ref_directive_list.html)

How can I deal with a column containing a list of items

I want to make a dropdown of categories so I call https://data.seattle.gov/resource/n6as-h2bj.json?$select=categories&$group=categories Some of the rows have multiple categories. Is there anyway to handle this via SODA or do I have do it via javascript?
Sorry for the delay in getting back to you, #tim-clemans. I missed this one.
I assume you're referring to the values separated by pipe (|) characters, correct?
{
"categories": "Suspicious Circumstance|Alarm|Crisis"
},
{
"categories": "Traffic|Suspicious Circumstance"
},
Unfortunately there's no functionality to do that from right within SoQL. A str.split(/|/) should probably do the trick though

AngularJS - Conditionally display key and value if they exist

This may be a little confusing to describe.
Basically, I am parsing multiple external JSON feeds that display in different views depending on the 'active tab' displayed. They both share the same partial template, so they both look exactly the same, just different content.
The problem that I am facing now is, that in some feeds, some keys are placed in an array and others are not.
For example, the feeds parses this kind of data:
JSON Feed 1 - One 'attributes' inside of 'link'
"link":{
"attributes":{
"href":"www.link1.com"
}
}
JSON Feed 2 - Two 'attributes' inside of 'link'
"link":[
{
"attributes":{
"href":"www.link1.com"
}
},
{
"attributes":{
"href":"www.link2.com"
}
}
]
The only way I am able to get the value "www.link1.com" is via:
For Feed 1:
link1
And for Feed 2:
link1
I am trying to figure out what would be the best way to do:
1) If link[0] exists - display it, else if [link] exists, display that instead.
2) Or if targeting the activeTab would be safer? For instance, if activeTab = view2 or view4, use [link][0], else if activeTab = view1 or view3 use [link], else if I do not want it to be displayed, do not display anything.
Also a relatable question, if I am on view2 can I only display [link][0] on that view?
Any feedback would be appreciated. Thanks!
In your model controller, you can reconstruct the JSON objects to make them similar. The value of link in both feeds should be an array.
Then in your template you can simply use ngRepeat to get the items from inside the array.
Okay - so I found a solution to one of the questions above: "How to only display [link][0] in a specific view"
Pro: It's a simple code that depends on the activeTab / view that is being displayed.
Con(?): Since I am really a newbie to AngularJS - not sure if this is the best solution.
Basically:
Depending on the ng-view that is currently displayed, than a specific JSON object will be displayed, such as:
<a ng-show="activeTab == 'view1' || activeTab == 'view3'" ng-href="{{item['link'][0]['attributes']['href']}}">
<h6>Link1 from Feed2</h6>
</a>
Although the primary question is still unresolved: How to swap/switch JSON objects (key,values) if one exists, and not the other. I am still definitely trying to find a solution, although any help is still appreciated.
Please let me know what you think, or how I can improve the solution to the problem!
Thanks!
Roc.