Qt Parsing and comparing the value in qt - json

I have json format like this->
{
"id": 7,
"sort_decs": "Identity",
"last_name": "Lawson",
},
{
"id": 8,
"sort_decs": "Identity",
"last_name": "Ferguson",
},
{
"id": 9,
"sort_decs": "Identity",
"last_name": "Funke",
},
{
"id": 10,
"sort_decs": "Identity",
"last_name": "Fields",
},
{
"id": 11,
"sort_decs": "Identity",
"last_name": "Edwards",
}
In qtreewidget how can I get sort_desc value for parent1 and last_name in its childs. if the sort_decs value is repeated how can get it only once in parent1 and last_name 's in childs .
I am gettting if the sort_decs value is == "Identity" manually.

You may not even need to use TreeView. If I understand your data correctly you could use a ListView with section.property: "sort_decs":
import QtQuick
import QtQuick.Controls
Page {
ListView {
id: listview
anchors.fill: parent
model: [
{ "id": 1, "sort_decs": "Griffindor", "last_name": "Potter", },
{ "id": 2, "sort_decs": "Griffindor", "last_name": "Weasley", },
{ "id": 3, "sort_decs": "Griffindor", "last_name": "Granger", },
{ "id": 4, "sort_decs": "Slytherin", "last_name": "Malfoy", },
{ "id": 5, "sort_decs": "Slytherin", "last_name": "Crabbe", },
{ "id": 6, "sort_decs": "Slytherin", "last_name": "Goyle", },
{ "id": 7, "sort_decs": "Identity", "last_name": "Lawson", },
{ "id": 8, "sort_decs": "Identity", "last_name": "Ferguson", },
{ "id": 9, "sort_decs": "Identity", "last_name": "Funke", },
{ "id": 10, "sort_decs": "Identity", "last_name": "Fields", },
{ "id": 11, "sort_decs": "Identity", "last_name": "Edwards", }
]
delegate: ItemDelegate {
width: ListView.view.width - 20
background: Item { }
text: modelData.last_name
padding: 0
icon.source: "Tree.svg"
}
section.property: "sort_decs"
section.criteria: ViewSection.FullString
section.delegate: ItemDelegate {
//width: ListView.view.width - 20
padding: 2
background: Rectangle { border.color: "#888" }
text: section; font.pointSize: 14
}
ScrollBar.vertical: ScrollBar {
width: 20
policy: ScrollBar.AlwaysOn
}
}
}
// Tree.svg
<svg width="32" height="32">
<path d="M16 0 16 32 M16 16 32 16" stroke="black" fill="none"/>
</svg>
You can Try it Online!

Related

Why JsonNode.get("key").asText() is returning empty String

I have a problem with saving to playersJson String, data from node "players". Node is created based on json dataJson, created with byte[] playesFile. I suspect that the error is due to incorrect parsing of these strings?
As a result, String playersJson returns an empty String and I would like it to contain information about "players" in json format, which I will then throw into Map<String,Player>
Code fragment:
byte[] playersFile = storageService.downloadFile(FILENAME);
String dataJson = new String(playersFile);
try {
JsonNode jsonNode = objectMapper.readTree(dataJson);
String playersJson = jsonNode.get("players").asText();
data = objectMapper.readValue(playersJson, new TypeReference<Map<String, Player>>(){});
} catch (JsonProcessingException e) {
e.printStackTrace();
}
String dataJson:
{
"players":
[
{
"key": "03a2452c-9d6b-47f5-9616-9a6833312762",
"value": {
"uuid": "2ae8d022-e0f4-4502-8e0e-1874997543e3",
"name": "Artur",
"elo": 2000,
"gamesPlayed": 0,
"email": "arti123i#mail.com"
}
},
{
"key": "8526db7c-6930-45bf-9ae1-fb93e97ff4ba",
"value": {
"uuid": "1bb43d73-3f94-40fc-a680-99f4a9304001",
"name": "Kamil",
"elo": 2000,
"gamesPlayed": 0,
"email": "kamil223#mail.com"
}
},
{
"key": "5a65ba8c-2180-464b-afe4-69c29b785282",
"value": {
"uuid": "b3dc8c98-5759-433e-88cd-0233946b9241",
"name": "Marek",
"elo": 3000,
"gamesPlayed": 0,
"email": "marek111#mail.com"
}
}
],
"games":
[
{
"reportedTime": "2022-07-11T14:43:10.0354202+02:00",
"reportedBy": {
"uuid": "2ae8d022-e0f4-4502-8e0e-1874997543e3",
"name": "Artur",
"elo": 2016,
"gamesPlayed": 1,
"email": "arti123i#mail.com"
},
"result": [
[
{
"player": {
"uuid": "2ae8d022-e0f4-4502-8e0e-1874997543e3",
"name": "Artur",
"elo": 2016,
"gamesPlayed": 1,
"email": "arti123i#mail.com"
},
"eloBefore": 2000,
"eloAfter": 2016
},
{
"player": {
"uuid": "1bb43d73-3f94-40fc-a680-99f4a9304001",
"name": "Kamil",
"elo": 2016,
"gamesPlayed": 1,
"email": "kamil223#mail.com"
},
"eloBefore": 2000,
"eloAfter": 2016
}
],
[
{
"player": {
"uuid": "b3dc8c98-5759-433e-88cd-0233946b9241",
"name": "Marek",
"elo": 2968,
"gamesPlayed": 1,
"email": "marek111#mail.com"
},
"eloBefore": 3000,
"eloAfter": 2968
}
]
]
}
]
}
code image
code debugging

JSON Transformation using JQ

I am trying to transform my JSON to different structure using JQ. I am able to partially achieve my new structure, however i get a additional blocks of data.
As i iterate i am able to get the structure in new format. But additional structures are coming.
Code Snippet- https://jqplay.org/s/3gulSlZiWz
JSON
{
"amazon": {
"activeitem": 2,
"createdDate": "2019-01-15T17:36:31.588Z",
"lastModifiedDate": "2019-01-15T17:36:31.588Z",
"user": "net",
"userType": "new",
"items": [
{
"id": 1,
"name": "harry potter",
"state": "sold",
"type": {
"branded": false,
"description": "artwork",
"contentLevel": "season"
}
},
{
"id": 2,
"name": "adidas shoes",
"state": "in inventory",
"type": {
"branded": false,
"description": "Spprts",
"contentLevel": "season"
}
},
{
"id": 3,
"name": "watch",
"state": "returned",
"type": {
"branded": false,
"description": "walking",
"contentLevel": "special"
}
},
{
"id": 4,
"name": "adidas shoes",
"state": "in inventory",
"type": {
"branded": false,
"description": "running",
"contentLevel": "winter"
}
}
],
"product": {
"id": 4,
"name": "adidas shoes",
"source": "dealer",
"destination": "resident"
}
}
}
JQ Query:
.amazon|
{
userType: .userType,
userName: .user,
itemCatalog: {
itemId: .items[].id,
name: .items[].name
}
}
Expected Response:
{
"userType": "new",
"userName": "net",
"itemCatalog": {
"itemId": 1,
"name": "harry potter"
},{
"itemId": 2,
"name": "adidas shoes"
}, {
"itemId": 3,
"name": "watch"
},{
"itemId": 4,
"name": "adidas shoes"
}
}
But getting something weird long duplicated response.
As already pointed out in a comment, the "expected response" is not JSON and probably not what you want anyway. The following would make sense and in any case illustrates how to iterate appropriately:
.amazon
| { userType: .userType,
userName: .user,
itemCatalog: (.items | map({ itemId: .id, name} ))
}
Output
{
"userType": "new",
"userName": "net",
"itemCatalog": [
{
"itemId": 1,
"name": "harry potter"
},
{
"itemId": 2,
"name": "adidas shoes"
},
{
"itemId": 3,
"name": "watch"
},
{
"itemId": 4,
"name": "adidas shoes"
}
]
}

Modify array elements while grouping by a specific key using jq

Is it possible to modify/replace array elements while grouping by a specific key (.[].Parameter.Id) such that this array:
[{
"Id": 48,
"Parameter": {
"Id": 17
}
}, {
"Id": 196,
"Parameter": {
"Id": 17
}
}]
becomes this:
[
{
"p17": [48, 196]
}
]
Here is the source JSON file for a complete example:
[{
"Id": 78,
"PromotionType": 2,
"Amount": "100",
"UpperLimit": null,
"Variables": [{
"Id": 100,
"Parameter": {
"Id": 30
}
}]
}, {
"Id": 84,
"PromotionType": 2,
"Amount": null,
"UpperLimit": null,
"Variables": [{
"Id": 48,
"Parameter": {
"Id": 17
}
}, {
"Id": 196,
"Parameter": {
"Id": 17
}
}, {
"Id": 59,
"Parameter": {
"Id": 21
}
}, {
"Id": 60,
"Parameter": {
"Id": 21
}
}, {
"Id": 62,
"Parameter": {
"Id": 21
}
}]
}, {
"Id": 59,
"PromotionType": 2,
"Amount": "666.6",
"UpperLimit": null,
"Variables": [{
"Id": 96,
"Parameter": {
"Id": 8
}
}, {
"Id": 47,
"Parameter": {
"Id": 17
}
}]
}]
What I want to achieve is this:
[{
"Id": 78,
"PromotionType": 2,
"Amount": "100",
"UpperLimit": null,
"Variables": [{
"p30": [100]
}]
}, {
"Id": 84,
"PromotionType": 2,
"Amount": null,
"UpperLimit": null,
"Variables": [{
"p17": [48, 196]
}, {
"p21": [59, 60, 62]
}]
}, {
"Id": 59,
"PromotionType": 2,
"Amount": "666.6",
"UpperLimit": null,
"Variables": [{
"p8": [96]
}, {
"p17": [47]
}]
}]
I am reading through jq manual, jq cookbook and found some functions (e.g. with_entries, unique_by, inputs) that might help but could not figure out how to make it work.
Number of objects/inner objects are also not fixed. So I cannot simply replace using array indexes.
Any help would be appreciated.
Thanks,
Emre
jq solution:
jq 'map(.Variables
|= (group_by(.Parameter.Id)
| map(("p" + (.[0].Parameter.Id | tostring)) as $pid
| { ($pid) : map(.Id) }
)
)
)' input.json
The output:
[
{
"Id": 78,
"PromotionType": 2,
"Amount": "100",
"UpperLimit": null,
"Variables": [
{
"p30": [
100
]
}
]
},
{
"Id": 84,
"PromotionType": 2,
"Amount": null,
"UpperLimit": null,
"Variables": [
{
"p17": [
48,
196
]
},
{
"p21": [
59,
60,
62
]
}
]
},
{
"Id": 59,
"PromotionType": 2,
"Amount": "666.6",
"UpperLimit": null,
"Variables": [
{
"p8": [
96
]
},
{
"p17": [
47
]
}
]
}
]

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).

Aggregate two payloads in Mule ESB

My mule code is hitting two tables and getting some details.
First one is order details, which I am storing in a flow variable i.e order and another database is returning order item details which I am storing in orderitem variable.
I want to aggregate both the payload based on one condition. Every orderId has order items (which is stored in flowVars.orderitem) and map these order items to respective orderID.
flowVars.order value is as below
[{partnerId=e83185e9f33e4234ba9eaa81dba515ad, orderId=12345, orderDate=2017-02-28 16:41:41.0, id=22}, {partnerId=e83185e9f33e4234ba9eaa81dba515ad, orderId=123456, orderDate=2017-02-28 16:41:41.0, id=23}, {partnerId=e83185e9f33e4234ba9eaa81dba515ad, orderId=11111, orderDate=2017-02-28 16:41:41.0, id=24}, {partnerId=e83185e9f33e4234ba9eaa81dba515ad, orderId=321123, orderDate=2017-05-19 15:25:41.0, id=26}]
and flowVars.orderitem value is as below
[{productCode=ELT-LP-ICND1-020067, orderId=12345, quantity=10, id=14}, {productCode=ELT-IP-ICND1-1.0, orderId=12345, quantity=11, id=15}, {productCode=ELT-LP-ICND1-020067, orderId=123456, quantity=12, id=16}, {productCode=ELT-IP-ICND1-1.0, orderId=123456, quantity=13, id=17}, {productCode=ELT-LP-ICND1-020067, orderId=11111, quantity=14, id=18}, {productCode=ELT-IP-ICND1-1.0, orderId=11111, quantity=15, id=19}, {productCode=ELT-LP-ICND2-020067, orderId=321123, quantity=5, id=20}]
Expected Output
[
{
"orderId": "12345",
"orderDate": "2017-02-28T16:41:41",
"partnerId": "e83185e9f33e4234ba9eaa81dba515ad",
"orderItems": [
{
"productCode": "ELT-LP-ICND1-020067",
"quantity": "10"
},
{
"productCode": "ELT-IP-ICND1-1.0",
"quantity": "11"
}
]
},
{
"orderId": "123456",
"orderDate": "2017-02-28T16:41:41",
"partnerId": "e83185e9f33e4234ba9eaa81dba515ad",
"orderItems": [
{
"productCode": "ELT-LP-ICND1-020067",
"quantity": "12"
},
{
"productCode": "ELT-IP-ICND1-1.0",
"quantity": "13"
}
]
},
{
"orderId": "11111",
"orderDate": "2017-02-28T16:41:41",
"partnerId": "e83185e9f33e4234ba9eaa81dba515ad",
"orderItems": [
{
"productCode": "ELT-LP-ICND1-020067",
"quantity": "14"
},
{
"productCode": "ELT-IP-ICND1-1.0",
"quantity": "15"
}
]
},
{
"orderId": "321123",
"orderDate": "2017-05-19T15:25:41",
"partnerId": "e83185e9f33e4234ba9eaa81dba515ad",
"orderItems": [
{
"productCode": "ELT-LP-ICND1-020067",
"quantity": "5"
}
]
}
]
Here I need to show respective order item details of an order. So basically I need to combine both the payloads.
I tried using dataweave but not luck.
Dataweave code:
%dw 1.0
%output application/json
%var mergeddata = flowVars.orderitem groupBy $.orderId
---
flowVars.order map ((data,index) ->
{
orderid: data.orderId,
partnerid: data.partnerId,
orderdate: data.orderDate,
order: flowVars.orderitem default [] map ((data1 ,indexOf) ->
{
(productcode: data1.productCode) when (data1.orderId == data.orderId),
(quantity: data1.quantity) when (data1.orderId == data.orderId) ,
(id: data1.id) when (data1.orderId == data.orderId)
}
)})
And output after transformation:
{
"orderid": "12345",
"partnerid": "e83185e9f33e4234ba9eaa81dba515ad",
"orderdate": "2017-02-28T16:41:41",
"order": [
{
"productcode": "ELT-LP-ICND1-020067",
"quantity": 10,
"id": 14
},
{
"productcode": "ELT-IP-ICND1-1.0",
"quantity": 11,
"id": 15
},
{
},
{
},
{
},
{
},
{
}
]
},
{
"orderid": "123456",
"partnerid": "e83185e9f33e4234ba9eaa81dba515ad",
"orderdate": "2017-02-28T16:41:41",
"order": [
{
},
{
},
{
"productcode": "ELT-LP-ICND1-020067",
"quantity": 12,
"id": 16
},
{
"productcode": "ELT-IP-ICND1-1.0",
"quantity": 13,
"id": 17
},
{
},
{
},
{
}
]
},
{
"orderid": "11111",
"partnerid": "e83185e9f33e4234ba9eaa81dba515ad",
"orderdate": "2017-02-28T16:41:41",
"order": [
{
},
{
},
{
},
{
},
{
"productcode": "ELT-LP-ICND1-020067",
"quantity": 14,
"id": 18
},
{
"productcode": "ELT-IP-ICND1-1.0",
"quantity": 15,
"id": 19
},
{
}
]
},
{
"orderid": "321123",
"partnerid": "e83185e9f33e4234ba9eaa81dba515ad",
"orderdate": "2017-05-19T15:25:41",
"order": [
{
},
{
},
{
},
{
},
{
},
{
},
{
"productcode": "ELT-LP-ICND2-020067",
"quantity": 5,
"id": 20
}
]
}
]
As you can see that I am almost there and able to map order item details with respective orderId but still I am getting some blank values after transformation.
Can anyone help me to achieve expected output. Thanks in advance!!!
You need to filter the flowVars.orderitem map, rather than iterate it in full and only print values when the orderId matches.
order: ((flowVars.orderitem default []) filter (data.orderId == $.orderId)) map ((data1 ,indexOf) -> {
productcode: data1.productCode,
quantity: data1.quantity
id: data1.id
})
You can then remove all of those 'when' statements too.