0

Node 12

I have the following JSON string (used as filters for database searches)

  {"$and":[{"txnType":{"$in":["Cash Expense"]}}]}

I want that ugly string to look like this so I can display it to the front end:

 txnType="Cash Expense"

Here is my method:

    function prettyupFilters(uglyFilters){
        let filters
        filters = uglyFilters
        .replace(/{/g,'').replace(/}/g,'')
        .replace('$$and','').replace('$$in', '=')
        .replace(/[/g,'').replace(/]/g,'')
        .replace(/:/g,'').replace(/:/g,'')
        return filters
    }

But, this produces:

 "$nd"["txnTy""$in"["Csh Exns"]]

How do i tweak my code to replace characters in my string to achieve my desired output? Thanks

BoundForGlory
  • 4,114
  • 15
  • 54
  • 81
  • Any reason you're trying to parse JSON with regex rather than `JSON.parse`? Parsing structured data with regexes is generally [doomed to failure](https://stackoverflow.com/a/1732454)... – Lionel Rowe Sep 08 '20 at 15:27
  • Why don't you transform it into JSON? (`JSON.parse`) – SourceOverflow Sep 08 '20 at 15:27

2 Answers2

5

You should parse the string to an object like this:

JSON.parse("...")

You can then extract the values you need

const obj = JSON.parse(`{"$and":[{"txnType":{"$in":["Cash Expense"]}}]}`)
console.log(`txnType="${obj.$and[0].txnType.$in[0]}"`)
Tracer69
  • 1,050
  • 1
  • 11
  • 26
0

You can use object destructuring to cover all your basis, and grow the program to handle other cases of different tags rather than making it static to the indexes you currently have

const jsonStr = '{"$and":[{"txnType":{"$in":["Cash Expense"]}}]}';

const jsonObj = JSON.parse(jsonStr);

const { $and: andArr = [] } = jsonObj;

let cleanStr = ""
if(andArr.length) {
   const [{ txnType = {} }] = andArr;
   const { $in: inArr = [] } = txnType;
   if(inArr.length) {
      [cleanStr] = inArr;
   }
}

console.log(cleanStr);
Edward Romero
  • 2,905
  • 1
  • 5
  • 17
  • Your answer looks promising..im testing it now. – BoundForGlory Sep 08 '20 at 15:43
  • 1
    @Wyck it won't throw an error if you're destructuring from an empty object. It will just give you undefined. If you're destructuring from an array that's empty, then you will get undefined as well. Also you can easily add initializers to make sure that you are not dealing with undefined. That's where you see me doing things like `const { $in: inArr = [] } = txnType;`. This basically sets inArray as an empty array if the property $in doesn't exist in txnType object. You start dealing with exception handling if you are trying to destructure from undefined vars. – Edward Romero Sep 08 '20 at 15:44
  • @EdwardRomero my bad. You're right because you used an inline default for the property. Thanks for clarifying. – Wyck Sep 08 '20 at 15:57
  • @BoundForGlory does it satisfy your question? Or do you have follow up questions about it? – Edward Romero Sep 08 '20 at 17:07
  • @EdwardRomero - ive written my method. i didnt use your answer, but the one i accepted needed tweaking for my purpose. I'll still up vote your answer. Thanks. – BoundForGlory Sep 10 '20 at 19:04