Serialize JSON like objects passed to `execute` as `key = 'val'` pairs?
See original GitHub issueawait ctx.db.execute('INSERT INTO user SET ?', [{ email: body.email, password: hash }])
This throws an error, as JSON like objects are simply serialized as a JSON string. Is there a way to serialize as key = 'val'
pairs? If not, what is the reason why?
https://github.com/mysqljs/mysql serializes JSON like objects like:
Objects are turned into key = ‘val’ pairs for each enumerable property on the object. If the property’s value is a function, it is skipped; if the property’s value is an object, toString() is called on it and the returned value is used.
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Serializing and Deserializing Json objects with key value pairs ...
First open Visual Studio and create new project in Console application template.
Read more >Work with JSON data - SQL Server - Microsoft Learn
OPENJSON transforms the array of JSON objects into a table in which each object is represented as one row, and key/value pairs are...
Read more >How to Create JSON Structure Using C# (JSON Creation Part 2)
Click on the Validate JSON button to validate it. The JSON key-value pairs will be arranged and validation will be performed on the...
Read more >JSON serialize a dictionary with tuples as key - Stack Overflow
JSON only supports strings as keys. You'll need to choose a way to represent those tuples as strings.
Read more >Query and Manipulate JSON Data with T-SQL - Pluralsight
In this guide you will learn how to utilize several JSON built-in ... each individual cell is represented by a specific key-value pair....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
No. “Execute” is for prepared statements, where code (“sql”) and data separated.
key = 'val'
part is breaking this - it requires modification of sql query you want to execute.If you really need this, you can use
.query()
. With prepared statement each placeholder must map 1:1 to input parameter (In case of mysql JSON type it can be an object, but it’ll be serialized to string before transmitting - this is what you observe. We could add some warning if types are inherently incompatible (and we know in advance, which is usually but not always possible - we get this metadata from PREPARE)Found issue https://github.com/sidorares/node-mysql2/issues/553 which is more specific to my question, so I’ll continue there.