Going from a flat stucture to a hierarchy using jsonata - jsonata

I am tryig to use jsonata to to transform a flat json result set to something that aggregates some items and creates arrays.
Any advice on how I go from a source like this
`
{
"name": "myname",
"payload": [
{
"role": "Testrole 1",
"permission": "Testpermission 1"
},
{
"role": "Testrole 1",
"permission": "Testpermission 2"
},
{
"role": "Testrole 2",
"permission": "Testpermission 3"
}
]
}
`
to a target like this
`
{
"name": "myname",
"roles": [
{
"rolename": "Testrole 1",
"permissions": [
{
"permissionname": "Testpermission 1"
},
{
"permissionname": "Testpermission 2"
}
]
},
{
"rolename": "Testrole 2",
"permissions": [
{
"permissionname": "Testpermission 3"
}
]
}
]
}
`
I've checked the jsonata documentation but cannot find a good way to achieve this.

You can use the reduce operator to group by a role as described in the Grouping docs here, and then map it to your desired shape:
{
"name": name,
"roles": payload{
role: permission
} ~> $each(function($permissions, $role) {
{
"rolename": $role,
"permissions": $permissions.{ "permissionname": $ }
}
})
}
Live example on the playground: https://stedi.link/WFEPgxp

Related

Incorrectly selected data in the query

Only articles that contain the EmailMarketing tag are needed.
I'm probably doing the wrong search on the tag, since it's an array of values, not a single object, but I don't know how to do it right, I'm just learning graphql. Any help would be appreciated
query:
query {
enArticles {
title
previewText
tags(where: {name: "EmailMarketing"}){
name
}
}
}
result:
{
"data": {
"enArticles": [
{
"title": "title1",
"previewText": "previewText1",
"tags": [
{
"name": "EmailMarketing"
},
{
"name": "Personalization"
},
{
"name": "Advertising_campaign"
}
]
},
{
"title": "title2",
"previewText": "previewText2",
"tags": [
{
"name": "Marketing_strategy"
},
{
"name": "Marketing"
},
{
"name": "Marketing_campaign"
}
]
},
{
"title": "article 12",
"previewText": "article12",
"tags": []
}
]
}
}
I believe you first need to have coded an equality operator within your GraphQL schema. There's a good explanation of that here.
Once you add an equality operator - say, for example _eq - you can use it something like this:
query {
enArticles {
title
previewText
tags(where: {name: {_eq: "EmailMarketing"}}){
name
}
}
}
Specifically, you would need to create a filter and resolver.
The example here may help.

Patch Values Reactive Forms Array of Array Angular 2

{
"id": "1",
"name": ""
"parts": [
{
"name": "partname1",
"description": "description 1"
"messages": [
{
"text": "text 1"
"sender": "sender 1"
}
]
},
{
"name": "partname2",
"description": "description 1"
"messages": [
{
"text": "text 2"
"sender": "sender 2"
}
]
}
]
}
Based on this: https://angular.io/guide/reactive-forms
Using this code:
setParts(parts) {
const partsFormGroup= parts.map(part => this.fb.group(part));
const partsFormArray = this.fb.array(partsFormGroup);
this.myForm.setControl('parts', partsFormArray);
}
patchValue works fine for parts properties: name & description.
How to patchValue to messages?
I am getting this error:
ERROR TypeError: this.validator is not a function

How can i create FHIR ValueSet with hierarchical content?

I want create ValueSet with link to my CodeSystem.
ValueSet expantion with enclosed contains should be like:
{
...
"expansion": {
"parameter": [
{
"name": "total",
"valueString": "282"
}
],
"contains": [
{
"code": "0",
"display": "asd",
"contains": [
{
"code": "High"
},
{
"code": "Okso",
"display": "1"
}
]
}
]
}
...
}
I tried to create CodeSystem with hierarchical concepts, and ValueSet with compose.include to my CodeSystem. But i getted expantion with 1 level contents like this:
"contains": [
{
"code": "0",
"display": "asd"
}, {
"code": "High"
}, {
"code": "Okso",
"display": "1"
}
]
I tried change different attributes CodeSystem, but this bring me no result.
Help me please with a simple example!

artillery.io xhr poll error

I am unable to connect using artillery.io with setting engine = socketio, please find my configuration json below
socket error {"type":"Transport error", "description":400}
"scenarios": [
{
"name": "my test",
"engine": "socketio",
"flow": [
{
"emit": {
"channel": "command",
"namespace": "command"
}
},
{
"think": 1
}
]
}
]
"scenarios": [
{
"name": "my test",
"engine": "socketio",
"flow": [
{
"emit": {
"channel": "command"
"data": "hello"
"namespace": "/command"
}
},
{
"think": 1
}
]
}
]
Please try using above command

Extracting data from Deeply Nested JSON in Ruby on Rails

Have a Json out put like
{
"query": {
"results": {
"industry": [
{
"id": "112",
"name": "Agricultural Chemicals",
"company": [
{
"name": "Adarsh Plant",
"symbol": "ADARSHPL"
},
{
"name": "Agrium Inc",
"symbol": "AGU"
}
},
]
{
"id": "914",
"name": "Water Utilities",
"company": [
{
"name": "Acque Potabili",
"symbol": "ACP"
},
{
"name": "Water Resources Group",
"symbol": "WRG"
}
]
}
]
}
}
}
Need the out put like - Company Name, Company Symbol, Company id,
Company id name
and example of output would be
Adarsh Plant, ADARSHPL, 112, Agricultural Chemicals
Agrium Inc, AGU, 112, Agricultural Chemicals
Acque Potabili, ACP, 914, Water Utilities
Water Resources Group, WRG, 914, Water Utilities
Any suggestions
There's a typo in your sample json, but we'll talk about it later.
Assuming your json data converted to hash object already like this:
json={
"query"=> {
"results"=> {
"industry"=> [
{
"id"=> "112",
"name"=> "Agricultural Chemicals",
"company"=> [
{
"name"=> "Adarsh Plant",
"symbol"=> "ADARSHPL"
},
{
"name"=> "Agrium Inc",
"symbol"=> "AGU"
}
]
},
{
"id"=> "914",
"name"=> "Water Utilities",
"company"=> [
{
"name"=> "Acque Potabili",
"symbol"=> "ACP"
},
{
"name"=> "Water Resources Group",
"symbol"=> "WRG"
}
]
}
]
}
}
}
You can use inject and map to handle the two level array of industry, inject will iterate the outer array:
json["query"]["results"]["industry"].inject([]){|m,o|
m += o["company"].map{|x| [x["name"],x["symbol"],o["id"],o["name"]]}
}
result is an array of arrays with the order as you wish:
=> [["Adarsh Plant", "ADARSHPL", "112", "Agricultural Chemicals"],
["Agrium Inc", "AGU", "112", "Agricultural Chemicals"],
["Acque Potabili", "ACP", "914", "Water Utilities"],
["Water Resources Group", "WRG", "914", "Water Utilities"]]
If you want get a string delimited by comma, you could chain on .flatten.join(",") at the end.
json["query"]["results"]["industry"].inject([]){|m,o|
m += o["company"].map{|x| [x["name"],x["symbol"],o["id"],o["name"]]}
}.flatten.join(",")
Result:
=> Adarsh Plant,ADARSHPL,112,Agricultural Chemicals,Agrium Inc,AGU,112,Agricultural Chemicals,Acque Potabili,ACP,914,Water Utilities,Water Resources Group,WRG,914,Water Utilities
The typo of your json data:
In the middle }, ] { should be changed to ] },{ .
Convert json to hash
https://stackoverflow.com/a/7964378/3630826

Resources