How to query site data in hugo using where - hugo

In a hugo template how can I do a lookup on .Site.Data. Assuming I have a data structure like this:
{
"my-blog-post": {
"Version": 21,
"Revision": 0,
"Code": "my-blog-post",
"Name": "My Blog Post",
"Parent": "the-parent-post"
},
"another-post": {
"Version": 21,
"Revision": 0,
"Code": "another-post",
"Name": "Another Post",
"Parent": "the-parent-post"
}
}
Inside the detail page for a blog post, I would like to get the list of all the posts that have the current post as the parent. Basically I would like to query posts that have "the-parent-post" as the value of Parent field.
I can achieve this using range but I need to get them using where.
I experimented with expressions like this but no joy.
{{$child_posts := where (where .Site.Data "Section" "blog") "Parent" "the-parent-post" }}
I can do this:
{{$posts := where $site.Pages "Section" "blog"}}
{{ range sort $posts "Name" "asc" }}
{{ $post := . }}
{{if eq $post.parent $currentPage.Code}}
...Do something...
{{end}}
{{end}}
But it is not ideal and not what I need.
Thanks in advance.

Your are trying to range through a map of maps. You need an array of maps.
https://discourse.gohugo.io/t/filtering-data-in-json-using-where/35287/2

Related

Extract just one value from json array result in vb6

The result of json that I received from the SMS sending panel by Rest API is as follows and display in textbox :
{
"status": "OK",
"code": "OK",
"message": "Ok",
"data": {
"messages": [
{
"number": "+9710001529",
"message": "Hello World",
"sender": "+97911308600",
"time": "2022-07-12T20:12:14Z",
"type": "normal"
},
{
"number": "+9710001529",
"message": "Just For Test",
"sender": "+979051931024",
"time": "2022-06-28T23:15:22Z",
"type": "normal"
},
{
"number": "+9710001529",
"message": "Test",
"sender": "+979565547989",
"time": "2022-01-28T16:04:50Z",
"type": "mobilepanel"
},
{
"number": "+9710001529",
"message": "Comment",
"sender": "+979102900089",
"time": "2018-06-16T22:23:23Z",
"type": "normal"
}
]
},
"meta": {
"total": 37,
"pages": 4,
"limit": 10,
"page": 0,
"prev": null,
"next": "http://0.0.0.0:80/v1/inbox?limit=10\u0026page=1"
}
}
Now, I need to fetch the first mobile number with the name "sender" and show it in textbox for searching in database. The result should look like this: +97911308600.
I downloaded VB-JSON, VB6 JSON Parser Class Library and try to get a specific field from JSON data structure. if json result was not array like this code works good:
{
"status": "OK",
"code": "OK",
"message": "Ok",
"data": {
"credit": 2655946.6574392905
}
}
my code :
Dim p As Object
Set p = json.parse(Text1.text)
Debug.Print p.Item("data").Item("credit")
My expected output :
2655946.6574392905
The problem is when the Json result is a collection of arrays. How can I read first "sender" value as Mobile number just like value of "credit"?
Please guide me or post code. Thank you
Short answer:
o.item("data").item("messages").item(1).item("number")
Easy way to find out..
Put a breakpoint right after the .Parse(...) call and then stop your execution and proceed to the "Immediate Window".
Start debugging using the TypeName(...) method from VB6.
?TypeName(o) => Dictionary
?TypeName(o.item("data")) => Dictionary
?TypeName(o.item("data").item("messages")) => Collection
So, this is how the parser renders the array of objects. Instead of a Dictionary, it is a collection. Collections can be indexed via numeric keys (base 1).
Thus,
?TypeName(o.item("data").item("messages").item(1)) => Dictionary
Now, we're back to a Dictionary, so we're back on a JSON object as opposed to an array. You could also have done .Count on the Collection if you needed to iterate.
And, then, finally, extract the field from the collection you wanted:
o.item("data").item("messages").item(1).item("number") => +9710001529
And, you're there.
Try using tiny mdJson.bas module from this thread instead of some bloated components and libraries.
It's a single file JSON parser implementation that uses nested VBA.Collections to represent JSON objects and arrays and uses JSON Path expressions to access nested data.
Your code would become this
Dim p As Object
Set p = JsonParseObject(Text1.text)
Debug.Print JsonValue(p, "data/credit")
You can read the first sender like this
Debug.Print JsonValue(p, "messages/0/sender")
. . . or using JSON Path expressions like this
Debug.Print JsonValue(p, "$.messages[0].sender")

ng-repeat with variabile as something

Let's imagine I have an array I want to iterate. So standard way I would just
<div ng-repeat=obj in arr>
Which works correctly ofc. But let's also imagine situation i will work whole time just with obj property data.
So for example:
<span>{{obj.data.firstName}}</span>
<span>{{obj.data.lastName}}</span>
It works but it start looking ugly, and just in case it will have one aditional subobject or array it can go even worser.
So I hope if there is something like:
<div ng-repeat=obj.data as o in arr>
I didnt find any information about it in Angular Doc but I belieave it could be possible or atleast work arounded.
Any idea?
EDIT:
e.g. array:
[{
"data": {
"Name": "MyName",
"Gender": "female"
},
"data2": {
"nothing": true
}
}, {
"data": {
"Name": "MyNam2e",
"Gender": "male"
},
"data2": {
"nothing": true
}
}]

How do I filter to show only the first three properties from my JSON using Angular

I have the following JSON file to read and I want to only display some particular keys I want from the JSON.
[
{
"recommendation_id": "cuhwbchuwbcuhw-ccnahcbhb-12",
"title": "hellow world",
"content": "Hello angular",
"name": "John",
"home": "USA"
},
{
"recommendation_id": "cuhwcacabchuwbw-ccnahcbhb-32",
"title": "ng-show",
"content": "ng-show is amazing",
"name": "Google",
"home": "USA"
},
......
{
"recommendation_id": "guwqiwu212wbcuhw-ccnahcbhb-12",
"title": "Awesome",
"content": "Hello Awesome",
"name": "Mike",
"home": "Canada"
}
]
Here is what I use to access the header for my table using Jade.
table(id="allRecommendations")
thead
tr
th(ng-repeat='(key, value) in recommendationsAll[1]') {{key}}
And the output is
recommendation_id title content name home
But I only want the first three items, which should display like the following:
recommendation_id title content
So, what do I miss on my filter? I am interested in getting the object properties not my property details. How do I filter to show particular JSON properties?
This will solve the problem.
table(id="allRecommendations")
thead
tr
th(ng-repeat="(key, value) in recommendationsAll[1]", ng-show="key == 'recommendation_id' || key == 'title' || key == 'content' " ) {{key}}

How to append data to a resultset in cakephp 3

I have a simple json view like this
{
"articles": [
{
"id": 1,
"title": "Article one",
},
{
"id": 2,
"title": "Article two",
}
]
}
This results are paginated, and i'd like to append the pageCount to the results, something like this.
{
"articles": [
{
"id": 1,
"title": "Article one",
},
{
"id": 2,
"title": "Article two",
}
],
"pageCount": 5
}
How can i achieve this? i can't append it directly because it's a resultset object.
Should i be doing this in the view/controller/model?
thanks a lot!
The _serialize key
Given that you are using the JsonView and auto-serialization
Cookbook > Views > JSON and XML Views > Using Data Views with the Serialize Key
you can simply add another variable for the view, and mark it for serialization, like
$this->set('articles', $this->paginate());
$this->set('pageCount', $this->request->params['paging'][$this->Articles->alias()]['pageCount']);
$this->set('_serialize', ['articles', 'pageCount']);
Using view templates
If you're not using auto-serialization, but custom view templates
Cookbook > Views > JSON and XML Views > Using a Data View with Template Files
you can make use of the PaginatorHelper in your template and add the data when converting to JSON, like
$pageCount = $this->Paginator->param('pageCount');
echo json_encode(compact('articles', 'pageCount'));

ng-options dont select items from resource

Iam trying to generate a multiple select-element with ng-options and want to preselect some items. I get the preselected items from a database as an array of objects. That is my HTML markup to build the selectable:
<select ng-model="currentAppointment.services" ng-options="value.id as value.name group by value.group for value in servicegroups"></select>
The currentAppointment.services variable hold the current selected objects. In servicegroups they are all selectable items, which i get from a REST service. The JSON for the servicegroups look like:
[
{
"id": 0,
"name": "Test1"
"price": 20.0
},
{
"id": 1,
"name": "Test2"
}
]
And the currentAppointment JSON:
{
"_id": "1001",
"title" : "test",
"services": [
{
"id": 0,
"name": "Test1"
"price": 20.0
}
]
}
The select-element is created correctly. But the service object with the id 0 is not preselected. I can image that the problem is, that angular compare an normal object against an angularjs Resource-object and so the objects are not equal. Instead the objects should be compared by the id. But how can i achieve that without coding a own directive?
If you want to pre-populate the dropdown list by assigning it with an object, then you need to remove value.id from the comprehension expression.
<select ng-model="currentAppointment.services" ng-options="value.name group by value.group for value in servicegroups"></select>
then do
$scope.currentAppointment.services = $scope.servicegroups[0];
Working Demo

Resources