JavaScript provides methods JSON.stringify to serialize into JSON and JSON.parse to read from JSON. Both methods support transformer functions for smart reading/writing. If an object has toJSON, then it is called by JSON.stringify. JavaScript JSON: Serialization Options. The method JSON.stringify can accept at the most two arguments, in addition to the object being serialized, these arguments are used to specify alternate ways to serialize a JavaScript Object. The first argument is filter, which can be either a function or an array. Mar 03, 2019 The JSON.stringify method converts a JavaScript object or value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified. No, the standard way to serialize to JSON is to use an existing JSON serialization library. If you don't wish to do this, then you're going to have to write your own serialization methods. Mar 30, 2017 How to: Serialize and deserialize JSON data JSON (JavaScript Object Notation) is an efficient data encoding format that enables fast exchanges of small amounts of data between client browsers and AJAX-enabled Web services.
Active3 years, 9 months ago
I'm using the tag-it library for jquery to make a tagging system (a bit like the stackoverflow one).
After the user types his tags the library returns a javascript array that I want to save in a MySQL database. I didn't find a serialize and unserialize function in javascript.
Before coding my own function I'd like to make sure I'm not reinventing the wheel here. It seems crazy that there is no native way to save an array to a database and then use it again.
tl;dr => how can I save a javascript array in a MySQL database to reuse it later ?
LukmoLukmo86044 gold badges1414 silver badges2727 bronze badges
3 Answers
You can use
JSON.stringify()
(MDN docu) and JSON.parse()
(MDN docu) for converting a JavaScript object into a string representation to store it inside a database.If your backend is written in PHP, there are similar methods to work with JSON strings there:
json_encode()
(PHP docu) and json_decode()
(PHP docu).Most other languages offer similar functionalities for JSON strings.
SirkoSirko
57.3k1616 gold badges108108 silver badges149149 bronze badges
You can use
JavaScript Object Notation
(JSON
) format. Javascript
supports these methods:- JSON.stringify ->serializes object to string
- JSON.parse ->deserializes object from string
Json.parse
Engineer39k1010 gold badges7474 silver badges8585 bronze badges
How about just JSONing it?
By the way, JSON is often used for serializing in some other languages, even though they have their own serializing functions. )
raina77owraina77ow80.7k1010 gold badges147147 silver badges183183 bronze badges
Not the answer you're looking for? Browse other questions tagged javascriptjquerytag-it or ask your own question.
JSON is a format for storing and transporting data.
JSON is often used when data is sent from a server to a web page.
What is JSON?
- JSON stands for JavaScript Object Notation
- JSON is a lightweight data interchange format
- JSON is language independent *
- JSON is 'self-describing' and easy to understand
* The JSON syntax is derived from JavaScript object notation syntax, but the JSON format is text only. Code for reading and generating JSON data can be written in any programming language.
JSON Example
This JSON syntax defines an employees object: an array of 3 employee records (objects):
JSON Example
{
'employees':[
{'firstName':'John', 'lastName':'Doe'},
{'firstName':'Anna', 'lastName':'Smith'},
{'firstName':'Peter', 'lastName':'Jones'}
]
}
'employees':[
{'firstName':'John', 'lastName':'Doe'},
{'firstName':'Anna', 'lastName':'Smith'},
{'firstName':'Peter', 'lastName':'Jones'}
]
}
The JSON Format Evaluates to JavaScript Objects
The JSON format is syntactically identical to the code for creating JavaScript objects.
Because of this similarity, a JavaScript program can easily convert JSON data into native JavaScript objects.
JSON Syntax Rules
- Data is in name/value pairs
- Data is separated by commas
- Curly braces hold objects
- Square brackets hold arrays
JSON Data - A Name and a Value
JSON data is written as name/value pairs, just like JavaScript object properties.
A name/value pair consists of a field name (in double quotes), followed by a colon, followed by a value:
JSON names require double quotes. JavaScript names do not.
JSON Objects
JSON objects are written inside curly braces.
Just like in JavaScript, objects can contain multiple name/value pairs:
JSON Arrays
JSON arrays are written inside square brackets.
Just like in JavaScript, an array can contain objects:
'employees':[
{'firstName':'John', 'lastName':'Doe'},
{'firstName':'Anna', 'lastName':'Smith'},
{'firstName':'Peter', 'lastName':'Jones'}
]
{'firstName':'John', 'lastName':'Doe'},
{'firstName':'Anna', 'lastName':'Smith'},
{'firstName':'Peter', 'lastName':'Jones'}
]
In the example above, the object 'employees' is an array. It contains three objects.
Each object is a record of a person (with a first name and a last name).
Lord Shiva Songs Mp3 Bhajan Free Download. Lord Shiva Songs Mp3 Bhajan Free Download. Panchalingam Album Bhajan ( Bhakti Songs ) Mp3 Free Download. Free lord shiva songs download. Lord Shiva Songs Mp3 Bhajan Free Download. Shankara Album Bhajan ( Bhakti Songs ) Mp3 Free Download.
Converting a JSON Text to a JavaScript Object
A common use of JSON is to read data from a web server, and display the data in a web page.
Serialize Json Javascript Converter
For simplicity, this can be demonstrated using a string as input.
First, create a JavaScript string containing JSON syntax:
var text = '{ 'employees' : [' +
'{ 'firstName':'John' , 'lastName':'Doe' },' +
'{ 'firstName':'Anna' , 'lastName':'Smith' },' +
'{ 'firstName':'Peter' , 'lastName':'Jones' } ]}';
'{ 'firstName':'John' , 'lastName':'Doe' },' +
'{ 'firstName':'Anna' , 'lastName':'Smith' },' +
'{ 'firstName':'Peter' , 'lastName':'Jones' } ]}';
Then, use the JavaScript built-in function
JSON.parse()
to convert the string into a JavaScript object:Finally, use the new JavaScript object in your page:
Example
<p></p>
<script>
document.getElementById('demo').innerHTML =
obj.employees[1].firstName + ' ' + obj.employees[1].lastName;
</script>
Try it Yourself »<script>
document.getElementById('demo').innerHTML =
obj.employees[1].firstName + ' ' + obj.employees[1].lastName;
</script>
You can read more about JSON in our JSON tutorial.