Node reads and writes JSON, formats output JSON

Source: Internet
Author: User
Tags diff readfile

Today is in the 5th day of deep conviction internship, received the task to do the foreign language version of the product, web-side intern currently only I one, the front-end and PHP are to me to do ...

Now need to write a script, processing two JSON files, the Bjson file in a does not have the Chinese words to find to write another JSON, the actual requirements are much more complex, first write a preliminary demo:

var fs = require (' FS '), PATH = require (' path '); let ans = {};//The difference set for one side (not a strict difference set, just one side) function diff (A, b) {var keys    A = Object.keys (a);    Console.log (KEYSA);    var keysb = Object.keys (b); Console.log (KEYSB) var ret = {} Keysb.foreach (key = {if (keysa.indexof (key) = = = 1) {ret[        Key] = B[key];    }    }); return ret;}    Fs.readfile (Path.join (__dirname, ' Json/json1.json '), ' UTF8 ', function (err,data1) {if (err) throw err;    Console.log (DATA1);    Data1 = Json.parse (data1)//Console.log (typeof data1);         Fs.readfile (Path.join (__dirname, ' Json/json2.json '), ' UTF8 ', function (ERR,DATA2) {if (err) throw err;         Console.log (DATA2);         Data2 = Json.parse (data2);        Ans = diff (data1, data2);        Console.log (typeof ans);        Console.log (ANS);        Let Str_ans = Json.stringify (Ans,null, 4);            Fs.writefile (' Ret.json ', Str_ans, ' UTF8 ', (err) = {if (err) throw err; Console. log (' done ');     }); });});

Note ReadFile and Readfilesync,writefile and Writefilesync, the latter being the synchronous version of the former.
But the JSON format that was written at the beginning of the big Ugly:

Later, Stringify's API was found to know the formatted output.

Json.stringify (value[, Replacer [, Space]])

Parameters

    • Value
      The value that will be serialized into a JSON string.
    • Replacer Optional
      If the parameter is a function, each property of the serialized value is transformed and processed by the function during serialization, and if the parameter is an array, only the name of the property contained in the array is serialized into the final JSON string, or if the parameter is null or not supplied, All properties of the object are serialized; For more detailed explanations and examples of this parameter, refer to the article using native JSON objects.
    • Space is optional
      Specifies a blank string for indentation, which is used to beautify the output (pretty-print), and if the argument is a number, it represents how many spaces it has; the upper limit is 10. A value less than 1 means there is no space, and if the argument is a string (the first 10 letters of the string), the string is used as a space, and if the argument is not provided (or null), there will be no spaces.

The return value is a JSON string that represents the given value.

Node reads and writes JSON, formats output JSON

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.