0

I have a script that generates a JSON formatted string. I want to use the data contained in this string in another script. I'm not sure where to even start. I've thought about running both scripts from the same directory and somehow outputting the JSON data into a text file and then loading it into the second script but this seems like more of a workaround than an actual solution. Is it possible to somehow call the second script from the first and pass it the data? Much like passing data between functions in a single script?

FWIW I have tried simply combining the functions the two scripts perform into one, but this has caused me countless headaches with no progress. For simplicity sake I'd prefer to keep the functions performed by each script separate (apart from the obvious data sharing requirement!).

Any advice or a point in the right direction would be greatly appreciated.

1
  • You could use ES6 Import/Export Commented Aug 26, 2017 at 14:12

3 Answers 3

3

If JSON data is less than 5 mb then you can use localStorage to save the output in browser.

In first js file:

function fOne(){
    var jsonStr = ''; //insert some json data here
    localStorage.setItem("myJson", JSON.stringify(jsonStr)); //save data
}

In your second js file:

function fTwo(){
    if (localStorage.getItem("myJson") !== null) {
        var passedJson = localStorage.getItem("myJson"); //get saved data anytime
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

My apologies @TusharTyagi, I should have mentioned I am using node.js and not programming for browser work. I believe the above will work in a browser environment only?
Alright then, because of the tags used in your question, more ppl may get confused so you must edit it asap :)
I really like this. No faffing about with global variables. One small improvement I would make is to use sessionStorage instead, unless the data has to persist between sessions, this is the safest way of storing data temporarily.
0

It is hard to say without some code to reference but maybe just a global variable with a check if its null.

var myJsonString = null;

function one () {
    var jsonString = "[]";
    myJsonString = jsonString;
}

function two () {
    //string not set so bail
    if (myJsonString === null) { return; }
}

Comments

0

So it actually depends what environment you are programming in. If its a web browser, all of the code is in the global space actually and is run in order. so if you have <script src="file1"> then <script src="file2"> in your html file, and you put x = 10 in the first file and use it in the second, you will have no problems. If you are using node.js, I can post a solution for that as well, just let me know.

(btw if you put x = 10 in the second file and reference it from the first it will throw an error)

There are ways of avoiding this, such as using a document.ready function or wrapping things in functions and then calling them at the end. You need to make sure that functions and variables are created before being used (this is a common mistake beginners make and are confused by)

3 Comments

Sorry, new to Javascript so I should have mentioned I am indeed using node.js and not writing code for use on websites.
I have found this after refining my search terms based on your response. I'll give what I see there a try. Thankfully my two scripts do not try to alter either scripts data - one creates, the other reads. One way only :)
oh no problem. that changes a lot. let me know if you need more help. good luck!

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.