Topic
Summary
- How to save the state of a variable in order to be able to recreate it when needed?
- How to transform a JavaScript variable into a JSON string by using
JSON.stringify()
? (DVJS11
)
- How to parse a JSON string and transform it into a JavaScript variable by using
JSON.parse()
? (DVJS13
)
- How to transform a JavaScript variable into a JSON file? (
DVJS21
)
- How to transform a JSON file into a JavaScript variable like
aoa_drinks
? (DVJS23
)
Test
Summary
input
Summary
dictionary files
- The input data of the DVJS13 is the output data of the DVJS11.
- The input data of the DVJS23 is the output data of the DVJS21.
DVJS11_transform_a_JavaScript_variable_into_a_JSON_string
Summary
Main DVJS
Code Name |
Data type |
Group By |
Purposes |
Remark |
DVJS11 _transform _a_JavaScript_variable _into _a_JSON_string |
1.aoh_drinks : an array of hashes
2.s_JSON : a JSON string |
no |
To transform a JavaScript variable into a JSON string by using JSON.stringify()
|
a JS variable to a JSON string(memory) |
Notes
Summary
Q1: What does the following DQL statement mean?
// M21. define s_JSON: Transform a JavaScript variable into a JSON string
// with two space characters as white space for indentation
// JSON.stringify(value[, replacer[, space]])
// #####################################################################
let s_JSON = JSON.stringify(aoh_drinks, null, 2);
A1
- The above statement means that to transform a JavaScript variable like
aoh_drinks
into a JSON string like s_JSON
by using two space characters as white space for indentation.
code DVJS11_transform_a_JavaScript_variable_into_a_JSON_string
Summary_code
title: DVJS11_transform_a_JavaScript_variable_into_a_JSON_string =>To transform a JavaScript variable into a JSON string by using `JSON.stringify()`
collapse: close
icon:
color:
```dataviewjs
// M01. define print() :
// #####################################################################
// version : 2022-05-01 v1.00 Justdoitcc
function print(...aArgs) {
let aText = [...aArgs];
let sTextJoined = aText
.join("")
.replace(/[ ]/g, " ")
.replace(/(\r\n|\r|\n)/g, "<br>");
dv.el("span", sTextJoined);
return true;
}
// M11. define aoh_drinks : aoh = an array of hashes
// #####################################################################
// The `aoh_drinks` consists of each `h_drink`.
// A `h_drink` is a row(hash), which consists of three fields sush as `name`, `price` and `caffeine content`.
let aoh_drinks = [
{ name: "Black Coffee", price: 120, "caffeine content": 300 },
{ name: "Green Tea", price: 100, "caffeine content": 200 },
{ name: "Apple Juice", price: 110, "caffeine content": 0 },
{ name: "Iced Chocolate", price: 130, "caffeine content": 0 },
{ name: "Hot Chocolate", price: 105, "caffeine content": 0 },
];
// M21. define s_JSON: Transform a JavaScript variable into a JSON string
// with two space characters as white space for indentation
// JSON.stringify(value[, replacer[, space]])
// #####################################################################
let s_JSON = JSON.stringify(aoh_drinks, null, 2);
// M31.Output the heading as H5:
// #####################################################################
dv.span("##### M33.Transform a JavaScript variable into a JSON string");
// M33.Output s_JSON:
// #####################################################################
dv.span(s_JSON);
// M35.Output s_JSON: Require print() in the step M01
// #####################################################################
//print(s_JSON);
Screenshots(DVJS11)
Output by M33.dv.span (Not formatted)
```JSON
M33.Transform a JavaScript variable into a JSON string
[
{
"name": "Black Coffee",
"price": 120,
"caffeine content": 300
},
{
"name": "Green Tea",
"price": 100,
"caffeine content": 200
},
{
"name": "Apple Juice",
"price": 110,
"caffeine content": 0
},
{
"name": "Iced Chocolate",
"price": 130,
"caffeine content": 0
},
{
"name": "Hot Chocolate",
"price": 105,
"caffeine content": 0
}
]
```
Output by M35.print (formatted)
```JSON
M33.Transform a JavaScript variable into a JSON string
[
{
"name": "Black Coffee",
"price": 120,
"caffeine content": 300
},
{
"name": "Green Tea",
"price": 100,
"caffeine content": 200
},
{
"name": "Apple Juice",
"price": 110,
"caffeine content": 0
},
{
"name": "Iced Chocolate",
"price": 130,
"caffeine content": 0
},
{
"name": "Hot Chocolate",
"price": 105,
"caffeine content": 0
}
]
```
DVJS13_transform_a_JSON_string_into_a_JavaScript_variable
Summary
Main DVJS
Code Name |
Data type |
Group By |
Purposes |
Remark |
DVJS13 _transform _a_JSON_string _into _a_JavaScript_variable |
1.s_JSON_from_Q82_DVJS11 : a JSON string
2.aoh_drinks : an array of hashes |
no |
To parse a JSON string and transform it into a JavaScript variable by using JSON.parse()
|
a JSON string(memory) to a JS variable |
Notes
Summary
Q1: What does the following DQL statement mean?
// M21. define aoh_drinks:
// Parse a JSON string into a JavaScript variable
// JSON.parse(text, reviver)
// #####################################################################
let aoh_drinks = JSON.parse(s_JSON_from_Q82_DVJS11);
A1
- The above statement means that to parse a JSON string like
s_JSON_from_Q82_DVJS11
and transform it into a JavaScript variable like aoh_drinks
.
code DVJS13_transform_a_JSON_string_into_a_JavaScript_variable
Summary_code
title: DVJS13_transform_a_JSON_string_into_a_JavaScript_variable =>To parse a JSON string and transform it into a JavaScript variable by using `JSON.parse()`
collapse: close
icon:
color:
```dataviewjs
// M11. define s_jsonString_from_DVJS11:
// The following expression should be copied by yourself from the result of the Q82_DVJS11.
// #####################################################################
let s_JSON_from_Q82_DVJS11 = `
[
{
"name": "Black Coffee",
"price": 120,
"caffeine content": 300
},
{
"name": "Green Tea",
"price": 100,
"caffeine content": 200
},
{
"name": "Apple Juice",
"price": 110,
"caffeine content": 0
},
{
"name": "Iced Chocolate",
"price": 130,
"caffeine content": 0
},
{
"name": "Hot Chocolate",
"price": 105,
"caffeine content": 0
}
]
`;//<= Never modify it.
// M21. define aoh_drinks:
// Parse a JSON string into a JavaScript variable
// JSON.parse(text, reviver)
// #####################################################################
let aoh_drinks = JSON.parse(s_JSON_from_Q82_DVJS11);
// M31.Output the heading as H5:
// #####################################################################
dv.header(5, "M33.Parse a JSON string and transform it into a JavaScript variable");
// M33.Output aoh_drinks:
// #####################################################################
//dv.span(aoh_drinks);
dv.table(
["N", "Name", "Price", "Caffeine Content"],
aoh_drinks.map((h_drink, index) => [
index + 1,
h_drink.name,
h_drink.price,
h_drink["caffeine content"],
])
);
Screenshots(DVJS13)
DVJS21_transform_a_JavaScript_variable_into_a_JSON_file
Summary
Main DVJS
Code Name |
Data type |
Group By |
Purposes |
Remark |
DVJS21 _transform _a_JavaScript_variable _into _a_JSON_file |
aoa_drinks : an array of arrays |
no |
1.To equire Node.js: Node.js:JavaScript runtime 2.To transform a JavaScript variable into a JSON file |
a JS variable to a JSON file
The DVJS21 is based on the heading How to write to JSON files using the fs.writeFile method in the following topic. - Reading and writing JSON files in Node.js: A complete tutorial
|
Notes
Summary
To modify the code: M31
Original Example
```JS
// M31. define s_file_folder : "D:/in/test"
// #####################################################################
let s_file_folder = "D:/in/test";
```
Another Example
```JS
// M31. define s_file_folder : "D:/in/test"
// #####################################################################
let s_file_folder = "D:/data_exchange/test";
```
To modify the code: M41
Original Example
```JS
// M41. define s_filename: "newX_from_Q82_DVJS21.json"
// #####################################################################
let s_filename = "newX_from_Q82_DVJS21.json";
```
Another Example
```JS
// M41. define s_filename: "newX_from_Q82_DVJS21.json"
// #####################################################################
let s_filename = "aoa_drinks_from_Q82_DVJS21.json";
```
code DVJS21_transform_a_JavaScript_variable_into_a_JSON_file
Summary_code
title: DVJS21_transform_a_JavaScript_variable_into_a_JSON_file =>1.To equire Node.js: [Node.js:JavaScript runtime](https://nodejs.org/en/) 2.To transform a JavaScript variable into a JSON file
collapse: close
icon:
color:
```dataviewjs
// M11. define aoa_drinks : aoa = an array of arrays
// #####################################################################
// The `aoa_drinks` consists of each `a_drink`.
// An `a_drink` is a row(array), which consists of three fields sush as `name`, `price` and `caffeine content`.
// "Name", "price", "Caffeine Content"
let aoa_drinks = [
["Black Coffee", 120, 300],
["Green Tea", 100, 200],
["Apple Juice", 110, 0],
["Iced Chocolate", 130, 0],
["Hot Chocolate", 105, 6],
];
// M21. define s_JSON: Transform a JavaScript variable into a JSON string
// with two space characters as white space for indentation
// #####################################################################
let s_JSON = JSON.stringify(aoa_drinks, null, 2);
// M30. define fs: require Node.js FS Module
// #####################################################################
const fs = require('fs');
// M31. define s_file_folder : "D:/in/test"
// #####################################################################
let s_file_folder = "D:/in/test";
// M33. create a folder recursively:
// where s_file_folder doesn't exist
// Creates D:/in/test, regardless of whether `D:/in` and D:/in/test exists.
// #####################################################################
try {
// M33.TRY11 Successfully created a folder recursively:
// #########################################################
if (!fs.existsSync(s_file_folder)) {
fs.mkdirSync(s_file_folder, { recursive: true });
}
} catch (err) {
// M33.TRY21 Error created a folder recursively: To print messages
// #########################################################
dv.span(`##### Error : Folder not created:`);
dv.span(`##### ${s_file_folder}`);
}
// M41. define s_filename: "newX_from_Q82_DVJS21.json"
// #####################################################################
let s_filename = "newX_from_Q82_DVJS21.json";
// M43. define s_output_filepath: "D:/in/test/newX_from_Q82_DVJS21.json"
// #####################################################################
let s_output_filepath = s_file_folder + "/" + s_filename;
// M45. define b_file_exists:
// #####################################################################
let b_file_exists = true;
// M47. define b_file_exists: To check the existence of s_output_filepath
// fs.existsSync(path): Returns true if the path exists, false otherwise.
// Sync.= Synchronous
// #####################################################################
b_file_exists = fs.existsSync(s_output_filepath);
// M49. check the existence of s_output_filepath:
// To print a message where it exists
// The DVJS21 ONLY creates a new file instead of updating an older file becaue it is too easy to run the DVJS21 in an Obsidian vault.
// Moreover, the older file may be to be used by the DVJS23.
// #####################################################################
if (b_file_exists) {
// CASE_01: The file exists.
dv.span(`##### ${s_output_filepath} exists.`);
dv.span("##### You have to remove it first.");
}
// M61. fs.writeFile :
// Write a JSON string to a local text file where it doesn't exist.
// #####################################################################
if (b_file_exists) {
// CASE_01: The file exists.
// do nothing
} else {
// CASE_02: The file doesn't exist.
fs.writeFile(s_output_filepath, s_JSON, (err) => {
if (err) {
// M61.IF11 Error writing to a local text file: To print messages
// #########################################################
dv.span("##### Error writing to a local text file");
dv.span(err);
} else {
// M61.IF21 Successfully wrote to a local text file: To print messages
// #########################################################
dv.span("##### Successfully wrote to a local text file");
}
});
}
Screenshots(DVJS21)
Output
- filename :
D:/in/test/newX_from_Q82_DVJS21.json
```JSON
[
[
"Black Coffee",
120,
300
],
[
"Green Tea",
100,
200
],
[
"Apple Juice",
110,
0
],
[
"Iced Chocolate",
130,
0
],
[
"Hot Chocolate",
105,
6
]
]
```
DVJS23_transform_a_JSON_file_into_a_JavaScript_variable
Summary
Main DVJS
Code Name |
Data type |
Group By |
Purposes |
Remark |
DVJS23 _transform _a_JSON_file _into _a_JavaScript_variable |
“D:/in/test/ newX_from_Q82_DVJS21.json”: a JSON file |
no |
1.To equire Node.js: Node.js:JavaScript runtime 2.To transform a JSON file into a JavaScript variable like aoa_drinks
|
a JSON file to a JS variable
The DVJS23 is based on the heading How to read a JSON file in the following topic. - How to Read, Write and Parse JSON in JavaScript
|
Notes
Summary
To modify the code: M33
- To define s_input_filepath
- The value of
s_input_filepath
must be the same as the value of s_output_filepath
defined in the DVJS21.
Original Example
```JS
// M33. define s_input_filepath:
// The file was created by the Q82_DVJS21.
// #####################################################################
let s_input_filepath = "D:/in/test/newX_from_Q82_DVJS21.json";
```
Another Example
```JS
// M33. define s_input_filepath:
// The file was created by the Q82_DVJS21.
// #####################################################################
let s_input_filepath = "D:/data_exchange/test/aoa_drinks_from_Q82_DVJS21.json";
```
To modify the code: M51.TRY11
Original Example
```JS
// M51.TRY11 Successfully parsed a JSON file: To print messages
// #########################################################
let aoa_drinks = JSON.parse(data);
dv.span("##### Successfully parsed a JSON file");
dv.table(["Name", "Price", "Caffeine Content"], aoa_drinks);
//dv.span(aoa_drinks);
```
Another Example
```JS
// M51.TRY11 Successfully parsed a JSON file: To print messages
// #########################################################
let drinks = JSON.parse(data);
dv.span("##### Successfully parsed a JSON file");
dv.table(["Name", "Price", "Caffeine Content"], drinks);
//dv.span(drinks);
```
code DVJS23_transform_a_JSON_file_into_a_JavaScript_variable
Summary_code
title: DVJS23_transform_a_JSON_file_into_a_JavaScript_variable =>1.To equire Node.js: [Node.js:JavaScript runtime](https://nodejs.org/en/) 2.To transform a JSON file into a JavaScript variable like `aoa_drinks`
collapse: close
icon:
color:
```dataviewjs
// M30. define fs: require Node.js FS Module
// #####################################################################
const fs = require('fs');
// M33. define s_input_filepath:
// The file was created by the Q82_DVJS21.
// #####################################################################
let s_input_filepath = "D:/in/test/newX_from_Q82_DVJS21.json";
// M35. define b_file_exists:
// #####################################################################
let b_file_exists = false;
// M37. define b_file_exists: To check the existence of s_input_filepath
// fs.existsSync(path): Returns true if the path exists, false otherwise.
// Sync.= Synchronous
// #####################################################################
b_file_exists = fs.existsSync(s_input_filepath);
// M39. check the existence of s_input_filepath:
// To print a message where it doesn't exist
// #####################################################################
if (b_file_exists) {
// CASE_01: The file exists.
// do nothing
} else {
// CASE_02: The file doesn't exist.
dv.span(`##### ${s_input_filepath} doesn't exist.`);
dv.span("##### You have to create it first by the Q82_DVJS21.");
}
// M51. fs.readFile :
// Read a JSON file
// and try to parse it into a JavaScript variable like `aoa_drinks`
// #####################################################################
if (b_file_exists) {
// CASE_01: The file exists.
// #################################################################
fs.readFile(s_input_filepath, (err, data) => {
// M51.IF10 Error reading a JSON file: To print messages
// #########################################################
if (err) {
dv.span("##### Error reading a JSON file");
dv.span(err);
}
try {
// M51.TRY11 Successfully parsed a JSON file: To print messages
// #########################################################
let aoa_drinks = JSON.parse(data);
dv.span("##### Successfully parsed a JSON file");
dv.table(["Name", "Price", "Caffeine Content"], aoa_drinks);
//dv.span(aoa_drinks);
} catch (err) {
// M51.TRY21 Error parsing a JSON file: To print messages
// #########################################################
dv.span("##### Error parsing a JSON file");
dv.span(err);
}
});
} else {
// CASE_02: The file doesn't exist.
// #####################################################################
// do nothing
// exit the code
}
Screenshots(DVJS23)
Input
- filename :
D:/in/test/newX_from_Q82_DVJS21.json
```JSON
[
[
"Black Coffee",
120,
300
],
[
"Green Tea",
100,
200
],
[
"Apple Juice",
110,
0
],
[
"Iced Chocolate",
130,
0
],
[
"Hot Chocolate",
105,
6
]
]
```
Output(DVJS23)
Related resources
Summary