Dictionary literals in Mako expressions - mako

The following throws a syntax error, "unexpected EOF while parsing":
${foo({'bar':'baz'})}
which I guess is from the inner closing curly brace.
This works fine:
${foo(dict(bar='baz'))}
but what's the syntax for using a dictionary literal?

From Brian Rue on the Mako Templates Google Group:
This is a long-outstanding bug; just use dict(). If you need a dictionary
with keys that aren't strings, convert a list of tuples into a dict.
e.g. instead of this:
${foo({1: 'a', 2: 'b'})}
do this:
${foo(dict([(1, 'a'), (2, 'b')]))}

Related

Redshift JSON Parsing

I have some JSON data in Redshift table of type character varying. An example entry is:
[{"value":["*"], "key":"testData"}, {"value":"["GGG"], key: "differentData"}]
I want to return vales based on keys, how can i do this? I'm attempting to do something like
json_extract_path_text(column, 'value') but unfortunately it errors out. Any ideas?
So the first issue is that your string isn't valid JSON. There are mismatched and missing quotes. I think you mean:
[{"value":["*"], "key":"testData"}, {"value":["GGG"], "key": "differentData"}]
I don't know if this is a data issue or a transcription error but these functions won't work unless the json text is valid.
The next thing to consider is that at the top level this json is an array so you will need to use json_extract_array_element_text() function to pick up an element of the array. For example:
json_extract_array_element_text('json string', 0)
So putting this together we can extract the first "value" with (untested):
json_extract_path_text(
json_extract_array_element_text(
'[{"value":["*"], "key":"testData"}, {"value":["GGG"], "key": "differentData"}]', 0
), 'value'
)
Should return the string ["*"].

Parse JSON data in T-SQL [duplicate]

This is driving me nuts, and I don't understand what's wrong with my approach.
I generate a JSON object in SQL like this:
select #output = (
select distinct lngEmpNo, txtFullName
from tblSecret
for json path, root('result'), include_null_values
)
I get a result like this:
{"result":[{"lngEmpNo":696969,"txtFullName":"Clinton, Bill"}]}
ISJSON() confirms that it's valid JSON, and JSON_QUERY(#OUTPUT, '$.result') will return the array [] portion of the JSON object... cool!
BUT, I'm trying to use JSON_QUERY to extract a specific value:
This gets me a NULL value. Why??????? I've tried it with the [0], without the [0], and of course, txtFullName[0]
SELECT JSON_QUERY(#jsonResponse, '$.result[0].txtFullName');
I prefixed with strict, SELECT JSON_QUERY(#jsonResponse, 'strict $.result[0].txtFullName');, and it tells me this:
Msg 13607, Level 16, State 4, Line 29
JSON path is not properly formatted. Unexpected character 't' is found at
position 18.
What am I doing wrong? What is wrong with my structure?
JSON_QUERY will only extract an object or an array. You are trying to extract a single value so, you need to use JSON_VALUE. For example:
SELECT JSON_VALUE(#jsonResponse, '$.result[0].txtFullName');

I need to replace the character \\ of a string and read with JSON_EXTRACT

When saving the JSON object in Mysql with JSON.stringify it puts the character "\" in the string. I am building a VIEW and I need to separate the data with json_extract, for that I used the MySql REPLACE command but the return is null.
EDITED
JSON IN FIELD "DADOS" (LONGTEXT)
{
"pessoal":"[{\"nome\":\"Marie Luiza Novaes\",\"nascimento\":\"1994-06-20\",\"civil\":\"Casado(a)\",\"sexo\":\"F\",\"rg\":\"469326293\",\"cpf\":\"06649073504\"}]",
"contato":[],
"interesse":[],
"adicional":[],
"profissional":[],
"academico":[],
"anotacoes":[],
"extras":"[]"
}
1 - GET NOME
SELECT
json_extract (REPLACE(dados,'\\"','"'), '$.pessoal[0].nome') dados
FROM
cadastro
2 - GET NOME
SELECT
json_extract (REPLACE(dados,'\\',''), '$.pessoal[0].nome') dados
FROM
cadastro
TEST
I see multiple problems with your current approach. First, the JSON literal text in your column appears to be somewhat malformed. The JSON array does not take double quotes, because it is part of the JSON structure. Second, the JSON path syntax you are using is also off. The following exact setup is working for me:
WITH cadastro AS (
SELECT '{"pessoal":[{"nome":"Marie Luiza Novaes","nascimento":"1994-06-20","civil":"Casado(a)","sexo":"F","rg":"469326293","cpf":"06649073504"}],
"contato":[],
"interesse":[],
"adicional":[],
"profissional":[],
"academico":[],
"anotacoes":[],
"extras":[]}' AS dados
)
SELECT
JSON_EXTRACT(dados, '$.pessoal[0].nome') dados
FROM cadastro;
Demo
The output from this query is "Marie Luiza Novaes".

Check and print occurrences of an array of string in a dataset in Python

I want to check if an array of strings occur in a dataset and print those rows where the string array elements occur.
rareTitles = {"Capt", "Col", "Countess", "Don", "Dr", "Jonkheer", "Lady",
"Major", "Mlle", "Mme", "Ms", "Rev", "Sir"}
dataset[rareTitles in (dataset['Title'])]
I am getting following error:
TypeError: unhashable type: 'set'
First of all, I think the comparison should go the other way around - you look for a dataset['Title'], that contains string from rareTitles.
You can use str attribute of a pandas DataSeries, which allows as to use string methods, like contains. As this method accepts also a pattern as a regular expression, you can put as an argument something like 'Capt|Col...'. To join all elements of a set you can use str.join() method.
So the solution would be
dataset[dataset['Title'].str.contains('|'.join(rareTitles))]
Link to documentation: pandas.Series.str.contains

Escape double quotes in json functions mysql

I know it should be simple, but I can't figure out how to escape the double quotes inside mysql query for json manipulation.
I have the following example:
SET #j = '{"4": 1, "0as#x"" : [1, 2, 3, 4], "b": [2, 3]}';
Please pay attention to the double quotes inside the second key: 0as#x"
If i run this query: SELECT JSON_ARRAY_APPEND(#j, '$."0as#x\"', '2');
I get the following error:
Error Code: 3141. Invalid JSON text in argument 1 to function json_array_append: "Missing a colon after a name of object member." at position 16.
All I want is to know how to escape the double quotes inside variable name of json object key.
I also have tried doubling the quotes """, with two backslashes \\"...
Could you please help me?
Thank you!
Later EDIT
In the set statement I escaped the double quotes with \". This is done behind if you use JSON_OBJECT.
In the end I escaped with \\ the double quotes and it worked.
The final code that is working:
SET #j = JSON_OBJECT('4', 1, '0as#x"', '[1, 2, 3, 4]', 'b', '[2, 3]');
SELECT JSON_ARRAY_APPEND(#j, '$."0as#x\\""', 2);
Use the JSON_OBJECT function to build the object, this way:
SET #j = JSON_OBJECT('4', 1, '0as#x"', '[1, 2, 3, 4]', 'b', '[2, 3]');
Then the function (no extra quotes around the key name):
SELECT JSON_ARRAY_APPEND(#j, '$.0as#x"', 2);
Hope it helped.