-1

I'm editing my post because i want to clarify the meaning of a post, the real question is not about the code i wrote or about equality operator,it's about representing json file in var. I'll try to ask this again in different way, 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?


other developer ask me to send some function a json file, or a variable that represent json file? how am I supposed to do that? can i represent file in variable in node js or just hold json obj?

var ob1 = require('./test.json');
var ob2 = {"a" : "b"};

var b = (ob1 === ob2)

test.json of course include exactly the same json as in ob2 object. when i debug this code in intellij it seems that ob1 and ob2 are exactly the same. both object with same content.

so why do 'b' value is false?

thanks.

masterG
  • 168
  • 3
  • 10
  • 3
    Possible duplicate of [How to determine equality for two JavaScript objects?](http://stackoverflow.com/questions/201183/how-to-determine-equality-for-two-javascript-objects) – 4castle May 21 '16 at 02:12
  • An object with the same contents is not necessarily the same object. `==` and `===` compare references, not values. – This company is turning evil. May 21 '16 at 02:13
  • Using `require` is the way to do it in node.js. If you want to see the contents of a JSON variable. Try outputting `JSON.stringify(ob1)`. – 4castle May 21 '16 at 02:19
  • @4castle - so lets say i need to implement a function that : get json string as parameter and store that json in a file and send this file to another functions ? what should i do? , and it's not duplicate questions like you comment, my real question are the questions i wrote in the start of my post, this question was not about equality operator, but how to store json file in var. – masterG May 21 '16 at 02:26

4 Answers4

2

You need to be very clear on your terminology and concepts.

  1. 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}.

  2. JSON is a string-based representation loosely based on JavaScript literal object syntax, such as the string '{"a": 1}'.

  3. 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').

  4. 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.

  5. 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?

0

Both objects ob1 and ob2 may have the same content, but they are still two different objects.

obj1 === obj2 does not compare the objects' contents, but their references. These references are not the same, so the comparison has a negative result.

You will have to implement the comparison of the objects' contents yourself, e.g.:

ob1.a === ob2.a
TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
0

There isn't really a "file" object in Node, but you can send the path to the JSON file as a parameter. Or, even better, send the object itself as a parameter.

4castle
  • 32,613
  • 11
  • 69
  • 106
0

There is no file object in NodeJS. If you want to pass a file to another function, pass the path to the file to the function that needs the file. Then from that function, read the file contents using fs module.

Below function accepts a json object, writes it in to a file & returns the path of the file.

function createJSONFile(jsonObject) {
  var jsonPath = './jsonFileName.json';
  var fs = require('fs');
  fs.writeFileSync(jsonPath, JSON.stringify(jsonObject));
  return jsonPath;
}

Also, when you read the json data from the file, though the content will be the same, you cannot expect the object that you saved in the json file is same as the one you read. Because, they are two different objects now.

var object = {name: 'test'};
var filePath = createJSONFile(object);
var json = require(filePath);

object === json // false

//to check the equality of the content, you need to use `JSON.stringify`.
JSON.stringify(object) === JSON.stringify(json) // true
  • Using `stringify` to compare the value of 2 objects is not ideal & will be a performance hit when the objects are big. –  May 21 '16 at 05:12