java-script:object-to-string-and-back
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
java-script:object-to-string-and-back [2023/08/11 12:45] – odefta | java-script:object-to-string-and-back [2023/08/11 12:59] (current) – [Notes] odefta | ||
---|---|---|---|
Line 55: | Line 55: | ||
< | < | ||
If you're working with a more complex object structure that includes methods or other non-serializable values, you may need to implement custom serialization/ | If you're working with a more complex object structure that includes methods or other non-serializable values, you may need to implement custom serialization/ | ||
+ | </ | ||
+ | |||
+ | <note tip> | ||
+ | You can format the output of the **JSON.stringify** to be more easy to read or add transformation / filtering. | ||
+ | Syntax: | ||
+ | <code javascript> | ||
+ | JSON.stringify(value, | ||
+ | </ | ||
+ | * **value**: The value to convert to a JSON string. | ||
+ | * **replacer**: | ||
+ | * **space**: A String or Number object that's used to insert **white space** into the output JSON string for readability purposes. | ||
+ | |||
+ | Example: | ||
+ | <code javascript> | ||
+ | const obj = { | ||
+ | name: ' | ||
+ | age: 30, | ||
+ | friends: [' | ||
+ | }; | ||
+ | |||
+ | const jsonString = JSON.stringify(obj, | ||
+ | // 2 is the number of spaces for indentation | ||
+ | </ | ||
+ | |||
+ | Output: | ||
+ | <code javascript> | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | ] | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | To ski age from being included: | ||
+ | <code javascript> | ||
+ | const obj = { name: ' | ||
+ | const filteredJSON = JSON.stringify(obj, | ||
+ | console.log(filteredJSON); | ||
+ | </ | ||
+ | |||
</ | </ |
java-script/object-to-string-and-back.1691747103.txt.gz · Last modified: 2023/08/11 12:45 by odefta