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.

Mattaus
  • 41
  • 6

3 Answers3

2

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
    }
}
Tushar Tyagi
  • 307
  • 1
  • 12
  • 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? – Mattaus Aug 26 '17 at 22:50
  • Alright then, because of the tags used in your question, more ppl may get confused so you must edit it asap :) – Tushar Tyagi Aug 27 '17 at 02:49
  • 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. – Florestan Korp Aug 07 '20 at 11:16
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; }
}
fjoe
  • 1,160
  • 6
  • 15
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)

Luple
  • 433
  • 3
  • 16
  • Sorry, new to Javascript so I should have mentioned I am indeed using node.js and not writing code for use on websites. – Mattaus Aug 26 '17 at 22:44
  • [I have found this](https://stackoverflow.com/questions/17120117/sharing-modifying-a-variable-between-multiple-files-node-js) 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 :) – Mattaus Aug 26 '17 at 23:00
  • oh no problem. that changes a lot. let me know if you need more help. good luck! – Luple Aug 27 '17 at 02:24