0

I was wondering how can I extract the JSON data from a javascript file. The javascript is intended to be used as a config file and contains one variable with JSON data in it. This is similar to the require-config.js file used in Magento 2, just for reference. It looks something like this:

var config = {
    fieldsets : [
        {
            title :  'Quote Essentials',
            description : 'Some',
            fields : [
                {
                    label : 'What type of project is this for?',
                    required : true,
                    class : '',
                    type : 'input',
                    inputType : 'text',
                    hint : 'For example: company uniforms, clothing line, school events, etc.',
                    id : ''
                },
                {
                    label : 'How many total items do you need?',
                    required : true,
                    class : '',
                    type : 'input',
                    inputType : 'text',
                    hint : 'Please note: the minimum order size is 24 pieces.',
                    id : ''
                },
...
12
  • 1
    This isn’t JSON. Commented Aug 24, 2017 at 13:33
  • I know , it is a javascript file that has a variable that contains json data. Commented Aug 24, 2017 at 13:34
  • Where do you need to extract that? In browser? On server? Commented Aug 24, 2017 at 13:35
  • No, it's a JS object. There's no JSON anywhere there. Commented Aug 24, 2017 at 13:35
  • @Andy yes, true. My bad for saying is json. and I have a client side script that would read this file and parse the javascript object Commented Aug 24, 2017 at 13:37

1 Answer 1

1

If you're accessing this server-side you can export the config

module.exports = {
  fieldset: [ ... ]
}

and require it

const config = require('./config.js');

If you're trying to access it on the client-side, just place the config script before the scripts that access it and you'll be able to access it like you would any other object:

config.fieldset...

One problem with this is that you're adding the config variable directly to window and by doing this you might be over-writing an existing config variable. Probably unlikely, but a way to mitigate this is to provide a namespace for your code so you don't pollute the global namespace and the code becomes more modularised. WRT to your config file, your namespace technique might work like this:

// Semi-colon to prevent minify issues
// Pass `window` into the immediately-invoked function expression
// as an argument
;(function (window) {

   // config is local to this function and can't leak    
   var config = { ... };

   // If the `app` variable isn't available, create it
   window.app = window.app || {};

   // Add config to the app namespace
   window.app.config = config;

})();

And you can do something similar to the rest of your code.

You would access the config with app.config.fieldset....

Hope that helps.

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

Comments

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.