question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Serialize JSON like objects passed to `execute` as `key = 'val'` pairs?

See original GitHub issue
await 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:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sidorarescommented, Mar 2, 2018

If not, what is the reason why?

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)

0reactions
bigyonkcommented, Nov 9, 2018

Found issue https://github.com/sidorares/node-mysql2/issues/553 which is more specific to my question, so I’ll continue there.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found