Substitute Symbolic Codes in an Object with Written Text

Here's the structure of the object:

{ where: { [Symbol(or)]: [ [Object], [Object] ] },hooks: true, rejectOnEmpty: false }

When I use JSON.stringify on it, the result is:

{"where":{},"hooks":true,"rejectOnEmpty":false}

This happens because [Symbol(or)] is undefined and gets removed by stringify.

The value originates from Sequelize operators, particularly Op.or. Is there a way to make stringify convert this into a string, so the output would be:

{"where":{"[Symbol(or)]": [[<<stringifiedObject>>], [<<stringifiedObject>>]]},"hooks":true,"rejectOnEmpty":false}

I am aware that I could use a custom function with JSON.stringify to replace undefined with a placeholder, but I prefer to keep the original Symbol in the string to differentiate between Symbol(and) and Symbol(or), even if both are undefined.

Answer №1

Resolved using the following function:

const clean = (data) => {
    try {
        const keys = Reflect.ownKeys(data);
        const result = {};
        keys.forEach((k) => {
            let value = data[k];
            if (Object.prototype.toString.call(value) === '[object Object]') {
                value = clean(value);
            }
            if (typeof k === 'symbol') {
                const newKey = `${String(k)}`;
                result[newKey] = value;
            } else {
                result[k] = value;
            }
        });
        return result;
    } catch (error) {
        console.log(error);
    }
};

Reflect.ownKeys fetches all keys, including Symbols.

Then, I validate if a key is a Symbol and convert it to a string. Recursive function calls are made to apply the same procedure to nested objects.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to utilize the WebGL camera in order to create dynamic transitions between various polygons?

Recently, a friend suggested exploring WebGL as an alternative to CSS transitions. I have a collection of polygons that form a 2D board game. https://i.sstatic.net/D0dnc.png In essence, the application moves the player space by space starting at the top ...

What is the best way to remove table row data fetched from an API within a table?

Can someone assist me with deleting the table tr from the user table, where data is retrieved from an API? I have attempted the code below: $("principleTable").find("tr").hide(); $("#delAgentModal").modal("hide"); ...

The JSON appears to be fine, but encountering an error during parsing

So I've been trying to make sense of this JSON data in Ruby, but every time I attempt to parse it on platforms like , I keep encountering an error that I just can't seem to crack. Here's the JSON data I'm dealing with: "{\"RED&b ...

Creating a CSS triangle that smoothly transitions between two different colors

Is it possible to create a triangle in CSS that smoothly transitions between two colors without relying on a hover state? .arrow-down { width: 0; height: 0; border-left: 20px solid transparent; border-right: 20px solid transparent; b ...

How can a new <li> element be created without being influenced by certain CSS styles?

I am attempting to enhance my menubar by adding a signup/login item aligned on the right without affecting the other center-aligned items. Essentially, I have an entry named "Login/Sign Up" that should maintain its own unique style while seamlessly blendin ...

Remove click event listeners from a specific element within a group of selected elements using D3

In my D3 class, I have a group of selectors that I want to remove the click event from: d3.selectAll('.selectors').on('click',function(){ //Remove the click event from the currently clicked element. }); I'm encountering tw ...

What is the best way to manage the changing selection in a drop-down list?

Can someone help me with a coding issue I am facing? <select name="txtTK" > <option value="None">---</option> <option value="Mat">Materials</option> <option value="Cate">Category</option> <option ...

Using the https module in Node.js to transfer a file to a PHP server

What is the best method to send an HTTP post request that includes a jpg file to a php server using the node https module? I attempted to use the request module, but it is unreliable (timing out most of the time) and already deprecated. Here is the functi ...

Unexpected errors causing havoc in my internet browser

I am facing difficulties uploading large files (~ 2 GB) on my server. To prevent crashes caused by huge files, I have removed the bodyParser from Express. However, the crash error occurs randomly, making it challenging to pinpoint the exact cause. The cod ...

extracting attributes from JSON using Python

When making a call to a URL that returns a JSON object with an attribute labeled from, I attempted to access it in Python using the following code: object.from.username $ python sample_app.py File "sample_app.py", line 63 print comment.from.usern ...

When delving into an object to filter it in Angular 11, results may vary as sometimes it functions correctly while other times

Currently, I am working on implementing a friend logic within my codebase. For instance, two users should be able to become friends with each other. User 1 sends a friend request to User 2 and once accepted, User 2 is notified that someone has added them a ...

Adjusting the background color of <tr> depending on the number of <tr> elements in the HTML using CSS

When working on my HTML page, I have utilized multiple <tr> tags with different bgcolor values such as: <tr bgcolor="#000000"> <tr bgcolor="#E5F1CC"> <tr bgcolor="#D30A0A"> <tr bgcolor="#656766"> I am aiming to assign unique ...

Switch the paper tab to a dropdown menu in Polymer.js

Can someone assist me in transforming the paper tab into a paper drop down menu in polymer JS? I want the drop-down to appear with a list of values when hovering over the Top menu. Activity Execution <paper-tab cla ...

Rounding up the cents area using JavaScript

So, imagine this scenario: I'm inputting a dollar amount into a text field using code similar to this: <input type="text" name="qtr-revenue-<?php echo $qtr ?>" id="qtr-revenue-<?php echo $qtr ?>" class=&quo ...

Pass the ActiveRecord object retrieved in the success of the ajax call in Rails as a parameter to another subsequent

Imagine this scenario: I have a view named "Dashboard" featuring a basic form and a button labeled "Draw Graphs". The goal is to trigger two ajax requests upon clicking the button, fetching the required data from the database for creating two distinct grap ...

How can users create on-click buttons to activate zoom in and zoom out features in a Plotly chart?

I am currently working on an Angular application where I need to implement zoom in and zoom out functionality for a Plotly chart. While the default hoverable mode bar provides this feature, it is not suitable for our specific use case. We require user-cr ...

The JavaScript file specified in the script tag's src attribute was successfully downloaded, but it did not execute as expected after being fetched

I've implemented an ajax call using jQuery in the following manner: $.ajax({ type: 'GET', url: 'edit.htm', success: function(data){ container.html(data); }, }); After making the ajax call, the data returned includes s ...

Verify if a username is available using jQuery

When trying to register, I use this function to check the availability of a username. If the username already exists, the submit button is disabled. The check is done using a php script that queries the database for existing usernames. Everything works per ...

The database cursor is executing the query, but an OperationalError is being raised due to a missing column in the api_ingredient.meal_id

Seeking assistance in saving data from a menu.json file to my SQL database using Django Rest Framework API. Can someone review my database model for any issues? Additionally, I am encountering an error which I have detailed below. models.py class Meal(mod ...

Running jQuery scripts through PHP

$("#bt-potrdi").click( function(e) { e.stopPropagation(); $("#belina").css({"z-index":200}); $("body").addClass("ext"); $("#vpisok_frame").css({"z-index":250}).fadeIn(200); }); Upon clicking the button, the ...