Related
Im trying to add a new set of values into my pathlist for example 3:{ "x" : 30, "y": 30 , "r":10, "color":1} to my pathlist and i am stuck here at pathlist.addentries( what to type here) . Thanks in advance for any help
Map<int, Map<String, double>> pathList = {
1: {"x": 10, "y": 10, "r": 10, "color": 0},
2: {"x": 20, "y": 20, "r": 10, "color": 1},
};
You can simply add new values like
pathList[3] = {"x": 11, "y": 10, "r": 11, "color": 11};
or as any index pathList[i] = {}
You can add one value like this
pathList[3]={ "x" : 30, "y": 30 , "r":10, "color":1};
If you want to add multiple values you can use this
pathList.addAll({
4: {"x": 10, "y": 10, "r": 10, "color": 0},
5: {"x": 20, "y": 20, "r": 10, "color": 1},
});
I'm not entirely sure of the correct term to used here, wherther its Json Unpacking or Json explode, or simply JSON flattening. However, I'm trying to read columns for a Nested JSON array in Databricks.
For example, I would like to pull at the customerId number 'cust5001, which looks like the following key/value pair
array
0: {"customerId": "cust5001", "orderDate": "2021-12-24 00.00.00.000", "orderDetails": [{"productId": "prd9001", "quantity": 2, "sequence": 1, "totalPrice": {"gross": 550, "net": 500, "tax": 50}}, {"productId": "prd9002", "quantity": 3, "sequence": 2, "totalPrice": {"gross": 300, "net": 240, "tax": 60}}], "orderId": "ord1001", "shipmentDetails": {"city": "Delhi", "country": "India", "postalCode": "110040", "state": "New Delhi", "street": "M.G.Road"}}
1: {"customerId": "cust5002", "orderDate": "2021-12-25 00.00.00.000", "orderDetails": [{"productId": "prd9001", "quantity": 1, "sequence": 1, "totalPrice": {"gross": 275, "net": 250, "tax": 25}}, {"productId": "prd9004", "quantity": 4, "sequence": 2, "totalPrice": {"gross": 1000, "net": 900, "tax": 100}}], "orderId": "ord1002", "shipmentDetails": {"city": "Mumbai", "country": "India", "postalCode": "400064", "state": "Maharastra", "street": "Malad"}}
If you take a look at the complete image below, I believe I need to explode the column 'datasets'l and then query "customerId":"cust5001"
To be totally straight, I would like to flatten the whole two rows, but if someone could show me how pull out "customerId":"cust5001", it would be start.
I am using this Stacked Bar Chart Example at the link: https://vega.github.io/vega/examples/stacked-bar-chart/
But I can't figure out how to show the field title on axis x and y.
I want to show the title x and y below.
Is there a way to display the label title on the x and y axes?
Just add a title attribute.
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "A basic stacked bar chart example.",
"width": 500,
"height": 200,
"padding": 5,
"data": [
{
"name": "table",
"values": [
{"x": 0, "y": 28, "c": 0}, {"x": 0, "y": 55, "c": 1},
{"x": 1, "y": 43, "c": 0}, {"x": 1, "y": 91, "c": 1},
{"x": 2, "y": 81, "c": 0}, {"x": 2, "y": 53, "c": 1},
{"x": 3, "y": 19, "c": 0}, {"x": 3, "y": 87, "c": 1},
{"x": 4, "y": 52, "c": 0}, {"x": 4, "y": 48, "c": 1},
{"x": 5, "y": 24, "c": 0}, {"x": 5, "y": 49, "c": 1},
{"x": 6, "y": 87, "c": 0}, {"x": 6, "y": 66, "c": 1},
{"x": 7, "y": 17, "c": 0}, {"x": 7, "y": 27, "c": 1},
{"x": 8, "y": 68, "c": 0}, {"x": 8, "y": 16, "c": 1},
{"x": 9, "y": 49, "c": 0}, {"x": 9, "y": 15, "c": 1}
],
"transform": [
{
"type": "stack",
"groupby": ["x"],
"sort": {"field": "c"},
"field": "y"
}
]
}
],
"scales": [
{
"name": "x",
"type": "band",
"range": "width",
"domain": {"data": "table", "field": "x"}
},
{
"name": "y",
"type": "linear",
"range": "height",
"nice": true, "zero": true,
"domain": {"data": "table", "field": "y1"}
},
{
"name": "color",
"type": "ordinal",
"range": "category",
"domain": {"data": "table", "field": "c"}
}
],
"axes": [
{"orient": "bottom", "scale": "x", "zindex": 1, "title":"x"},
{"orient": "left", "scale": "y", "zindex": 1, "title":"y"}
],
"marks": [
{
"type": "rect",
"from": {"data": "table"},
"encode": {
"enter": {
"x": {"scale": "x", "field": "x"},
"width": {"scale": "x", "band": 1, "offset": -1},
"y": {"scale": "y", "field": "y0"},
"y2": {"scale": "y", "field": "y1"},
"fill": {"scale": "color", "field": "c"}
},
"update": {
"fillOpacity": {"value": 1}
},
"hover": {
"fillOpacity": {"value": 0.5}
}
}
}
]
}
My colleague is response for backend development, this is one of example of the API response.
I want to know that how can I separate it to the map?
And There have same name, I am very difficult to handle it. JSON to Dart generated code also will have error case by same name.
Is there have any other solution to handle this Json?
{
"formId": 1,
"formTypeId": 2,
"formTemplateId": 1,
"version": 1,
"history": "N",
"refNum": "AGQ/T01/01/A1/QA2/0001",
"dateOfAcceptance": "2020/12/16 14:54:01",
"dateOfReacceptance": "2020/12/16 14:54:01",
"blockId": 242,
"floorId": 1,
"flatId": 98,
"isPublic": "N",
"locationId": 1,
"fromFloorId": 1,
"toFloorId": 2,
"elevationId": 1,
"isNextVendor": "Y",
"isMultipleVendor": "N",
"formStatusId": 1,
"checkResults": 1,
"submissionDate": "2020/12/16 14:54:01",
"submittedBy": "ADMINISTRATOR",
"siteId": "YLTL",
"formDetails": [
{
"formDetailsId": 1,
"formId": 1,
"title": "A. 驗收項目",
"checkType": 1,
"required": "Y",
"parentId": 0,
"orderNum": 1,
"formDetailsOptions": [],
"fromDetails": [
{
"formDetailsId": 3,
"formId": 1,
"title": "Title group",
"checkType": 2,
"required": "Y",
"parentId": 1,
"orderNum": 1,
"formDetailsOptions": [],
"fromDetails": [
{
"formDetailsId": 4,
"formId": 1,
"title": "門編號:",
"checkType": 5,
"required": "Y",
"parentId": 3,
"orderNum": 1,
"answerVal": "A001",
"formDetailsOptions": []
},
{
"formDetailsId": 5,
"formId": 1,
"title": "位置:",
"checkType": 6,
"required": "Y",
"parentId": 3,
"orderNum": 2,
"formDetailsOptions": [
{
"formDetailsOptionsId": 1,
"formDetailsId": 5,
"orderNum": 1,
"optionDesc": "單位大門",
"defectOpts": "N",
"additionOpts": "N",
"selected": "N"
},
{
"formDetailsOptionsId": 2,
"formDetailsId": 5,
"orderNum": 2,
"optionDesc": "廚房門",
"defectOpts": "N",
"additionOpts": "N",
"selected": "N"
},
{
"formDetailsOptionsId": 3,
"formDetailsId": 5,
"orderNum": 3,
"optionDesc": "廁所客廳",
"defectOpts": "N",
"additionOpts": "N",
"selected": "N"
}
]
},
{
"formDetailsId": 6,
"formId": 1,
"title": "假框抆塞:",
"checkType": 4,
"required": "Y",
"parentId": 3,
"orderNum": 3,
"answerVal": "Y",
"formDetailsOptions": []
},
{
"formDetailsId": 7,
"formId": 1,
"title": "門框尺寸:",
"checkType": 4,
"required": "Y",
"parentId": 3,
"orderNum": 4,
"answerVal": "Y",
"formDetailsOptions": []
},
{
"formDetailsId": 8,
"formId": 1,
"title": "門框木料:",
"checkType": 4,
"required": "Y",
"parentId": 3,
"orderNum": 5,
"answerVal": "Y",
"formDetailsOptions": []
},
{
"formDetailsId": 9,
"formId": 1,
"title": "門框接口:",
"checkType": 4,
"required": "Y",
"parentId": 3,
"orderNum": 6,
"answerVal": "Y",
"formDetailsOptions": []
},
{
"formDetailsId": 10,
"formId": 1,
"title": "門框色澤:",
"checkType": 4,
"required": "Y",
"parentId": 3,
"orderNum": 7,
"answerVal": "Y",
"formDetailsOptions": []
},
{
"formDetailsId": 11,
"formId": 1,
"title": "磨耳及蟻油:",
"checkType": 4,
"required": "Y",
"parentId": 3,
"orderNum": 8,
"answerVal": "Y",
"formDetailsOptions": []
},
{
"formDetailsId": 12,
"formId": 1,
"title": "防火膠條:",
"checkType": 4,
"required": "Y",
"parentId": 3,
"orderNum": 9,
"answerVal": "Y",
"formDetailsOptions": []
},
{
"formDetailsId": 13,
"formId": 1,
"title": "平正及穩固:",
"checkType": 4,
"required": "Y",
"parentId": 3,
"orderNum": 10,
"answerVal": "Y",
"formDetailsOptions": []
},
{
"formDetailsId": 14,
"formId": 1,
"title": "備註:",
"checkType": 7,
"required": "N",
"parentId": 3,
"orderNum": 11,
"answerVal": "Test group remark",
"formDetailsOptions": []
}
]
}
]
},
{
"formDetailsId": 2,
"formId": 1,
"title": "B. 要改善位置及詳情備註:",
"checkType": 1,
"required": "Y",
"parentId": 0,
"orderNum": 2,
"answerVal": "Test B remark",
"formDetailsOptions": []
}
],
"formDefects": [
{
"formDefectsId": 1,
"formId": 1,
"defectRemark": "Test Remark",
"followActionId": 1,
"reporterType": 1,
"formDefectsImgs": [
{
"formDefectsImgId": 1,
"formDefectsId": 1,
"imgType": 1,
"filePath": "data/images/1/DEFECT-20201228150623123-2F3F4H.jpg",
"orderNum": 1
}
]
}
],
"formVendors": [
{
"formVendorId": 2,
"formId": 1,
"vendorId": "3954",
"orderNum": 2,
"isNextVendor": "N",
"formVendorImgs": [
{
"formVendorImgId": 1,
"formVendorId": 2,
"filePath": "data/images/1/QUERY-20201228151423123-2FF2V4.png",
"orderNum": 1
}
],
"vendor": {
"vendorId": "3954",
"vendorDesc": "博西華",
"vendorDescDisp": "BSH"
}
}
],
"formNextVendors": [
{
"formVendorId": 1,
"formId": 1,
"vendorId": "3985",
"orderNum": 1,
"isNextVendor": "Y",
"formVendorImgs": [
{
"formVendorImgId": 2,
"formVendorId": 1,
"filePath": "data/images/1/QUERY-20201228151423123-VD5F5H.png",
"orderNum": 2
}
],
"vendor": {
"vendorId": "3985",
"vendorDesc": "瑞基",
"vendorDescDisp": "Zuki"
}
}
]
}
With this kind of complex json i would suggest using dart packages for serialization. For example:
https://pub.dev/packages/json_serializable
or
https://pub.dev/packages/built_value
If you can share Dart models, then use json_serializable package. That would make things easier. But...
...if you have to live with the JSON response that your colleague developed and you cannot share your models that convert from/to Json, one alternative would be to use https://app.quicktype.io/
I entered your JSON response and out came generated Dart code that seems legit, at a first glance at least.
Example from that code generation:
factory ComplexJson.fromJson(Map<String, dynamic> json) => ComplexJson(
formId: json["formId"],
formTypeId: json["formTypeId"],
formTemplateId: json["formTemplateId"],
version: json["version"],
history: historyValues.map[json["history"]],
refNum: json["refNum"],
dateOfAcceptance: json["dateOfAcceptance"],
dateOfReacceptance: json["dateOfReacceptance"],
blockId: json["blockId"],
floorId: json["floorId"],
flatId: json["flatId"],
isPublic: historyValues.map[json["isPublic"]],
locationId: json["locationId"],
fromFloorId: json["fromFloorId"],
toFloorId: json["toFloorId"],
elevationId: json["elevationId"],
isNextVendor: historyValues.map[json["isNextVendor"]],
isMultipleVendor: historyValues.map[json["isMultipleVendor"]],
formStatusId: json["formStatusId"],
checkResults: json["checkResults"],
submissionDate: json["submissionDate"],
submittedBy: json["submittedBy"],
siteId: json["siteId"],
formDetails: List<MDetail>.from(json["formDetails"].map((x) => MDetail.fromJson(x))),
formDefects: List<FormDefect>.from(json["formDefects"].map((x) => FormDefect.fromJson(x))),
formVendors: List<FormVendor>.from(json["formVendors"].map((x) => FormVendor.fromJson(x))),
formNextVendors: List<FormVendor>.from(json["formNextVendors"].map((x) => FormVendor.fromJson(x))),
);
A PostgreSQL 10 table contains JSON data like (here an SQL Fiddle):
[
{
"col": 7,
"row": 12,
"value": 3,
"letter": "A"
},
{
"col": 8,
"row": 12,
"value": 10,
"letter": "B"
},
{
"col": 9,
"row": 12,
"value": 1,
"letter": "C"
},
{
"col": 10,
"row": 12,
"value": 2,
"letter": "D"
}
]
How to extract only the "letter" values and concatenate them to a string like
ABCD
I suppose at the end I should use the ARRAY_TO_STRING function, but which JSON function to use for extracting the "letter" values to an array?
UPDATE:
Got a tip at the very helpful PostgreSQL mailing list too:
SELECT string_agg(x->>'letter','') FROM json_array_elements(
'[{"col": 7, "row": 12, "value": 3, "letter": "A"}, {"col": 8, "row": 12, "value": 10, "letter": "B"}, {"col": 9, "row": 12, "value": 1, "letter": "C"}, {"col": 10, "row": 12, "value": 2, "letter": "D"}]'::json
) x;
Use jsonb_array_elements() and string_agg():
with my_table(json_data) as (
values(
'[
{
"col": 7,
"row": 12,
"value": 3,
"letter": "A"
},
{
"col": 8,
"row": 12,
"value": 10,
"letter": "B"
},
{
"col": 9,
"row": 12,
"value": 1,
"letter": "C"
},
{
"col": 10,
"row": 12,
"value": 2,
"letter": "D"
}
]'::jsonb)
)
select string_agg(value->>'letter', '')
from my_table
cross join lateral jsonb_array_elements(json_data)
string_agg
------------
ABCD
(1 row)