I have a data like below and I would like to get a value of sub_key1:
'{"key_1":"val_1", "key_2":"{\"sub_key1\":\"sub_val1\", \"sub_key2\":\"sub_val2\"}"}'
If I run below query, it works fine and gets me the value of key_2.
SELECT ('{"key_1":"val_1", "key_2":"{\"sub_key1\":\"sub_val1\", \"sub_key2\":\"sub_val2\"}"}')::json->'key_2';
But if I run below query, I do not get anything in return.
SELECT (('{"key_1":"val_1", "key_2":"{\"sub_key1\":\"sub_val1\", \"sub_key2\":\"sub_val2\"}"}')::json->'key_2')::json->'sub_key1';
How to get a value of sub_key1?
The value you get out by using -> is a JSON string literal. Casting that to json will do nothing, and accessing a property on a string doesn't work.
You'll need to use ->> instead to get the string as a postgres text which you then can convert to a json object:
SELECT (('{"key_1":"val_1", "key_2":"{\"sub_key1\":\"sub_val1\", \"sub_key2\":\"sub_val2\"}"}')::json->>'key_2')::json->'sub_key1';
But either way, fix the system that generates this JSON not to put serialised JSON strings into JSON.
Related
I have a text column that save a json string on it.
I want to select specific element of json as new column and i do not want to change type of this column to json.
Is it possible?
How can i do that?
My table name is 'logs' and my column name is 'response' and my target element in JSON string is 'server_response_time'.
If you have a valid JSON string stored in a string column, you can directly use JSON functions on it. MySQL will happily convert it to JSON under the hood.
So:
with t as (select '{"foo": "bar", "baz": "zoo"}' col)
select col, col ->> '$.foo' as foo
from t
If your string is not valid JSON, this generates a runtime error. This is one of the reasons why I would still recommend storing your data as JSON rather than string: that way, data integrity is enforced at the time when your data is stored, rather than delayed until it is read.
I want to select the data from my table which is json data so to show my table data is like this :
user_id: 1
metaname: mymetaname
meta_value: a:1:{i:0;a:10:{s:7:"street1";s:36:"shiv plaza";s:4:"city";s:5:"surat";s:5:"state";s:7:"gujarat";s:7:"zipcode";s:6:"395010";s:14:"dollet_country";s:2:"IN";s:10:"tostreet1l";s:5:"surat";s:7:"tocityl";s:5:"surat";s:8:"tostatel";s:5:"surat";s:10:"tozipcodel";s:6:"395000";s:17:"todollet_countryl";s:2:"IN";}}
And i am trying to run this query :
SELECT user_id,JSON_EXTRACT(meta_value, '$."city"') FROM `usermetatable`
But it's showing error :
[Invalid JSON text in argument 1 to function json_extract: "Invalid
value." at position 0.]
My json data in table can not be changed to other and it's correct JSON for sure, Could anyone correct above query ?
That's not JSON data. It looks like a serialized PHP object. See http://php.net/serialize
There's no MySQL function for extracting a field from that serialized object. You should fetch the whole object into a PHP app, and call unserialize() on it, then access the object members.
I am using anorm with play-scala and have execute queries using:
val result = SQL("SELECT * FROM users)()
What I have been doing is the following:
val queryResult = SQL(query)().map(result =>
result.asList
).toList
However, I have realized that instead of converting it to a list, I want to actually receive JSON. Since I may change the query, I would like the ability for a JSON serializer that takes in any result and understands the column names and convert them and the results into JSON.
EDIT:
I'm using play 2.3.10 with scala version 2.11.6.
I'm trying to map the SqlResult's metadata column names to an object or rather a json parser. I was able to get the SqlResult's metadata as
List(MetaDataItem(ColumnName(x),false, java.lang.Integer), MetaDataItem(ColumnName(y),.....
Thanks
I'm trying to insert JSON into a Postgresql column who's data type is JSON, but I'm having trouble finding how I can do this. This is as far as I've gotten but it's not correct because it just overwrites it every time, instead of adding a new key pair.
I'm using pg-promise node module to perform these queries. Here's what I have so far:
db.query("UPDATE meditation_database SET completed=$1 WHERE user_id=$2", [{myVar : true}, user_id]);
Also 'myVar' should be updated to the variable value, but instead it treats it as a string. How can I get the actual value of 'myVar' instead of it being treated literally.
Thanks,
I'm trying to insert JSON into a Postgresql column who's data type is JSON, but I'm having trouble finding how I can do this.
By executing this:
db.query("INSERT INTO meditation_database(completed, user_id) VALUES($1, $2)",
[{myVar : true}, user_id]);
Also 'myVar' should be updated to the variable value, but instead it treats it as a string. How can I get the actual value of 'myVar' instead of it being treated literally.
myVar is serialized into JSON as a string, that's the proper JSON format for property names, and is the only format that PostgreSQL will accept.
This is as far as I've gotten but it's not correct because it just overwrites it every time, instead of adding a new key pair.
If you are asking how to update JSON in PostgreSQL, this question has been answered previously, and in great detail: How do I modify fields inside the new PostgreSQL JSON datatype?
Take for example
{'name':'Ruth', 'age':28, 'city':'madrid'}
This JSON gets indexed and is stored as a string. Is there a way to return this field value as JSON in the response?
Add &wt=json at the end of your query to get Solr to return a response in JSON format. XML is default. For example:
http://localhost:8080/solr/select?q=*&wt=json
I came across this which did what I want
https://issues.apache.org/jira/browse/SOLR-1690