extract data from object array from json field in mysql 8 - mysql

I would like through a sql query to extract data from a json field in mysql8
TABLE :
id | int
game_at | datetime
scores | json
Registrations in mysql :
id : 1,
game_at : 2022-11-04 15:53:00
scores :
[
{
"tr": "4",
"tt": "10",
"type": "alpha",
"week": 44,
"id": "67"
},
{
"tr": "7",
"tt": "10",
"type": "alpha",
"week": 44,
"id": "71"
},
{
"tr": "7",
"tt": "10",
"type": "beta",
"week": 44,
"id": "67"
},
{
"tr": "8",
"tt": "10",
"type": "beta",
"week": 44,
"id": "71"
}
]
id : 2,
game_at : 2022-11-05 16:53:00
scores :
[
{
"tr": "2",
"tt": "10",
"type": "alpha",
"week": 45,
"id": "67"
},
{
"tr": "6",
"tt": "10",
"type": "alpha",
"week": 45,
"id": "71"
},
{
"tr": "3",
"tt": "10",
"type": "beta",
"week": 45,
"id": "67"
},
{
"tr": "7",
"tt": "10",
"type": "beta",
"week": 45,
"id": "71"
}
]
id : 3,
game_at : 2022-11-04 17:53:00
scores :
[
{
"tr": "10",
"tt": "10",
"type": "alpha",
"week": 45,
"id": "67"
},
{
"tr": "6",
"tt": "10",
"type": "alpha",
"week": 45,
"id": "71"
},
{
"tr": "9",
"tt": "10",
"type": "beta",
"week": 45,
"id": "67"
},
{
"tr": "1",
"tt": "10",
"type": "beta",
"week": 45,
"id": "71"
}
]
I tried this to start :
$sql ='SELECT scores->"$.type"
FROM adresse
WHERE scores->"$.poliste" = "67"
';
but it returns null?
For example, I would like to retrieve all the values ​​where:
id: 67
type: alpha
{
"tr": "4",
"tt": "10",
"type": "alpha",
"week": 44,
"id": "67"
},
{
"tr": "2",
"tt": "10",
"type": "alpha",
"week": 45,
"id": "67"
},
{
"tr": "10",
"tt": "10",
"type": "alpha",
"week": 45,
"id": "67"
},
to create a table
I read that there is JSON_TABLE or substring_index but I don't understand how it works. An example would allow me to move forward Thank you in advance

Related

Flutter dynamic data from api to make Widgets

In my case the json will be different every time and i have to make widget like "inputtype": "dropdown", "inputtype": "radiobutton" but if i craete model it don't go as i wants it to.
{
"fields": [
{
"id": 31,
"name": "make",
"isrequired": "required",
"valuetype": "text",
"priority": 1,
"inputtype": "dropdown",
"max_min": [],
"rangeable": "false",
"choices": [
{
"id": 46,
"name": "Samsung",
"categoryextrafield_id": 31,
"created_at": "2021-12-29T01:30:47.000000Z",
"updated_at": "2021-12-29T01:30:47.000000Z",
"priority": 10
},
{
"id": 47,
"name": "Dell",
"categoryextrafield_id": 31,
"created_at": "2021-12-29T01:30:52.000000Z",
"updated_at": "2021-12-29T01:30:52.000000Z",
"priority": 20
},
{
"id": 48,
"name": "IBM",
"categoryextrafield_id": 31,
"created_at": "2021-12-29T01:31:09.000000Z",
"updated_at": "2021-12-29T01:31:09.000000Z",
"priority": 30
},
{
"id": 49,
"name": "Acer",
"categoryextrafield_id": 31,
"created_at": "2021-12-29T01:31:24.000000Z",
"updated_at": "2021-12-29T01:31:24.000000Z",
"priority": 40
}
],
"available": []
},
{
"id": 32,
"name": "model",
"isrequired": "required",
"valuetype": "text",
"priority": 2,
"inputtype": "textfield",
"max_min": [],
"rangeable": "false",
"choices": [],
"available": [
{
"model": "a51"
},
{
"model": "y9s"
},
{
"model": "a31"
},
{
"model": "yS10"
},
{
"model": "Y10S"
},
{
"model": "A551"
},
{
"model": "node8"
},
{
"model": "s9"
},
{
"model": null
},
{
"model": "2021"
},
{
"model": "2020"
},
{
"model": "2010"
},
{
"model": "Civic"
},
{
"model": "2019"
},
{
"model": "Daewooy9"
},
{
"model": "corei5"
},
{
"model": "corei9"
},
{
"model": "corei3"
},
{
"model": "corei11"
}
]
},
{
"id": 29,
"name": "features",
"isrequired": "required",
"valuetype": "text",
"priority": 3,
"inputtype": "checkbox",
"max_min": [],
"rangeable": "false",
"choices": [
{
"id": 41,
"name": "Bluetooth",
"categoryextrafield_id": 29,
"created_at": "2021-12-29T01:19:00.000000Z",
"updated_at": "2021-12-29T01:19:00.000000Z",
"priority": 1
},
{
"id": 42,
"name": "Fingerprint",
"categoryextrafield_id": 29,
"created_at": "2021-12-29T01:19:10.000000Z",
"updated_at": "2021-12-29T01:19:10.000000Z",
"priority": 10
},
{
"id": 43,
"name": "LedDisplay",
"categoryextrafield_id": 29,
"created_at": "2021-12-29T01:19:35.000000Z",
"updated_at": "2021-12-29T01:19:35.000000Z",
"priority": 15
}
],
"available": []
},
{
"id": 30,
"name": "condition",
"isrequired": "required",
"valuetype": "text",
"priority": 4,
"inputtype": "radiobutton",
"max_min": [],
"rangeable": "false",
"choices": [
{
"id": 44,
"name": "Used",
"categoryextrafield_id": 30,
"created_at": "2021-12-29T01:20:31.000000Z",
"updated_at": "2021-12-29T01:20:31.000000Z",
"priority": 10
},
{
"id": 45,
"name": "New",
"categoryextrafield_id": 30,
"created_at": "2021-12-29T01:20:38.000000Z",
"updated_at": "2021-12-29T01:20:38.000000Z",
"priority": 20
}
],
"available": []
}
]
}

Error parsing a specific JSON file in Snowflake with File Format

I have created a Stage and File Format in Snowflake which works with all my other JSON files except this, which throws an error:
Error parsing JSON: misplaced { File 'rooms.json.gz', line 1,
character 2 Row 0, column $1
I am using the same query that I am using for other files.
SELECT $1
FROM #MySchema.MY_STAGE/rooms.json.gz
;
What is wrong with the structure of this specific JSON file?
{
"rooms": [
{
"area": 131.49,
"longDescription": "",
"dateCreated": 1589908063390,
"reservable": false,
"name": "E249",
"remoteInfo": "",
"description": "",
"id": 2,
"type": {
"hexColor": "c16058",
"contentFlag": 1,
"cost": 0.0,
"dateCreated": 1308610520717,
"color": {},
"name": "BREAK ROOM",
"occupiable": false,
"id": 120,
"parkingSpace": false,
"dateUpdated": 1591818585913,
"typeCode": ""
},
"floor": {
"area": 25312.9878,
"dateCreated": 1589907703870,
"drawingAvailable": true,
"interiorGross": 0.0,
"name": "2",
"leaseArea": 0.0,
"id": 12,
"building": {
"address": {
"country": {
"defaultSelected": true,
"subdivisionCategoryName": "state",
"alpha2Code": "US",
"isoCode": "US",
"name": "United States of America (the)",
"id": 223
},
"city": "Some City",
"street": "Some Drive",
"postalCode": "00000",
"state": {
"country": {
"defaultSelected": true,
"subdivisionCategoryName": "state",
"alpha2Code": "US",
"isoCode": "US",
"name": "United States of America (the)",
"id": 223
},
"defaultSelected": false,
"code": "XX",
"name": "Some State",
"id": 66,
"categoryName": "state"
}
},
"code": "B2",
"dateCreated": 1589907508020,
"metric": false,
"name": "Some name",
"location": {},
"revitLink": "",
"id": 45,
"dateUpdated": 1601315841453,
"costCenters": []
},
"dateUpdated": 1600441936663
},
"capacity": 0,
"dateUpdated": 1600441936960
}
]
}
Edit: Screenshot from Notepad++ with all characters enabled

Map Reduce/Sum using Underscorejs

I am trying to convert the following dataset:
var data = {
"csrf": "token",
"items": [
{"category": "product1", "image": "image1.jpg", "cost": "$6", "item_totals": [
{"category": "discount", "amount": "-$1.0"}]
},
{"category": "product2", "image": "image2.jpg", "cost": "$8", "item_totals": [
{"category": "discount", "amount": "-$1.2"}]
}],
"totals": [
{"category": "shipping", "amount": "$0.00", "name": "Shipping", "is_zero": true},
{"category": "taxes", "amount": "$0.00", "name": "Taxes", "is_zero": true}
],
"total": "$1234", "subtotal": "$1234", "id": abc123, "currency_code": "USD"}
into a single value, being the sum of all 'amount' values in the item_totals array(s). I cannot seem to do this whilst accounting a varying amount of records in 'items' & for empty 'item_total' arrays. Any help would be great!
var data = {
"csrf": "token",
"items": [{
"category": "product1",
"image": "image1.jpg",
"cost": "$6",
"item_totals": [{
"category": "discount",
"amount": "-$1.0"
}]
},
{
"category": "product2",
"image": "image2.jpg",
"cost": "$8",
"item_totals": [ // multiple item_totals
{
"category": "discount",
"amount": "-$1.2"
},
{
"category": "discount",
"amount": "-$1.2"
}
]
},
{
"category": "product2",
"image": "image2.jpg",
"cost": "$8",
"item_totals": [] // empty item_totals
},
{
"category": "product2",
"image": "image2.jpg",
"cost": "$8"
// absent item_totals
}
],
"totals": [{
"category": "shipping",
"amount": "$0.00",
"name": "Shipping",
"is_zero": true
},
{
"category": "taxes",
"amount": "$0.00",
"name": "Taxes",
"is_zero": true
}
],
"total": "$1234",
"subtotal": "$1234",
"id": "abc123",
"currency_code": "USD"
}
var totalAmount = _.chain(data.items)
.map(item => item.item_totals || []) // emit totals
.tap(console.log)
.reduce((memo, bucket) => memo.concat(bucket), []) // collect (flatten)
.tap(console.log)
.map(total => parseFloat(total.amount.replace('$', ''), 10)) // emit amounts
.tap(console.log)
.reduce((ret, amount) => ret + amount, 0) // collect (sum)
.value();
console.log(totalAmount);
<script src="http://underscorejs.org/underscore.js"></script>

how to sort a json file in shell script based on a given key?

I wanted to sort the file which have json data on basis of a key(fare) in a shell script (.sh) file.Is there a way to acheive the same ?
{ "route":[
{"match":"true","column":"10","fare":"120.0","source":"false","length":"1","name":"41","row":"4","width":"1","zIndex":"0"},
{"match":"true","column":"9","fare":"110.0","source":"false","length":"1","name":"37","row":"3","width":"1","zIndex":"0"},
{"match":"true","column":"8","fare":"500.0","source":"false","length":"1","name":"33","row":"2","width":"1","zIndex":"0"},
{"match":"true","column":"7","fare":"510.0","source":"false","length":"1","name":"29","row":"1","width":"1","zIndex":"0"},
{"match":"true","column":"6","fare":"50.0","source":"false","length":"1","name":"29","row":"0","width":"1","zIndex":"0"}
]
};
jq '.route = (.route | sort_by(.fare))' file.json
{
"route": [
{
"zIndex": "0",
"match": "true",
"column": "9",
"fare": "110.0",
"source": "false",
"length": "1",
"name": "37",
"row": "3",
"width": "1"
},
{
"zIndex": "0",
"match": "true",
"column": "10",
"fare": "120.0",
"source": "false",
"length": "1",
"name": "41",
"row": "4",
"width": "1"
},
{
"zIndex": "0",
"match": "true",
"column": "6",
"fare": "50.0",
"source": "false",
"length": "1",
"name": "29",
"row": "0",
"width": "1"
},
{
"zIndex": "0",
"match": "true",
"column": "8",
"fare": "500.0",
"source": "false",
"length": "1",
"name": "33",
"row": "2",
"width": "1"
},
{
"zIndex": "0",
"match": "true",
"column": "7",
"fare": "510.0",
"source": "false",
"length": "1",
"name": "29",
"row": "1",
"width": "1"
}
]
}
Note that the sorting is lexical ("50" is in the middle). This is because the values are strings not numbers. To get numeric sorting, we need to cast as a number
jq '.route = (.route | sort_by(.fare | tonumber))' file.json
{
"route": [
{
"zIndex": "0",
"match": "true",
"column": "6",
"fare": "50.0",
"source": "false",
"length": "1",
"name": "29",
"row": "0",
"width": "1"
},
{
"zIndex": "0",
"match": "true",
"column": "9",
"fare": "110.0",
"source": "false",
"length": "1",
"name": "37",
"row": "3",
"width": "1"
},
{
"zIndex": "0",
"match": "true",
"column": "10",
"fare": "120.0",
"source": "false",
"length": "1",
"name": "41",
"row": "4",
"width": "1"
},
{
"zIndex": "0",
"match": "true",
"column": "8",
"fare": "500.0",
"source": "false",
"length": "1",
"name": "33",
"row": "2",
"width": "1"
},
{
"zIndex": "0",
"match": "true",
"column": "7",
"fare": "510.0",
"source": "false",
"length": "1",
"name": "29",
"row": "1",
"width": "1"
}
]
}

Parse Json with different type of inner array

I have a great problem for parse a very difficult json string. For Eg
{
"facilityDetails": [
{
"tableName": "FACILITY",
"facilityDetails": [
{
"id": 1,
"itemId": "s101",
"name": "facility",
"status": 1,
"lastEditedOn": "01/Jan/201200: 00: 00.000"
}
]
},
{
"tableName": "PLACE_SERVICE",
"facilityDetails": [
{
"id": 1,
"itemId": "22",
"name": "placeservice",
"facility": "5",
"status": 1,
"lastEditedOn": "01/Jan/201000: 00: 00.000"
},
{
"id": 2,
"itemId": "55",
"name": "placeservice",
"facility": "t",
"status": 2,
"lastEditedOn": "01/Jan/201000: 00: 00.000"
},
{
"id": 3,
"itemId": "99",
"name": "placeservice",
"facility": "r",
"status": 33,
"lastEditedOn": "01/Jan/201000: 00: 00.000"
},
{
"id": 4,
"itemId": "22",
"name": "placeservice",
"facility": "",
"status": 0,
"lastEditedOn": "01/Jan/201000: 00: 00.000"
}
]
},
{
"tableName": "AGENT",
"facilityDetails": [
{
"agentId": 2,
"itemId": "1",
"name": "agent",
"defUnitId": 0,
"defRouteId": 0,
"color": "",
"synonyms": "",
"administrationType": 0,
"status": 0,
"lastEditedOn": "01/Jan/201200: 00: 00.000"
}
]
}
]
}
for this json string "facilityDetails" is an inner array that have different details at each time. How can i parse this type of json. If anyone know please help me
You should be able to parse this with the Json.NET library.
http://james.newtonking.com/projects/json-net.aspx