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
Related
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.
I have a use case to store dynamic JSON objects in a column in Big Query. The schema of the object is dynamically generated by the source and not known beforehand. The number of key value pairs in the object can differ as well, as shown below.
Example JSON objects:
{"Fruit":"Apple","Price":"10","Sale":"No"}
{"Movie":"Avatar","Genre":"Fiction"}
I could achieve the same in Hive by defining the column as map<string, string> object and I could query the data in the column like col_name["Fruit"] or col_name["Movie"] for that corresponding row.
Is there an equivalent way of above usage in Big Query? I came across 'RECORD' data type but the schema needs to be same for all the objects in the column.
Note: Storing the column as string datatype is not an option as the users need to query the data on the keys directly without parsing after retrieving the data.
Storing the data as a JSON string seems to be the only way to implement your requirement, at the moment. As a workaround, you can create a JavaScript UDF that parses the JSON string and extracts the necessary information. Below is a sample UDF.
CREATE TEMP FUNCTION extract_from_json(json STRING, key STRING)
RETURNS STRING
LANGUAGE js AS """
const obj = JSON.parse(json);
return obj[key];
""";
WITH json_table AS (
SELECT '{"Fruit":"Apple","Price":"10","Sale":"No"}' json_data UNION ALL
SELECT '{"Movie":"Avatar","Genre":"Fiction"}' json_data
)
SELECT extract_from_json(json_data, 'Movie') AS photos
FROM json_table
You can also check out the newly introduced JSON data type in BigQuery. The data type offers more flexibility when handling JSON data but please note that the data type is still in preview and is not recommended for production. You will have to enroll in this preview. For more information on working with JSON data, refer to this documentation.
I have a JSON array of objects in a MySQL table that I am trying to see if there is a way to query and just pull the data. For example.
JSON Array Object
email_address_dump
[{"value":"a123#yahoo.com","type":"personal"},{"value":"all123#hotmail.com","type":"personal"},{"value":"car_sq5#indeedemail.com","type":"personal"}]
is there a way to query out just the email address? so that the results can be something like this?
a123#yahoo.com, all123#hotmail.com, car_sq5#indeedemail.com
I am not trying to search within the column, I know that with JSON Obtains you can use a where clause, this is more of a JSON Extract.
I was able to solve this by using JSON Extract from MySQL.
json_extract(c.email_address_dump, ''$[*].value') as EmailAddressArray,
I am reading and writing mysql JSON type using JDBC driver and ResultSet. How do I fetch the JSON type from ResultSet as string JSON to be used in java program ?
mysql> set names UTF8MB4; // so that json values can be inserted using jdbc driver otherwise it was complaining of binary type
Java code (pseudo code):
String myjsonString = "{x: y}"; // assume this is proper
conn.getPreparedStatement("insert into col_json values (myjsonString)");
rs = conn.getStatement("select col_json from table_json_test");
String myorig_json = rs.getString("col_json");
myorig_json is coming as a string like this:
"base64:type15:rO0ABXNyABdqYXZhLnV0aWwuTGlua2VkSGFzaE1hcDTATlwQbMD7AgABWgALYWNjZXNzT3JkZXJ4
cgARamF2YS51dGlsLkhhc2hNYXAFB9rBwxZg0QMAAkYACmxvYWRGYWN0b3JJAAl0aHJlc2hvbGR4
cD9AAAAAAAAMdwgAAAAQAAAAAXQABXJlZmlkdAAgZWI3ODkzZWVjMDU5NGNhMjlhYjY1OTJjMjFj
MjIwNWZ4AA=="
expected is "{x: y}"
The JSON data type in MySQL is a special type that's stored internally as a binary object. This is why you can't get the value from the database as a string - the JDBC driver will grab the contents of the binary value and try to read it as a String, giving you the garbage you see.
JDBC drivers don't support the JSON object type at all. Instead, you have two options:
Store your JSON in a TEXT field - then you can use getString to read it, but you'll have to parse it yourself.
Use MySQL's special JSON query language to build your queries, which will mean that your ResultSet contains data read from the JSON and converted for you. For example, having the query:
SELECT col_json->>'$.x' AS x FROM <table>
will give you a ResultSet containing:
+----+
| x |
+----+
| y |
+----+
and you can use resultSet.getString(1) to extract the value.
There are plenty of tutorials around for how to query JSON fields in MySQL. Here's a nice simple one to start you off: http://www.mysqltutorial.org/mysql-json/
I have a JSON field in my model which stores data like this:
{ "old_val": {"status": value1},
"new_val": {"status": value2}
}
Now I want to refine my select query so that the result contains all those tuples whose JSON field has,
["new_val"]["status"] = value2 and ["old_val"]["status"] !=value1
how do I write this query in django. ???
This depends on what JSONField you are using and what database. Some of them just save json to text field. If this is the case when you can't access parts of data in the database and hence can't filter by it. If you are however using PostgreSQL 9.3+ than you can use its JSON support and its operators with extra:
Something.objects.extra(where=["data->'new_val'->>'status' = %s"], params=["foo"])
Note that PostgreSQL 9.4 has more operators than 9.3.
You may also take a look at django-pgjson, it encapsulates the use of some of postgresql json operators into custom lookups (new in Django 1.7):
Something.objects.filter(data__at_new_val__at_status="foo")
jsonField is basically a String. So, you have got to perform the queries that you would perform on any StringField.
needed_objects = YourModel.objects.filter(jsonfield__contains={"status": value2}).exclude(jsonfield__contains={"status": value1})
Hope this does the trick for you.