You need to be very clear on your terminology and concepts.
A JavaScript object is a native JavaScript type which contains key-value pairs, and has the literal form enclosed in curly brackets such as {a: 1}
.
JSON is a string-based representation loosely based on JavaScript literal object syntax, such as the string '{"a": 1}'
.
A JSON file is a file (a physical file, with a filename, in the local file system) containing JSON. In node, it may be read in and converted to a JavaScript object using require('foo.json')
.
A variable is a way to hold any JavaScript type. That could be a JavaScript object, or it could be a string, and that string could be JSON.
A parameter is the part of a function definition which represents something that will be passed in ("formal parameter"), or passed to the function when called.
So
other developer ask me to send some function a json file
I'm not really sure that other developers have asked you to send a "JSON file". If that's really what they meant, you could pass a filename. If you show us the function the developer has asked you pass a "JSON file" to, we would be able to figure out what they really meant.
or a variable that represent json file?
It's more likely that what they asked you to send as a parameter (not a "variable") is either (1) a JavaScript object or, less likely, (2) a JSON string. If they asked you to send a JavaScript object, and you want to send a JavaScript object corresponding to the contents of a JSON file, then you can read in the JSON file with require('foo.json')
, and that will read in the file and at the same time convert to a JavaScript object, which you can then just pass.
If they asked you to send "JSON", it's likely they are using incorrect terminology, and actually mean for you to send a JavaScript object, in which case see above.If they really meant "JSON" (meaning a string), then if you have the JSON string already, you could pass that as is; or if you have a JavaScript object, and need to pass it as JSON (for instance, to pass it to an AJAX-like interface), then you could convert the JavaScript object into JSON using JSON.stringify
.
In your particular case, you are reading a JSON file and creating a JavaScript object obj1
. Then you are creating another JavaScript object using literal object notation obj2
. If you want to compare those for deep equality, you can't use ==
or ===
, which compares object identity, and these two objects have distinct identities even if they have the same key/value pairs, so you'll have to use a library which offers deep equality checking, or write your own.
it's about representing json file in var.
I don't know what you mean by saying to "represent" a JSON file in a variable. What you can do, to repeat myself, is to read in the JSON file using require('foo.json')
and it will return a JavaScript object you can assign to a variable; or, you can read in the JSON file as text, using standard node file I/O APIs, then convert it a JavaScript object yourself using JSON.parse
.
I need to implement a function in Node js that get json string as parameter, store this json string in a json file and send this json file to other function, how am I supposed to do that?
I guess you mean "takes a JSON string as parameter". The most likely interpretation of "send this json file to other function" is actually "pass the corresponding JavaScript object to other function". If that's what you want to do, then
function takeJsonAndSendObject(json) {
otherFunction(JSON.parse(json));
}
If for some reason you really want to "store this json string in a json file", meaning a physical file on your hard disk, then you'll have to use node's file I/O API's to write the physical file. Then you could pass the name of the file to the other function. But why would you do that when you already have the JSON and/or JavaScript object and could pass it directly?