Skip to content
Advertisement

How to extract JSON from sql query?

My query looks like this:

JavaScript

I want to retry the messages that failed, but the message is a json string and the offset is a regular string. I need to extract the json messages and the offset strings and then iterate through each row. How would I do this? RIght now I am doing something like this:

JavaScript

//Service

JavaScript

buildQueryResult() simply appends all the strings together so the mapper can read it altogether. The mapper ignores the offset and doesn’t include it in the json, bceause it isn’t a valid json. How do I keep the offset and message and pass both to the service.retry() method?

Advertisement

Answer

You need to convert your string/json to a POJO or another way to convert it to object. I recomend you to use Jackson library and map it to a pojo.

Another way is to use some tools from the database to query directly:

JavaScript

In that way you can handle the values as a string at your code.

Can read more here: https://docs.microsoft.com/en-us/sql/t-sql/functions/json-value-transact-sql?view=sql-server-ver15

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement