SQL table rec_call has a jsonb column called config. It is deeply nested array of objects. That objects have items field which is array of objects. Further those objects have concepts which is an array of objects.
[{ "items": [{ "concepts": [{ "text": "hello" }] }] }]
Except the field $.items[*].concepts[*].text every field is optional.
Select config from rec_call LIMIT 1;
[
{
"items": [
{
"tag": "whitelist",
"concepts": [
{
"text": "cable",
"wordsAttributes": {
"groupName": "Festnetz",
"score": 1.0
},
"score": 0.0,
"count": 0
},
{
"text": "tv",
"wordsAttributes": {
"groupName": "Group 2",
"score": 1.0
}
},
{
"text": "adhl",
},
{
"text": "internet",
"wordsAttributes": {
"groupName": "Group 2",
"score": 1.0
}
}
]
},
{
"tag": "blacklist",
"concepts": [
{
"text": "cable",
"wordsAttributes": {
"groupName": "sssdd",
"score": 1.0
},
"textOriginal": "Cable",
"score": 0.0,
"count": 0
},
{
"text": "cable"
}
]
},
{
"tag": "filler",
"concepts": [
{
"text": "something",
"wordsAttributes": {
"groupName": "filler",
"score": 1.0
},
"score": 0.0,
"count": 0
}
]
}
]
}
]
Write a migration to transform config column for every row to following format:
Field transformations mappings:
first level grouping is by tag => whitelist | blacklist | filler,
second level grouping is done by wordsAttributes.groupName. If not exist than `group` is empty string .
for each group every concepts[*]->text => pushed into texts field
score => wordsAttributes.score
other is empty array
End result should be:
{
"whitelist": [
{
group: "Festnetz",
score: 1,
texts: ["cable"],
other: []
},
{
group: "Group 2",
score: 1,
texts: ["tv", "internet"],
other: []
},
{
group: "",
score: 1,
texts: ["adhl"],
other: []
}
],
"blacklist": [
{
group: "sssdd",
score: 1,
texts: ["something"],
other: []
}
],
"filler": [
{
group: "filler",
score: 1,
texts: ["Cable"],
other: []
}
]
}
Postgress 11, but if not possible than postgress >11