0

on my current project I have a variables.js which contains all variables like theme colors.

I want to put this complete variables.js into a Database but its rly long, and I would prefer to put it into the database as a string to keep it easy for upcomming changes.

Is there any way, to make that this string which looks like the example down below get converted into a object that would work exactly like that down below.

Here a example whats inside the variables.js

export default {
    faq_size                          : 5,
    debug_show_php_errors             : true,
    debug_disable_captcha             : false,
    discord_color                     : "#7289DA",
    theme                             : {primary: "deep-purple darken-4", secondary: "deep-purple accent-1",},
    currencies                        : ["€", "$", "₽", "£", "¥"],
    languages                         : [
        {
            lang   : 'en',
            code   : 'US',
            country: 'English',
        },
        {
            lang   : 'de',
            code   : 'DE',
            country: 'Deutsch',
        }
    ],
}
2
  • Have you tried JSON.stringify() yet? Commented Dec 11, 2020 at 11:30
  • Put JSON in your database, not text. Most databases do support it. Commented Dec 11, 2020 at 11:57

1 Answer 1

1
import data from 'variables.js'

// convert data to string 
let dataInStringFormat = JSON.stringify(data);
// now you can save this in any database you want.

// convert string to object again
let data = JSON.parse(dataInStringFormat);

Sign up to request clarification or add additional context in comments.

3 Comments

I'll also thought about that but with that way, but the code is still in json format when I'm putting it into a text field to edit it. Do you also know there a solution how I can make that it looks like a normal code and when applying it it looks then like the json?
you can save it to a NoSQL database that preserves its type. You can use MongoDB for it.
Ok I found a solution for that what I'm looking for. I've had to use more parameters on stringify JSON.stringify(data, null, 2);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.