Transform JSON array using ES 6 methods - javascript

I have the following example of an array format that needs to be transformed.
{ [
{
"condition": "$and",
"children": [
{ "column": "Title", "comparison": "$eq", "columnValue": "1" },
{ "column": "Event Status", "comparison": "$eq", "columnValue": "2" }
]
},
{
"condition": "$or",
"children": [
{
"column": "Issue Description",
"comparison": "$lt",
"columnValue": "3"
},
{ "column": "Number Label", "comparison": "$gte", "columnValue": "4" }
]
}
]}
It needs to be transformed like this...
{
[
{
"$and" : [
{
"Title" : {
"$eq" : "1"
}
},
{
"Event Status" : {
"$eq" : "2"
}
}
]
},
{
"$or" : [
{
"Issue Description" : {
"$lt" : "3"
}
},
{
"Number Label" : {
"$gte" : "4"
}
}
]
}
]
}
I've tried various iterations of map and reduce. Gotten close, but not completely there.
This is in a Vue project. Here is an example of what I tried.
const result = this.parents.map(({ condition, children }) => {
const childArray = children.reduce(
(c, v) => ({
...c,
[v.column]: { [v.comparison]: v.columnValue }
}),
{}
);
childArray.condition = condition;
return childArray;
});
This returns:
[
{
"Title": { "$eq": "1" },
"Event Status": { "$eq": "2" },
"condition": "$and"
},
{
"Issue Description": { "$lt": "3" },
"Number Label": { "$gte": "4" },
"condition": "$or"
}
]
I cannot figure out how to get the "condition" key in the right place.

ES6 computed property names will be a big help, allowing variable expression enclosed in [] square braces to compute a key value...
let inputExpressions = [
{
"condition": "$and",
"children": [
{ "column": "Title", "comparison": "$eq", "columnValue": "1" },
{ "column": "Event Status", "comparison": "$eq", "columnValue": "2" }
]
},
{
"condition": "$or",
"children": [
{
"column": "Issue Description",
"comparison": "$lt",
"columnValue": "3"
},
{ "column": "Number Label", "comparison": "$gte", "columnValue": "4" }
]
}
];
function translateExpression(expression) {
const translateClause = clause => {
return { [clause.column] : { [clause.comparison] : clause.columnValue } };
};
return { [expression.condition] : expression.children.map(translateClause) };
}
let resultExpressions = inputExpressions.map(translateExpression);
console.log(resultExpressions)

Related

Get key and value & key and object with reduce function javascript

I have below data from my API call and I am working with reduce function to set data as per my requirement. But not able to produce it, please check below code and required output. Also check the function I am trying and suggest me way to create the response.
API Response:
let cmpRecords = {
"status": "success",
"message": "Record Fetched Successfully",
"data": [
{
"_id": "6098ff60a8e9ee2c7c116d6e",
"record": {
"creatorName": "ABC",
"modifierName": ""
},
"is_deleted": "0"
},
{
"_id": "6098ff60a8e9ee2c7c116d6e",
"record": {
"creatorName": "ABC",
"modifierName": ""
},
"is_deleted": "0"
},
{
"_id": "6098ff60a8e9ee2c7c116d6z",
"record": {
"creatorName": "XYZ",
"modifierName": ""
},
"is_deleted": "0"
},
...
]
}
Required Output ::
{
"status": "success",
"message": "Record Fetched Successfully",
"data": [
{
"name": "ABC",
"record": [
{
"_id": "6098ff60a8e9ee2c7c116d6e",
"record": {
"creatorName": "ABC",
"modifierName": ""
},
"is_deleted": "0"
},
{
"_id": "6098ff60a8e9ee2c7c116d6e",
"record": {
"creatorName": "ABC",
"modifierName": ""
},
"is_deleted": "0"
}
]
},
{
"name": "XYZ",
"record": [
{
"_id": "6098ff60a8e9ee2c7c116d6z",
"record": {
"creatorName": "XYZ",
"modifierName": ""
},
"is_deleted": "0"
}
]
}
]
}
As I want to seperate record of same user, with key and value & key and object I used reduce function for this
group = cmpRecords.reduce((r, a) => {
let name = a.record.creatorName;
let record = a;
r[name] = [...r[name] || [], record];
return r;
}, {});
This responds with
"data": [
"ABC":[
{
"_id": "6098ff60a8e9ee2c7c116d6e",
"record": {
"creatorName": "ABC",
"modifierName": ""
},
"is_deleted": "0"
},
...
]
]
I tried other ways but not getting required response.
You can use Array.reduce() to group your records by name, the output here should be what you require:
const cmpRecords = { "status": "success", "message": "Record Fetched Successfully", "data": [ { "_id": "6098ff60a8e9ee2c7c116d6e", "record": { "creatorName": "ABC", "modifierName": "" }, "is_deleted": "0" }, { "_id": "6098ff60a8e9ee2c7c116d6e", "record": { "creatorName": "ABC", "modifierName": "" }, "is_deleted": "0" }, { "_id": "6098ff60a8e9ee2c7c116d6z", "record": { "creatorName": "XYZ", "modifierName": "" }, "is_deleted": "0" }, ] }
const output = {
...cmpRecords,
data: Object.values(cmpRecords.data.reduce((acc, cur) => {
acc[cur.record.creatorName] = acc[cur.record.creatorName] || { name: cur.record.creatorName, record: [] };
acc[cur.record.creatorName].record.push(cur);
return acc;
}, {}))
}
console.log('Output:', output)

Reorder an array from a specific data

I have absolutely no idea of which title I could write.
Actually, here is what I get from API :
[
{
"order": 1,
"role": {
"label": "singer"
},
"artist": {
"name": "AaRON"
}
},
{
"order": 1,
"role": {
"label": "author"
},
"artist": {
"name": "Simon Buret"
}
},
{
"order": 2,
"role": {
"label": "author"
},
"artist": {
"name": "Olivier Coursier"
}
},
{
"order": 1,
"role": {
"label": "composer"
},
"artist": {
"name": "John Doe"
}
}
]
And here is what I need to send :
"artist": {
"singer": [
"AaRON"
],
"author": [
"Simon Buret",
"Olivier Coursier"
]
}
Of course, the order property must be taken in account.
Example : Simon Buret is the first item because he has the order set to 1.
I have absolutely no idea how to implement that, I just did a map, but don't know what to put inside :/
this.artistControl.controls.map(artistControl => {
...
});
Is there a way to do what I need ?
Does this work for you:
let arr = [
{ "order": 1, "role": { "label": "singer" }, "artist": { "name": "AaRON" } },
{ "order": 1, "role": { "label": "author" }, "artist": { "name": "Simon Buret" } },
{ "order": 2, "role": { "label": "author" }, "artist": { "name": "Olivier Coursier" } },
{ "order": 1, "role": { "label": "composer" }, "artist": { "name": "John Doe" } }
];
let obj = {'artist': {}};
arr.forEach(a => {
obj['artist'][a.role.label] = obj['artist'][a.role.label] || [];
obj['artist'][a.role.label][a.order-1] = a.artist.name;
});
console.log(obj);
You could use reduce method with object as a accumulator param and then check if the key doesn't exist create it with empty array as value and then add names by order.
const data = [{"order":1,"role":{"label":"singer"},"artist":{"name":"AaRON"}},{"order":1,"role":{"label":"author"},"artist":{"name":"Simon Buret"}},{"order":2,"role":{"label":"author"},"artist":{"name":"Olivier Coursier"}},{"order":1,"role":{"label":"composer"},"artist":{"name":"John Doe"}}]
const result = data.reduce((r, {
role: { label },
artist: { name },
order
}) => {
if (name) {
if (!r[label]) r[label] = [];
r[label][order - 1] = name;
}
return r;
}, {})
console.log(result)
const array = [{"order":1,"role":{"label":"singer"},"artist":{"name":"AaRON"}},{"order":1,"role":{"label":"author"},"artist":{"name":"Simon Buret"}},{"order":2,"role":{"label":"author"},"artist":{"name":"Olivier Coursier"}},{"order":1,"role":{"label":"composer"},"artist":{"name":"John Doe"}}];
const result = array
.sort((item1, item2) => item1.order - item2.order)
.reduce((acc, { role, artist }) => ({
...acc,
artist: {
...acc.artist,
[role.label]: [
...(acc.artist[role.label] || []),
artist.name,
],
},
}), { artist: {} });
console.log(result);
Here is another approach with es5
const data = [{ "order": 1, "role": { "label": "singer" }, "artist": { "name": "AaRON" } }, { "order": 1, "role": { "label": "author" }, "artist": { "name": "Simon Buret" } }, { "order": 2, "role": { "label": "author" }, "artist": { "name": "Olivier Coursier" } }, { "order": 1, "role": { "label": "composer" }, "artist": { "name": "John Doe" } }];
var result = data.reduce(function(map, obj) {
map["artist"] = map["artist"] || {};
if (obj.role.label === 'author' || obj.role.label === 'singer') {
map["artist"][obj.role.label] = map["artist"][obj.role.label] || [];
map["artist"][obj.role.label][obj.order - 1] = obj.artist.name;
}
return map;
}, {});
console.log(result)

Unexpected data sorting result after filtering from parent and child node

I have list of tree node metadataList Like below:
[
{
"data": {
"metadata": {
"category": [
"Csp"
]
}
},
"children": [
{
"data": {
"metadata": {
"category": [
"Csp"
]
}
},
"children": [
]
},
{
"data": {
"metadata": {
"category": [
"Mpn"
]
}
},
"children": [
]
},
{
"data": {
"metadata": {
"category": [
"Mpn"
]
}
},
"children": [
]
},
{
"data": {
"metadata": {
"category": [
"Mpn"
]
}
},
"children": [
]
}
]
},
{
"data": {
"metadata": {
"category": [
"Isv"
]
}
},
"children": [
{
"data": {
"metadata": {
"category": [
"Isv"
]
}
},
"children": [
]
},
{
"data": {
"metadata": {
"category": [
"Isv"
]
}
},
"children": [
]
}
]
},
{
"data": {
"metadata": {
"category": [
"Csp"
]
}
},
"children": [
{
"data": {
"metadata": {
"category": [
"Csp"
]
}
},
"children": [
]
}
]
},
{
"data": {
"metadata": {
"category": [
"Mpn"
]
}
},
"children": [
{
"data": {
"metadata": {
"category": [
"Mpn"
]
}
},
"children": [
]
},
{
"data": {
"metadata": {
"category": [
"Mpn"
]
}
},
"children": [
]
},
{
"data": {
"metadata": {
"category": [
"Mpn"
]
}
},
"children": [
]
},
{
"data": {
"metadata": {
"category": [
"Csp"
]
}
},
"children": [
]
},
{
"data": {
"metadata": {
"category": [
"Isv"
]
}
},
"children": [
]
}
]
},
{
"data": {
"metadata": {
"category": [
"Incentives"
]
}
},
"children": [
{
"data": {
"metadata": {
"category": [
"Incentives"
]
}
},
"children": [
]
}
]
}
]
Which is a type of array of data and children collection it's class like below:
export default class CurrentTopicMetadataTreeNode {
public data: CurrentTopicMetadata;
public children: CurrentTopicMetadataTreeNode[];
}
export default class CurrentTopicMetadata {
public id: string;
public metadata: TopicMetadata
}
export class TopicMetadata {
public category: Category[]
}
export enum Category {
Csp = 'Csp',
Mpn = 'Mpn',
Incentives = 'Incentives',
Referrals = 'Referrals',
Isv = 'Isv',
}
What I am trying, to filter list as data and children order as per category. Let say if filter by a category all data and children belongs to that category should come like below order.
But I am getting data like this order :
One Element On Array Problem Set:
Here in this array if I search with Csp Only data in root node which is Csp and data in children only has one data which contains Csp would be in array.
[{
"data": {
"metadata": {
"category": [
"Csp"
]
}
},
"children": [
{
"data": {
"metadata": {
"category": [
"Csp"
]
}
},
"children": [
]
},
{
"data": {
"metadata": {
"category": [
"Mpn"
]
}
},
"children": [
]
},
{
"data": {
"metadata": {
"category": [
"Mpn"
]
}
},
"children": [
]
},
{
"data": {
"metadata": {
"category": [
"Mpn"
]
}
},
"children": [
]
}
]
}]
Expected Output: So after filtered by Csp node should be look like this:
[
{
"data": {
"metadata": {
"category": [
"Csp"
]
}
},
"children": [
{
"data": {
"metadata": {
"category": [
"Csp"
]
}
},
"children": [
]
}
]
}
]
here is my code, where I am doing wrong?
// Rule 1 check parent metadata category whether be empty
// Rule 2 and 3
function find_in_children(children, parent_category) {
children_has_same_category = []
for(var i in children) {
let child = children[i];
if(child.children != undefined && child.children.length > 0 && child.data.metadata.category == parent_category) {
children_has_same_category.push(child);
}
}
if(children_has_same_category.length > 0) {
return children_has_same_category
} else {
for(var i in children) {
let child = children[i];
return find_in_children(child.children, parent_category);
}
}
}
function check_object(object) {
let parent_category = object.data.metadata.category[0];
if(object.children != undefined && object.children.length > 0) {
return {'data': object.data, 'children': find_in_children(object.children, parent_category)}
} else {
return {'data': object.data}
}
}
function apply_rules(object) {
// Rule 1 check parent metadata category whether be empty
if(object.data.metadata.category.length > 0) {
return {'data': object.data}
} else {
return check_object(object)
}
}
target = {
value: 'Isv'
}
filtered_datas = []
for(var i in datas) {
let data = datas[i];
if(data.data.metadata.category.length > 0) {
result = apply_rules(data)
if(result.data.metadata.category[0] == target.value) {
filtered_datas.push(result);
}
}
}
Here is the data sample and result: https://jsfiddle.net/faridkiron/b02cksL8/#&togetherjs=F7FK3fBULx
I have resolve above problem like below way:
const findMatchedChild = (nodeInList, type) => {
const node = Object.assign({}, nodeInList)
node.children = nodeInList.children
.filter(child => child.data.metadata.category.includes(type))
.map(child => findMatchedChild(child, type))
return node
}
const cspList = findMatchedChild({ children: data }, 'Csp').children
console.log(JSON.stringify(cspList, null, 2));
Got the expected result.

Loop through nested Json javascript

I'm trying to loop through all the conditions for a form logic system utilizing the json-logic-js library and having issues going into the infinite nested conditions. It works when there is no additional nested operations but can't figure out away to loop through and detect if the condition contains additional conditions infinitely..
What I'm needing is this output
{
"or": [
{
"==": [
"0hxsj03jab9pjsu1",
"Rig 15"
]
},
{
"or": [
{
"==": [
"5l12cnsnxe1911bm",
"Corporate"
]
},
{
"==": [
"5l12cnsnxe1911bm",
"Pumping"
]
}
]
},
{
"and": [
{
"==": [
"69vsm5bkfb101n2n",
"Unsafe"
]
},
{
"or": [
{
"==": [
"b09ivo9r1aldu6jf",
"Yes"
]
},
{
"==": [
"0hxsj03jab9pjsu1",
"Rig 44"
]
},
{
"==": [
"0hxsj03jab9pjsu1",
"Other"
]
}
]
}
]
}
]
}
I currently have this much.
var item = {
"id":"8wj4xhwe3pd9aage",
"showif": {
"operator": "or",
"conditions": [
{
"operator": "and",
"conditions": [
{
"data": {
"fieldId": "69vsm5bkfb101n2n",
"settings": {
"values": [
"Unsafe"
]
}
}
},
{
"operator": "or",
"conditions": [
{
"data": {
"fieldId": "b09ivo9r1aldu6jf",
"settings": {
"values": [
"Yes"
]
}
}
},
{
"data": {
"fieldId": "0hxsj03jab9pjsu1",
"settings": {
"values": [
"Rig 44",
"Other"
]
}
}
}
]
}
]
},
{
"data": {
"fieldId": "0hxsj03jab9pjsu1",
"settings": {
"values": [
"Rig 15"
]
}
}
},
{
"data": {
"fieldId": "5l12cnsnxe1911bm",
"settings": {
"values": [
"Corporate",
"Pumping"
]
}
}
}
]
}
};
var condition = [];
item.showif.conditions.forEach(element => {
var orC = []
if(element.data){
element.data.settings.values.forEach(values => {
if(element.data.settings.values.length <= 1){
condition.push({"==": [element.data.fieldId,values]})
}else{
orC.push({"==": [element.data.fieldId,values]})
}
});
if(orC.length >= 1){
condition.push({"or":orC})
}
}
});
var Logic = {[item.showif.operator]: condition}
console.log(Logic)
I cant wrap my brain around this without getting lost..
I figured it out. For anyone else with this here is my code :)
var item = {
"id": "8wj4xhwe3pd9aage",
"showif": {
"operator": "or",
"conditions": [
{
"operator": "and",
"conditions": [
{
"data": {
"fieldId": "69vsm5bkfb101n2n",
"settings": {
"values": [
"Unsafe"
]
}
}
},
{
"operator": "or",
"conditions": [
{
"data": {
"fieldId": "b09ivo9r1aldu6jf",
"settings": {
"values": [
"Yes"
]
}
}
},
{
"data": {
"fieldId": "0hxsj03jab9pjsu1",
"settings": {
"values": [
"Rig 44",
"Other"
]
}
}
}
]
}
]
},
{
"data": {
"fieldId": "0hxsj03jab9pjsu1",
"settings": {
"values": [
"Rig 15"
]
}
}
},
{
"data": {
"fieldId": "5l12cnsnxe1911bm",
"settings": {
"values": [
"Corporate",
"Pumping"
]
}
}
}
]
}
};
function loopThrough(obj) {
var operator = obj.operator
var condition = [];
obj.conditions.forEach(element => {
var orC = []
if (element.data) {
element.data.settings.values.forEach(values => {
if (element.data.settings.values.length <= 1) {
condition.push({
"==": [element.data.fieldId, values]
})
} else {
orC.push({
"==": [element.data.fieldId, values]
})
}
});
if (orC.length >= 1) {
condition.push({
"or": orC
})
}
} else if (element.operator) {
condition.push(this.loopThrough(element))
}
});
return ({
[operator]: condition
})
}
console.log(loopThrough(item.showif));

How to query a json file?

Here is the file I would like to parse
I receive a file from a webservice in JSON format.
I would like to parse the content in such a way that I display the name of the president from the USA
{
"response": {
"result": {
"Countries": {
"row": [
{
"no": "1",
"FL": [
{
"content": "USA",
"val": "Country"
},
{
"content": "Barack Obama",
"val": "President"
}
]
},
{
"no": "2",
"FL": [
{
"content": "Cuba",
"val": "Country"
},
{
"content": "Raul Castro",
"val": "President"
}
]
}
]
}
}
}
}
The expected output
{ presidents: [
{ "name": "Barack Obama"}
]
}
could you provide a solution using a kind of JSON XPath?
Assuming that you are loading the response into a variable data:
var data = {
"response" : {
"result" : {
"Countries" : {
"row" : [{
"no" : "1",
"FL" : [{
"content" : "USA",
"val" : "Country"
}, {
"content" : "Barack Obama",
"val" : "President"
}
]
}, {
"no" : "2",
"FL" : [{
"content" : "Cuba",
"val" : "Country"
}, {
"content" : "Raul Castro",
"val" : "President"
}
]
}
]
}
}
}
};
You can then filter your data like this:
data.response.result.Countries.row.filter(function (el) {
return (el.FL[0].content == "USA");
})[0].FL[1];
To get to:
{
"content" : "Barack Obama",
"val" : "President"
}
To get the name, simply specify "content"
data.response.result.Countries.row.filter(function(el){
return (el.FL[0].content == "USA");
})[0].FL[1].content;
EDIT 1
One could search a json object like a string.
If we know that the element will have no children, then we could use something like this:
function find(query,obj) {
var str = JSON.stringify(obj);
var start = str.substr(0,str.indexOf(query)).lastIndexOf('{');
var end = str.substr(start,str.length).indexOf('}');
return str.substr(start,end);
}
console.log(find('"content":"USA"',data))
Despite of the age of the question I want to add this answer as reference for future visitors with the same problem:
You can use JSONPath. The page contains a description and an implementation in JavaScript and PHP.
t = {
"response": {
"result": {
"Countries": {
"row": [
{
"no": "1",
"FL": [
{
"content": "USA",
"val": "Country"
},
{
"content": "Barack Obama",
"val": "President"
}
]
},
{
"no": "2",
"FL": [
{
"content": "Cuba",
"val": "Country"
},
{
"content": "Raul Castro",
"val": "President"
}
]
}
]
}
}
}
}
res={};//Here we will store result
for (i in t.response.result.Countries.row) {
// get current country
country = t.response.result.Countries.row[i].FL[0].content;
// get current president
president = t.response.result.Countries.row[i].FL[1].content;
if (country == 'USA') {
res.presidents=[{name:president}];
break;
}
}

Categories