java-script:object-to-string-and-back
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| java-script:object-to-string-and-back [2023/08/11 09:34] – created odefta | java-script:object-to-string-and-back [2023/08/11 09:59] (current) – [Notes] odefta | ||
|---|---|---|---|
| Line 21: | Line 21: | ||
| ===== Object to String ===== | ===== Object to String ===== | ||
| - | - **JSON.stringify**: | + | - **JSON.stringify**: |
| - | <code javascript> | + | |
| const str = JSON.stringify(obj); | const str = JSON.stringify(obj); | ||
| </ | </ | ||
| - | - **toString Method**: If the object has a custom toString method, you can call it to get a string representation. | + | - **toString Method**: If the object has a custom toString method, you can call it to get a string representation. <code javascript> |
| - | <code javascript> | + | |
| const obj = { | const obj = { | ||
| name: ' | name: ' | ||
| age: 30, | age: 30, | ||
| toString: function() { | toString: function() { | ||
| - | return `Name: ${this.name}, | + | return `{" |
| } | } | ||
| }; | }; | ||
| - | const str = obj.toString(); | + | const str = obj.toString(); |
| </ | </ | ||
| + | ===== String to Object ===== | ||
| + | **JSON.parse**: | ||
| + | <code javascript> | ||
| + | const str = ' | ||
| + | const obj = JSON.parse(str); | ||
| + | </ | ||
| + | |||
| + | ===== Notes ===== | ||
| + | |||
| + | < | ||
| + | **JSON.stringify** will only include properties that have enumerable **string keys**. It will exclude properties with symbol keys, and it will also **skip** values that are **undefined, | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | **JSON.parse** expects a **valid JSON** string. If the string is not valid JSON, it will throw an error. | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | 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.1691746473.txt.gz · Last modified: 2023/08/11 09:34 by odefta
