Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Javascript convert Json to an array string format

I would like to convert json to a string "filter" format and visa versa.

Json Format

{
 "condition": "and",
 "rules": [
 {
 "field": "Name",
 "operator": "=",
 "value": "Jack"
 },
 {
 "field": "UserId",
 "operator": "=",
 "value": 2
 },
 {
 "field": "Surname",
 "operator": "=",
 "value": "Rose"
 },
 {
 "condition": "or",
 "rules": [
 {
 "field": "Code",
 "operator": "=",
 "value": "X"
 },
 {
 "field": "Type",
 "operator": "=",
 "value": "Z"
 }
 ]
 }
 ]
};

String Filter Format

[
 ["Name","=", "Jack"],
 ["and"],
 ["UserId", "=", 2],
 ["and"],
 ["Surname","=", "Rose"]
,
["and"],
(
["Code","=","X"],
["or"],
["Type","=","Z"]
)
]

At the moment I have the following js code which outputs something that is halfway there, but I'm missing the "and" and "or" conditions which need to come between the respective fields as well as the "(" ")" brackets around to group the fields.

I need to be able to convert from json to the "filter" format and back again.

This is the code I have so far: query in this case is the json I posted above.

const query={condition:"and",rules:[{field:"Name",operator:"=",value:"Jack"},{field:"UserId",operator:"=",value:2},{field:"Surname",operator:"=",value:"Rose"},{condition:"or",rules:[{field:"Code",operator:"=",value:"X"},{field:"Type",operator:"=",value:"Z"}]}]};
const parseRule = (rule) => {
 
 if (rule.rules) {
 return rule.rules.map(r=> parseRule(r));
 }
 
 return [rule.field, rule.operator, rule.value];
};
let filterFormat = parseRule(query);
console.log(filterFormat);

I'm stuck here. I almost need something like a join for arrays where the condition can be added as an array between the other arrays, without losing the arrays like you would with a normal join.

Answer*

Draft saved
Draft discarded
Cancel
4
  • The only thing missing for this solution is the parenthesis around the "or". The above solution outputs it as [["Code","=","X"],["or"],["Type","=","Z"]] where the final result should be (["Code","=","X"],["or"],["Type","=","Z"]). Other than that this solution solves all the other issues. Commented Jul 15, 2021 at 8:39
  • 1
    I added a quick custom toString function to use () around conditions. To be consistent however, I think you also need the () around the outermost part of the filter. In your example, that part has []. Commented Jul 15, 2021 at 9:04
  • what does that toString look like? Commented Jul 15, 2021 at 13:00
  • 1
    Woops, I actually did not press "Save edits" 🤦‍♂️ It's included now (in the code snippet) Commented Jul 15, 2021 at 13:08

lang-js

AltStyle によって変換されたページ (->オリジナル) /