Golang custom unmarshalling nested JSON - json

I have a JSON object that looks like this. It contains 3 meals a day for one week, for 21 total entries, since each meal is an individual entry.
{
"name": "MealPlan 1508620645147",
"items": [
{
"day": 1,
"mealPlanId": 0,
"slot": 1,
"position": 0,
"type": "RECIPE",
"value": "{\"id\":869953,\"imageType\":\"jpg\",\"title\":\"Cream Cheese & Fruit Breakfast Pastries\"}"
},
{
"day": 1,
"mealPlanId": 0,
"slot": 2,
"position": 0,
"type": "RECIPE",
"value": "{\"id\":537176,\"imageType\":\"jpg\",\"title\":\"Leftover Rice Casserole\"}"
},
{
"day": 1,
"mealPlanId": 0,
"slot": 3,
"position": 0,
"type": "RECIPE",
"value": "{\"id\":595927,\"imageType\":\"jpg\",\"title\":\"Spinach and Cheddar Quiche\"}"
},
{
"day": 2,
"mealPlanId": 0,
"slot": 1,
"position": 0,
"type": "RECIPE",
"value": "{\"id\":536716,\"imageType\":\"jpg\",\"title\":\"Candied Pecan Waffles\"}"
},
{
"day": 2,
"mealPlanId": 0,
"slot": 2,
"position": 0,
"type": "RECIPE",
"value": "{\"id\":893265,\"imageType\":\"jpg\",\"title\":\"Tahini Date Smoothie Bowls\"}"
},
{
"day": 2,
"mealPlanId": 0,
"slot": 3,
"position": 0,
"type": "RECIPE",
"value": "{\"id\":512880,\"imageType\":\"jpg\",\"title\":\"Grilled Caprese Salad Sandwich for #SundaySupper\"}"
},
{
"day": 3,
"mealPlanId": 0,
"slot": 1,
"position": 0,
"type": "RECIPE",
"value": "{\"id\":648647,\"imageType\":\"jpg\",\"title\":\"Jumbo Blueberry Muffins\"}"
},
{
"day": 3,
"mealPlanId": 0,
"slot": 2,
"position": 0,
"type": "RECIPE",
"value": "{\"id\":548554,\"imageType\":\"jpg\",\"title\":\"Brie, Pesto, and Sweet Pepper Grilled Cheese\"}"
},
{
"day": 3,
"mealPlanId": 0,
"slot": 3,
"position": 0,
"type": "RECIPE",
"value": "{\"id\":438024,\"imageType\":\"jpg\",\"title\":\"Mother's Manicotti\"}"
},
{
"day": 4,
"mealPlanId": 0,
"slot": 1,
"position": 0,
"type": "RECIPE",
"value": "{\"id\":681594,\"imageType\":\"jpg\",\"title\":\"Huevos Rancheros\"}"
},
{
"day": 4,
"mealPlanId": 0,
"slot": 2,
"position": 0,
"type": "RECIPE",
"value": "{\"id\":99184,\"imageType\":\"jpg\",\"title\":\"Black Bean Tacos\"}"
},
{
"day": 4,
"mealPlanId": 0,
"slot": 3,
"position": 0,
"type": "RECIPE",
"value": "{\"id\":604514,\"imageType\":\"jpg\",\"title\":\"Cheddar Scallion Dutch Baby\"}"
},
{
"day": 5,
"mealPlanId": 0,
"slot": 1,
"position": 0,
"type": "RECIPE",
"value": "{\"id\":622672,\"imageType\":\"jpg\",\"title\":\"Cinnamon-Sugar Streusel Baked French Toast\"}"
},
{
"day": 5,
"mealPlanId": 0,
"slot": 2,
"position": 0,
"type": "RECIPE",
"value": "{\"id\":636178,\"imageType\":\"jpg\",\"title\":\"Broccoli Cheddar Soup, A Panera Bread Co. Copycat\"}"
},
{
"day": 5,
"mealPlanId": 0,
"slot": 3,
"position": 0,
"type": "RECIPE",
"value": "{\"id\":452482,\"imageType\":\"jpg\",\"title\":\"Slow Cooker Macaroni and Cheese I\"}"
},
{
"day": 6,
"mealPlanId": 0,
"slot": 1,
"position": 0,
"type": "RECIPE",
"value": "{\"id\":157272,\"imageType\":\"jpg\",\"title\":\"Pomegranate-Nutella Waffles\"}"
},
{
"day": 6,
"mealPlanId": 0,
"slot": 2,
"position": 0,
"type": "RECIPE",
"value": "{\"id\":619111,\"imageType\":\"jpg\",\"title\":\"Barley, Bulgur and Vegetable Vegan Casserole\"}"
},
{
"day": 6,
"mealPlanId": 0,
"slot": 3,
"position": 0,
"type": "RECIPE",
"value": "{\"id\":510089,\"imageType\":\"jpg\",\"title\":\"Stovetop Mac and Cheese\"}"
},
{
"day": 7,
"mealPlanId": 0,
"slot": 1,
"position": 0,
"type": "RECIPE",
"value": "{\"id\":551869,\"imageType\":\"jpg\",\"title\":\"Berry Smoothie\"}"
},
{
"day": 7,
"mealPlanId": 0,
"slot": 2,
"position": 0,
"type": "RECIPE",
"value": "{\"id\":590452,\"imageType\":\"jpg\",\"title\":\"Cheesy Baked Pasta with Eggplant and Artichokes\"}"
},
{
"day": 7,
"mealPlanId": 0,
"slot": 3,
"position": 0,
"type": "RECIPE",
"value": "{\"id\":590452,\"imageType\":\"jpg\",\"title\":\"Cheesy Baked Pasta with Eggplant and Artichokes\"}"
}
]
}
I want to unmarshal it into an array of structs that will hold 3 meals each as Breakfast, Lunch, Dinner. So, I want my struct to look like this, where ID is the value.id field from the JSON, and Name is the value.title field from the JSON, Breakfast is the item with slot:1, Lunch is the item with slot:2 and Dinner is the item with slot:3.
type Day struct {
Breakfast meal
Lunch meal
Dinner meal
}
type meal struct {
ID int
Name string
}
How can I accomplish this in Go? My initial thought was to create an intermediate struct that holds all the data from the JSON, and then create another struct by using just the fields I need. How can I do this without using the intermediary struct ?

Did as mayo suggested and implemented UnmarshalJSON
func (wp *WeekPlan) UnmarshalJSON(b []byte) error {
wp.Days = make([]Day, 7)
var f map[string]*json.RawMessage
json.Unmarshal(b, &f)
var v []map[string]interface{}
json.Unmarshal(*f["items"], &v)
for _, item := range v {
day := int(item["day"].(float64)) - 1
mealnumber := int(item["slot"].(float64))
var value map[string]interface{}
json.Unmarshal([]byte(item["value"].(string)), &value)
fmt.Println(value)
id := int(value["id"].(float64))
name := value["title"].(string)
thisMeal := Meal{ID: id, Name: name}
var dateUpdate Day
dateUpdate = wp.Days[day]
switch mealnumber {
case 1:
dateUpdate.Breakfast = thisMeal
case 2:
dateUpdate.Lunch = thisMeal
default:
dateUpdate.Dinner = thisMeal
}
wp.Days[day] = dateUpdate
}
return nil
}
type WeekPlan struct {
Days []Day
}
type Day struct {
Breakfast Meal
Lunch Meal
Dinner Meal
}
type Meal struct {
ID int
Name string
CookTime int
Image string
}

Related

json jq request format

I need help to extract data from this JSON file using jq.
The app flv then verify the streamname mystrame is active and extract meta.video.height
I did try lot of queries without success and my jq knowledge is poor.
{
"port": 1935,
"server_index": 0,
"applications": [{
"name": "hls",
"live": {
"streams": [{
"name": "donbosco",
"time": 2380739,
"bw_in": 2112440,
"bytes_in": 541618713,
"bw_out": 0,
"bytes_out": 0,
"bw_audio": 35544,
"bw_video": 2076888,
"clients": [{
"id": 453,
"address": "127.0.0.1",
"time": 2380959,
"flashver": "FMLE/3.0 (compatible; Lavf57.83.100)",
"dropped": 0,
"avsync": 28,
"timestamp": 2382635,
"publishing": true,
"active": true
}],
"records": [],
"meta": {
"video": {
"width": 1168,
"height": 720,
"frame_rate": 25,
"codec": "H264",
"profile": "High",
"level": 3.1
},
"audio": {
"codec": "AAC",
"profile": "LC",
"channels": 2,
"sample_rate": 16000
}
},
"nclients": 1,
"publishing": true,
"active": true
}],
"nclients": 1
},
"recorders": {
"count": 0,
"lists": []
}
},
{
"name": "flv",
"live": {
"streams": [{
"name": "mystream",
"time": 2382811,
"bw_in": 2059096,
"bytes_in": 541841549,
"bw_out": 2059096,
"bytes_out": 543351459,
"bw_audio": 35472,
"bw_video": 2023624,
"clients": [{
"id": 452,
"address": "127.0.0.1",
"time": 2382727,
"flashver": "LNX 9,0,124,2",
"dropped": 0,
"avsync": -12,
"timestamp": 2384520,
"publishing": false,
"active": true
},
{
"id": 451,
"address": "127.0.0.1",
"time": 2383031,
"flashver": "FMLE/3.0 (compatible; Lavf58.74.100)",
"dropped": 0,
"avsync": -12,
"timestamp": 2384520,
"publishing": true,
"active": true
}
],
"records": [],
"meta": {
"video": {
"width": 1168,
"height": 720,
"frame_rate": 25,
"codec": "H264",
"profile": "High",
"level": 3.1
},
"audio": {
"codec": "AAC",
"profile": "LC",
"channels": 2,
"sample_rate": 16000
}
},
"nclients": 2,
"publishing": true,
"active": true
}],
"nclients": 2
},
"recorders": {
"count": 0,
"lists": []
}
}
]
}
Are you asking for help with the command-line JSON processor jq or the JavaScript library jQuery? They are not the same. For jq, try
jq '
.applications
| map(select(.name == "flv"))[].live.streams
| map(select(.name == "mystream" and .active))[].meta.video.height
'
720
Demo

How produces statistics with laravel?

I work on a laravel application that is a bit like a quiz. You create an exercise, you add questions and each question to answers.
Until then everything is fine.
My interest is to present statistics for each exercise after schoolchildren have answered the tests.
I want to be able to display the answer percentage for each question.
Here is an example of the data structure I have:
[
{
"id": 1,
"exercice_id": 1,
"question": "Lorem ipsum ?",
"responses": [
{
"id": 1,
"exercice_id": 1,
"question_id": 1,
"response_text": "Yes",
},
{
"id": 2,
"exercice_id": 1,
"question_id": 1,
"response_text": "No",
}
],
"choice": [
{
"id": 1,
"exercice_id": 1,
"question_id": 1,
"response_id": 1,
},
{
"id": 2,
"exercice_id": 1,
"question_id": 1,
"response_id": 1,
},
{
"id": 3,
"exercice_id": 1,
"question_id": 1,
"response_id": 2,
}
]
},
{
"id": 2,
"exercice_id": 1,
"question": "fake text ?",
"responses": [
{
"id": 3,
"exercice_id": 1,
"question_id": 2,
"response_text": "A",
},
{
"id": 4,
"exercice_id": 1,
"question_id": 2,
"response_text": "B",
},
{
"id": 5,
"exercice_id": 1,
"question_id": 2,
"response_text": "C",
}
],
"choice": [
{
"id": 4,
"exercice_id": 1,
"question_id": 2,
"response_id": 5,
},
{
"id": 5,
"exercice_id": 1,
"question_id": 2,
"response_id": 3,
}
]
}
]
I tried the groupBy method on several elements. but I have not found the formula yet.
return response()->json(Question::with('responses','choice')
->where('exercice_id',$exo->id)
//->groupBy('choice')
->get(),
200,[], JSON_NUMERIC_CHECK);

ORDER condition not working in cakephp 3 TREE behavior

I am using cakephp-3 tree behavior for Categories table. Here 'sequence_no' field are using to sorting sub-category list for a category. I am searching with a category_id to get all of his child category in ASCENDING order by 'sequence_no'. But order not working here. My code snippet and output here.
$categoris = $this->Categories->find('children', ['for' => $id])
->find('threaded')
->contain('ParentCategories')
->order(['Categories.sequence_no' => 'ASC'])
->toArray();
- OUTPUT SAMPLE:
{
"status": "OK",
"result": {
"data": [
{
"id": 15,
"store_id": 0,
"uuid": null,
"name": "cat-3",
"parent_id": 3,
"lft": 16,
"rght": 17,
"sequence_no": 2,
"url": "cat-3",
"layout_id": 0,
"status": 1,
"total": 0,
"created": "2018-06-12T07:36:15+00:00",
"modified": "2018-06-12T08:15:12+00:00",
"parent_category": {
"id": 3,
"store_id": 2,
"uuid": null,
"name": "Pants",
"parent_id": null,
"lft": 15,
"rght": 20,
"sequence_no": null,
"url": "pants",
"layout_id": 0,
"status": 1,
"total": 0,
"created": "2018-06-06T10:23:50+00:00",
"modified": "2018-06-06T10:23:50+00:00"
},
"children": []
},
{
"id": 16,
"store_id": 0,
"uuid": null,
"name": "cat-4",
"parent_id": 3,
"lft": 18,
"rght": 19,
"sequence_no": 1,
"url": "cat-4",
"layout_id": 0,
"status": 1,
"total": 0,
"created": "2018-06-12T07:36:34+00:00",
"modified": "2018-06-12T08:15:12+00:00",
"parent_category": {
"id": 3,
"store_id": 2,
"uuid": null,
"name": "Pants",
"parent_id": null,
"lft": 15,
"rght": 20,
"sequence_no": null,
"url": "pants",
"layout_id": 0,
"status": 1,
"total": 0,
"created": "2018-06-06T10:23:50+00:00",
"modified": "2018-06-06T10:23:50+00:00"
},
"children": []
}
]
}
}
Since findChildred finder add a ORDER lft asc clause to your query your order conditions will be appended to that
If you want to force your order you can do
->order(['Categories.sequence_no' => 'ASC'], true)
the second parameter in the order() method tells cake to overwrite the ORDER BY set before
see the manual, about the end of this paragraph

How do I render json in my react component?

I have the following json file I am importing into my component:
import { beer, wine, spririts, alcopop } from '../../config/calculator.json';
How can I use the json in my render?
Depending upon what was chosen I would like to use the data for each element, if user clicks beer, then show data for beer. For example, how would I loop through 'sizes' in 'beer'? This is my code so far:
{[drinkType].sizes.map((option, i) =>
<div value={option.id} key={i}>
{option}
</div>
)}
I would like to display the size name, pint, bottle, can etc
I get the error: Cannot read property 'map' of undefined
//calculator.json
{
"beer": {
"name": "Beer or cider",
"sizes": {
"568": {
"name": "Pint",
"size": 0.568,
"id": "pint",
"max": 10,
"icon": "beer_pint"
},
"440": {
"name": "Can",
"size": 0.44,
"id": "can",
"max": 10,
"icon": "beer_can"
},
"330": {
"name": "Bottle",
"size": "0.33",
"id": "bottle",
"max": "10",
"icon": "beer_bottle_330"
},
"275": {
"name": "Small bottle",
"size": 0.275,
"id": "smallBottle",
"max": 10,
"icon": "beer_bottle_275"
}
},
"strength": [4, 4.5, 5, 6, 7, 8, 9]
},
"wine": {
"name": "Wine or champagne",
"sizes": {
"125": {
"name": "Small glass",
"size": 0.125,
"id": "small",
"max": 10,
"icon": "wine_small_glass"
},
"175": {
"name": "Standard glass",
"size": 0.175,
"id": "std",
"max": 10,
"icon": "wine_standard_glass"
},
"250": {
"name": "Large glass",
"size": 0.25,
"id": "large",
"max": 10,
"icon": "wine_large_glass"
},
"1000": {
"name": "Bottle",
"size": 1,
"id": "bottle",
"max": 10,
"icon": "wine_bottle"
}
},
"strength": [9, 10, 10.5, 11, 11.5, 12, 13, 14, 15, 16, 17]
},
"spirits": {
"name": "Spirits or shots",
"sizes": {
"25": {
"name": "Single",
"size": 0.025,
"id": "single",
"max": 10,
"icon": "spirit_single"
},
"35": {
"name": "Large single",
"size": 0.035,
"id": "lgSingle",
"max": 10,
"icon": "spirit_large_single"
},
"50": {
"name": "Double",
"size": 0.05,
"id": "double",
"max": 10,
"icon": "spirit_double"
},
"70": {
"name": "Large double",
"size": 0.07,
"id": "lgDouble",
"max": 10,
"icon": "spirit_large_double"
},
"700": {
"name": "Bottle",
"size": 0.7,
"id": "bottle700",
"max": 3,
"icon": "spirit_bottles"
},
"1000": {
"name": "Bottle",
"size": 1,
"id": "bottle",
"max": 3,
"icon": "spirit_bottles"
}
},
"strength": [37, 40]
},
"alcopop": {
"name": "Alcopop",
"sizes": {
"275": {
"name": "Small bottle",
"size": 0.275,
"id": "small",
"max": 10,
"icon": "alcopops_small_bottle"
},
"330": {
"name": "Standard bottle",
"size": 0.33,
"id": "std",
"max": 10,
"icon": "alcopops_standard_bottle"
},
"750": {
"name": "Large bottle",
"size": 0.75,
"id": "large",
"max": 10,
"icon": "alcopops_large_bottle"
}
},
"strength": [5, 5.5]
}
}
.map does not work on objects like that,
map() is only for Arrays
Instead you can store the keys in array and use them, like,
import jsonData from '../../config/calculator';
...
//in constructor or any function
let sizes;
sizes={};
for(let i of Object.keys(jsonData)){
if(!sizes[i][0])sizes[i] = [];
for(let j of Object.keys(i.sizes)){
sizes[i].push(jsonData[i].sizes[j])
}
}
this.setState({
sizesArray:sizes
})
Now you can use this.state.sizesArray.map to iterate over sizes, like,
{this.state.sizes[drinkType].map((option, i) =>
<div value={option.id} key={i}>
{option}
</div>
)}
First, import your json inside the component:
import jsonData from '../../config/calculator'; //specify the appropriate location of the json file
Then you can access the "data" in your component in various ways. One way could be:
constructor() {
this.state = {
data: []
}
}
componentDidMount(){
this.setState({data: jsonData});
}
And accordingly, you can use this data(this.state.data).

Recursive call for JSON in swift 3

I have a response that looks like this
{
"id": 3,
"parent_id": 1,
"name": "QuickStart Intelligence",
"is_active": true,
"position": 2,
"level": 1,
"product_count": 590,
"children_data": [
{
"id": 53,
"parent_id": 3,
"name": "Find Training",
"is_active": true,
"position": 1,
"level": 2,
"product_count": 9,
"children_data": [
{
"id": 58,
"parent_id": 53,
"name": "Career Paths",
"is_active": true,
"position": 1,
"level": 3,
"product_count": 0,
"children_data": [
{
"id": 123,
"parent_id": 58,
"name": "Business Productivity",
"is_active": true,
"position": 1,
"level": 4,
"product_count": 245,
"children_data": []
},
{
"id": 70,
"parent_id": 58,
"name": "Creative & Design",
"is_active": true,
"position": 3,
"level": 4,
"product_count": 43,
"children_data": []
},
]
},
{
"id": 57,
"parent_id": 53,
"name": "Technology",
"is_active": true,
"position": 2,
"level": 3,
"product_count": 1,
"children_data": [
{
"id": 162,
"parent_id": 57,
"name": "Microsoft",
"is_active": true,
"position": 6,
"level": 4,
"product_count": 183,
"children_data": []
},
{
"id": 164,
"parent_id": 57,
"name": "Adobe",
"is_active": true,
"position": 8,
"level": 4,
"product_count": 34,
"children_data": []
}
]
}
{
"id": 100,
"parent_id": 53,
"name": "Certifications",
"is_active": true,
"position": 3,
"level": 3,
"product_count": 0,
"children_data": [
{
"id": 112,
"parent_id": 100,
"name": "Microsoft SQL Server 2012",
"is_active": true,
"position": 1,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 113,
"parent_id": 100,
"name": "Windows Server 2012 Microsoft Certifications",
"is_active": true,
"position": 2,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 114,
"parent_id": 100,
"name": "Visual Studio Microsoft Certifications",
"is_active": true,
"position": 3,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 115,
"parent_id": 100,
"name": "SharePoint 2013 Microsoft Certifications",
"is_active": true,
"position": 4,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 116,
"parent_id": 100,
"name": "Private Cloud Microsoft Certifications",
"is_active": true,
"position": 5,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 117,
"parent_id": 100,
"name": "Exchange Server 2013 Certifications",
"is_active": true,
"position": 6,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 118,
"parent_id": 100,
"name": "Lync Server 2013 Microsoft Certifications",
"is_active": true,
"position": 7,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 119,
"parent_id": 100,
"name": "Windows 8 Microsoft Certifications",
"is_active": true,
"position": 8,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 120,
"parent_id": 100,
"name": "Microsoft System Center Certifications",
"is_active": true,
"position": 9,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 121,
"parent_id": 100,
"name": "Microsoft Windows Server 2008 Certifications",
"is_active": true,
"position": 10,
"level": 4,
"product_count": 0,
"children_data": []
}
]
}
It goes on like that. I have initialized some variables in my class Categories like this
var categoryId = Int()
var parentId = Int()
var categoryName = String()
var isCategoryActive = Bool()
var categoryPostion = Int()
var categoryLevel = Int()
var categoryChildCount = Int()
var childArray = [Categories]()
I know that it should be done with a recursive call but somehow i am unable to wrap my head around the solution. I've tried looking on overflow but couldn't find anything relevant. this is how i am casting the json
class func getUserFromJson(_ jsonString: String) -> Categories {
let category = Categories()
do {
let dic = try JSONSerialization.jsonObject(with: jsonString.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!, options: .allowFragments) as! NSDictionary
if let id = dic.object(forKey: "id") as? Int {
category.categoryId = id
}
if let parentId = dic.object(forKey: "parent_id") as? Int {
category.parentId = parentId
}
if let name = dic.object(forKey: "name") as? String {
category.categoryName = name
}
if let isActive = dic.object(forKey: "is_active") as? Bool {
category.isCategoryActive = isActive
}
if let position = dic.object(forKey: "position") as? Int {
category.categoryPostion = position
}
if let level = dic.object(forKey: "level") as? Int {
category.categoryLevel = level
}
if let productCount = dic.object(forKey: "product_count") as? Int {
category.categoryChildCount = productCount
}
}
catch {
}
return category
}
How would i parse the children_data which is an array and has many children and again those children have children as you can see and some of them are empty so i need to check for that as well.
go like this.
let childrenData = dic["children_data"] as? NSArray
{
for data in childrenData
{
let child_dic = data as! NSDictionary
//Here Your Code.
}
}
You can use same for child of child.