Grails Creating JSON format output - json

I'm writing a Grails web app to take the input data below and reformat it into the output format below that then can be used to draw fancyTree view. I created a controller class with the code shown in the except that follows.
How can I iterate through to make the format like the output format shown below? I looked at how Groovy does each syntax but how can I use it to add ingredients?
code excerpt:
def list(){
def results = recipies.list()
def recipiesContents = [:] //Test List
recipiesContents=[]
for (record in results){
test.add([folder:true, title: results.Name, key: results.key
])
}
//render response types
withFormat {
html test
json {render test as JSON}
xml {render test as XML}
}
}
Input data:
{
recipies :[
{key: "1",
category:"Tuna Sandwich"
data:[{some data}],
ingredients: [
{item_name:"mayo",
brand: "My Own Brand"
},
{
item_name: "Tuna".
brand: "Bumble Bee"
}
]},
{key: "2",
category:"Chicken Noodle Soup"
data:[{some data}],
ingredients: [
{item_name:"condensed chicken soup",
brand: "Campbell"
},
{
item_name: "Noodles".
brand: "Top Ramen"
}
]}
]
}
output data:
[
{
title: "Tuna Sandwich"
ingredients: [
{title: "Tuna"},
{title: "mayo"}
]
},
{
title: "Chicken Noodle Soup",
ingredients: [
{title: "condensed chicken soup"},
{title: "Noodles"}
]
}
]

Have you tried:
for (record in results){
def ingredients = [:]
for (ingredient in record.ingredients) {
ingredients.add([title: ingredient.item_name)
}
test.add([title: results.category, ingredients: ingredients
])
}

Related

mongodb $lookup has_many association from embedded document

I have a boards collection, a lists collection, and a cards collection. An array of lists is embedded in a board document. I am trying to get an output that looks like this:
{
_id: 1,
title: "a board",
lists: [
{
_id: 1,
title: "a list",
cards: [ { _id: 1, title: "a card", list_id: 1 }, { _id: 2, title: "another card", list_id: 1 } ]
},
...
]
}
I want to nest the cards in the list it belongs to. The card document has a list_id field. I tried this:
db.boards.aggregate([
{ '$match' => { _id: 1 } },
{ '$lookup' => {
from: "cards",
localField: "lists._id",
foreignField: "list_id",
as: "cards"
} },
])
But that resulted in:
{
_id: 1,
title: "a board",
lists: [ { _id: 1, title: "a list" } ],
cards: [ { _id: 1, title: "a card", list_id: 1 }, { _id: 2, title: "another card", list_id: 1 } ]
}
I know that I have to use $unwind to get the result I want, but I can't get it to work
You need one additional aggregation pipeline step to "merge" these two lists and you can achieve it by running $map with embedded $filter. It simply works as a "join" operation on two arrays:
{
$project: {
_id: 1,
title: 1,
lists: {
$map: {
input: "$lists",
as: "list",
in: {
$mergeObjects: [
"$$list",
{
cards: {
$filter: {
input: "$cards",
cond: { $eq: [ "$$this.list_id", "$$list._id" ] }
}
}
}
]
}
}
}
}
}
Mongo Playground

Project data out of an immutable map to a different shape

I have a Map of immutable objects with a structure like:
id1: {
someField: 'anyvalue',
description: 'description1'
},
id2: {
someField: 'anotherValue`,
description: 'description2'
}
I want to project a List of the descriptions without resorting to toJS():
[ 'description1', 'description2' ]
How do I do this?
const data = new Immutable.Map({id1: {
someField: 'anyvalue',
description: 'description1'
},
id2: {
someField: 'anotherValue',
description: 'description2'
}});
const out = data.valueSeq().map(v => v.description).toList();
console.log(out)
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.js"></script>

Get Array Of Object On ajax Call success

I will make Ajax call on my Controller action method. I want result of JSON in this format.
// array of all brands
var brands = [
{ brandId: 1, name: "Ford" },
{ brandId: 2, name: "BMW" }
];
for this i will make another call
// array of all models
var models = [
{ modelId: 1, name: "Explorer", brandId: 1},
{ modelId: 2, name: "Focus", brandId: 1},
{ modelId: 3, name: "X3", brandId: 2},
{ modelId: 4, name: "X5", brandId: 2}
];
How can i do that please guide me.
You can use following code to solve your problem
public ActionResult SomeActionMethod(int id)
{
return Json(new {foo="bar", baz="Blech"});
}
Method from the jquery getJSON method by simply...
$.getJSON("../SomeActionMethod", { id: someId },
function(data) {
alert(data.foo);
alert(data.baz);
}
);
To serialize json in your controller, may be you can use http://www.newtonsoft.com/json/help/html/serializingjson.htm

How to use ng-filter Json [#attributes] using Angularjs?

I'm trying to filter sports feed by using sports id. but json feeds are #attribute based feed.
Json feeds
series: [
{
#attributes: {
id: "cdf590b4-0206-45b0-9122-20eae46a2b27",
name: "Pakistan tour of Sri Lanka, 2015",
year: "2015"
},
match: [
{},
{},
{},
{}
],
comment: []
},
{
#attributes: {
id: "324e971e-2044-4fa6-bdb2-0c47c99da64e",
name: "South Africa tour of Bangladesh, 2015",
year: "2015"
},
match: [
{},
{},
{},
{}
],
comment: []
},
My Controller
--So this my controller code.--
$http.get('http://example.com/cricket.php').then(function(response) {
$scope.sports = sports;
$scope.whichsports = $state.params.id;
}
This My View Filter Code
<ion-item ng-repeat="sport in sports | filter: { id : whichsports }">
So Please Help Me.!
Advance Thanks.
Probably $scope.sports = sports; should be instead $scope.sports = response;
And then in the ng-repeat you will get the sport attributes with sport['#attributes']

dynamic create json for treeview

I want to turn json
var treeNodes = [ {managerid:root,Employeeid:01},
{managerid:01,Employeeid:11},
{managerid:01,Employeeid:22},
{managerid:22,Employeeid:33},
{managerid:22,Employeeid:44}
];
into json like this using javascript.
json={
id:root,
children[{
id:01,
children[
{id:11},
{id:22}
]
children[
{id:33},
{id:44}
]
}
Can someone help with java script function?
First of all, your current JSON is incorrect:
var json = {
id: root,
children: [
{
id: 01,
children: [
{id: 11},
{id: 22}
]
},
{
children: [
{id: 33},
{id: 44}
]
}
]
};
Second, could you give more information about your table Employee?