Escaping Characters in Bigquery json_extract() function - json

when using Google's BigQuery, there's a function that can extract elements from json strings using jsonPath. For example:
SELECT JSON_EXTRACT(data,"$.key.value") AS feature FROM tablename
when the json key itself contains a dot,{"key.value":"value"} It's not clear how to escape that properly.
this jsonpath message board question says that jsonpath itself supports this format
#Test
public void path_with_bracket_notation() throws Exception {
String json = "{\"foo.bar\": {\"key\": \"value\"}}";
Assert.assertEquals("value", JsonPath.read(json, "$.['foo.bar'].key"));
However in bigquery this type of espcaping attempts cause Error: JSONPath parse error errors.

Update, new answer:
BigQuery's JSON_EXTRACT and JSON_EXTRACT_SCALAR functions now support JSON bracket notation in JSONPath, so the following query works:
SELECT JSON_EXTRACT('{"key.value": {"foo": "bar"}}', "$['key.value']")
and returns
{"foo":"bar"}
Old, now outdated answer:
Unfortunatelly BigQuery does not support escaping special characters in json path. The workaround would be to use REPLACE function to convert dots to underscores, i.e.
SELECT
json_extract(
replace('{"key.value":"value"}',
'key.value',
'key_value'),
'$.key_value')

use backtick to escape (it is also used to escape hyphen in project/dataset name)
SELECT JSON_VALUE(json_field.`key.value`) AS feature FROM tablename

Related

Error parsing query: Unexpected character \"'\"" Graph QL

I'm fairly new to GraphQL and I've been trying to call our graphQL server using postman with json format.
{"query":"query{stateQuery{avatar(address:'21638103') {action}}}"}
It always returns Unexpected character "'"" but when I try to use my GraphQL query below. It would query successfully.
query {
stateQuery {
avatar(address: "21638103") {
action
}
}
}
Unexpected character "'" means just what it says. It did not expect a '. Your example query that works uses "21638103" where the one that fails uses '21638103'. You need to use " instead of ' for strings.
GraphQL uses JSON ... JSON specs claims ...
A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes.

Supporting JSON paths in BigQuery

I was wondering if BigQuery has any additional support to JSON paths, as it seems like this is such a common way to work with nested data in BigQuery. For example, as a few years ago it seemed like the answer was: What JsonPath expressions are supported in BigQuery?, i.e., "Use a UDF".
However, it seems like using a path within an array, such as:
`$..Job'
Is such a common operation given BigQuery's repeated field, that about 70% of the times I've tried to use BigQuery's JSON_EXTRACT, I run into the limitation of having to iterate down an array.
Is this ability supported yet in BigQuery, or are there plans to support it, without having to do a UDF? As nice as something like the following works:
CREATE TEMPORARY FUNCTION CUSTOM_JSON_EXTRACT(json STRING, json_path STRING)
RETURNS STRING
LANGUAGE js AS """
try { var parsed = JSON.parse(json);
return JSON.stringify(jsonPath(parsed, json_path));
} catch (e) { return null }
"""
OPTIONS (
library="gs://xx-bq/jsonpath-0.8.0.js"
);
SELECT CUSTOM_JSON_EXTRACT(to_json_string(Occupation), '$..Job'), to_json_string(MovieInfo), json_extract(MovieInfo, '$.Platform') FROM `xx-163219.bqtesting.xx` LIMIT 1000
It ends up taking anywhere between 4-6x longer than a normal JSON_EXTRACT function (2s vs. about 10s). Or, is there something that I'm missing with what you're able to do with JSON objects in BQ?
Currently, the support for JSONPath on BigQuery includes and is limited to $, ., and [], where the latter can be either a child operator or a subscript (array) operator.
Other syntax elements from JSONPath are not supported, but for future reference, there's a public feature request to support complete JSONPath syntax.

Convert doctrine array to JSON

Is there a way to read a column of doctrine type "simply_array" or "array" in json?
My doctrine database is approached from another api and I want to read data from that api. However there is a column of type doctrine array that I want to convert into JSON.
I am unsure if there is a preferred way of doing this or I need to hack my way around it.
Here is an example of what is stored in the database as a doctrine array:
"a:1:{i:0;a:3:{s:3:\u0022day\u0022;i:5;s:4:\u0022time\u0022;s:7:\u0022morning\u0022;s:12:\u0022availability\u0022;N;}}"
That looks like the format of PHP's serialize() function. And the literal double-quotes in the string have been converted to unicode escape sequences.
You could do the following:
Fetch the serialized string
Fix the \u0022 sequences (replace them with ")
unserialize() it to reproduce the array
Convert the array to JSON with json_encode().

Omnifaces Escape JSON Strings

Is there a way to escape text for using in a json expression with Omnifaces:
e.g:
"reviewBody": "#{of:escapeJS(reviewBean.text)}"
This appears to escape single quotes but I want to escape double quotes.
For JSON encoding in EL, use #{of:toJson} instead (introduced in OmniFaces 1.5).
"reviewBody": #{of:toJson(reviewBean.text)}
Note that it already takes care of doublequotes. This function not only deals with CharSequence (String and friends), but also properly deals with Number, Boolean, Date, Object[], Collection, Map and even true Javabeans. Basically, you can encode "everything" with it without worrying about the format and quoting. See also the showcase, javadoc and source code.

How to convert string to BSON using MongoDB C++ driver?

Similar to toString is there a way we can convert a string to BSON object? I need to remove a document using C++ driver the the remove function expects the query to have BSON object.
Use the fromjson method found here:
http://api.mongodb.org/cplusplus/1.5.4/namespacemongo.html#a4f542be0d0f9bad2d8cb32c3436026c2
BSONObj mongo::fromjson ( const string & str )
Create a BSONObj from a JSON <http://www.json.org> string.
In addition to the JSON extensions extensions described here
http://mongodb.onconfluence.com/display/DOCS/Mongo+Extended+JSON, this function accepts
certain unquoted field names and allows single quotes to optionally be used when
specifying field names and string values instead of double quotes. JSON unicode escape
sequences (of the form ) are converted to utf8.
Exceptions:
MsgAssertionException if parsing fails. The message included with this assertion includes
a rough indication of where parsing failed.