Sql query for data stored as JSON - javascript

I have data stored in column as JSON, as shown below:
{"category":{"value":CAT, "demo":A.......
I want to query in SSMS and want an output like:
CAT
I tried "SELECT JSON_QUERY(TABLENAME,'$.CATEGORY') FROM TABLENAME" which gave result of the
{"value":CAT, "demo":A....... but I want only CAT. How do I do that?

Use JSON_VALUE to get specific values.
SELECT JSON_VALUE(TABLENAME,'$.CATEGORY.VALUE') AS Value
JSON_QUERY is for JSON fragments.
Source

You should use JSON_VALUE.
SELECT JSON_VALUE(ColumnName, '$.category.value') FROM TableName
Take note of the first parameter, it should be the column name, not the table name (as opposed to your example).

Related

Extracting values from JMSE

I want extract text values from every sub objects the given JSON structure . For do that I have used following JMSEPath query translations.en.*[0].[*].children.text but I was not unable to extract the value . Can someone suggest me a correct query or other approches
It may be better if you add to the question the expected output as well.
Making a big assumption that you want all text values "flatten up" into a single array you can try this: translation.en.*[].children[].text
Ref https://jmespath.org/tutorial.html#flatten-projections

is there any way to select all the fields data from the table except one field data using nodejs

I want to fetch all the fields data but don't want to specify the fields name and don't want to create a view.
I have tried
WITH orderschema as
(SELECT array_to_string(ARRAY(SELECT c.column_name
FROM information_schema.columns As c
WHERE table_name = 'orders'
AND c.column_name NOT IN('Quantity')
), ','))
SELECT * from orderschema
but this is returning the schema fields name as a single string and i can't use this single string to get all the fields from the orders table.
is there any way to exclude the field directly in psql?
Generally speaking no such way exists.
But - if you plan on converting data to JSON in Pg (which you shouldn't for performance reasons, but you might if you really, really want to), you could just delete the column from json.
The best way, though, is to write query that lists only the columns you need.

how to get a part of a text from a column in the database using tdbnput and print in talend

I have a talend job that goes like the above one and i am using a select statement to access the data from oracle
select id,name,details from employee ;
and send the results via email.However for a particular column details it has 10 different lines and that are dynamic and has various other unwanted/irrelevant information and i would like to precisely filter few lines and print them to the mail using tjava component.
How can i do that?
for eg:details column has values like:
"hi i am john
i work at abc
i go to oxford
i reside at 1st street
goodbye"
so the lines are totally random,i would want to match based on a specific keyword lets say "reside at" alone and filter that for different records and capture that in that output?
In order to do this you need to create a custom SQL query in the tDBinput.
"select id,name,details from employee
where details like '%reside at%'"
(Where the % are any text check the SQL "LIKE" function for more details)
With this you will filter the row you want.
If you need more filter you can modify you SQL query like you want.

how should i store lists of lists in MYSQL

I want to store this list of lists but I don't know how to store it in MySQL
list[x][y] the items in this list contains {li:[{x:x,y:y}] , pos:{x:y}}
list[x][y].li[z].x
list[x][y].li[z].y
list[x][y].pos.x
list[x][y].pos.y
for better undersing, please have a look at this
edited:
is this right? so this means i will only have 2 tables?
You should use a separate table with sub-lists that have a column parent_id, and then a third table with actual list items of low level lists.
The query for this will look like this:
SELECT li.x, li.y, sl.id
FROM li_items li
JOIN sub_lists sl on li.list_id = sl.id
JOIN lists l on sl.parent_id = l.id;
The process of converting the result rows depends on if you use some ORM or plain mysql client.
You could also store it as a JSON, as deleted answer has suggested, but than you wan't be able to query specific items without selecting and parsing all the lists. You could also use MySQL's JSON column, but In your case having separate tables seems to be better

Creating a better JSON format

I am returning a query object from Coldfusion as a JSON string which I then parse into JSON in Javascript. It has a bit of a strange format when I finally log it though.
I am faced with two problems. First, I do not know how to access the lowest element (i.e Arthur Weasley) as I cannot use a number in my selector (response.DATA[0].0 doesn't work because the lowest field name is a number). Second, is there any way to assign the values in the columns section to the fields that are numbered 1, 2 and 3?
What I'm really asking is how do I select my lowest level of data? If that can't be done because of the numbers for field names, how do I change the names to something more fitting?
My data logged:
First entry of first entry of DATA = response.DATA[0][0]
So
name = reponse.DATA[0][0];
trainsThing = response.DATA[0][1];

Categories

Resources