I have a set of coordinates that need to be in Geojson format but without the double quotes, i am also limited to select only on this dataset.
this line of code
json_extract_array(st_asgeojson(st_makeline( array_agg(st_geogpoint(locs.lon, locs.lat) order by locs.date))),'$.coordinates') as geo
gives me this
[
"[-8.6359,40.6371716666667]",
"[-8.63589166666667,40.637165]",
"[-8.63589333333333,40.6371583333333]",
"[-8.63589,40.637165]",
"[-8.63588833333333,40.6371716666667]",
"[-8.635885,40.6371766666667]",
"[-8.63588166666667,40.63718]"
]
i need it to be like this instead
[
[-8.6359,40.6371716666667],
[-8.63589166666667,40.637165],
[-8.63589333333333,40.6371583333333],
[-8.63589,40.637165],
[-8.63588833333333,40.6371716666667],
[-8.635885,40.6371766666667],
[-8.63588166666667,40.63718]
]
Ok i found the answer, in case some one else has the same issue.
use json_extract instead of json_array_extract.this will remove the double quotes
json_extract(st_asgeojson(st_makeline( array_agg(st_geogpoint(locs.lon, locs.lat) order by locs.date))),'$.coordinates') as geo
Related
I am trying to build a array from this string and need help with pattern on regexp_extract_all.
Here is my input string contains keyword value pairs
BEGIN
DECLARE p_JSON STRING DEFAULT """
{
"instances": [{
"LT_20MN_SalesContrctCnt": 388.0,
"Pyramid_Index": '',
"MARKET": "'Growth Markets','Europe'",
"SERVICE_DIM": "'S&C','F&M'",
"SG_MD": "'All Service Group'"
}]}
""";
SELECT split(x,":")[OFFSET(0)] as keyword, split(x,":")[OFFSET(1)] keyword_value
FROM unnest(split(REGEXP_REPLACE(JSON_EXTRACT(p_JSON, '$.instances'),r'([\'\"\[\]{}])', ''))) as x
END;
The above SQL is failing at SPLIT due to , with in the data.
All I am trying to do here is build a two columns Keyword and value.
The idea here is if I can extract each row using REGEXP_EXTRACT_ALL with out the last "," then I should be able to split into keyword and keyword_value columns. Btw the names or number of keywords/values are not fixed.
Intended output from REGEXP_EXTRACT_ALL:
"LT_20MN_SalesContrctCnt": 388.0
"Pyramid_Index": ''
"MARKET": "'Growth Markets','Europe'"
"SERVICE_DIM": "'S&C','F&M'"
"SG_MD": "'All Service Group'"
Appreciate if you can suggest a better way to handle this.
Thanks in advance.
Using your sample data, I just added an extra REGEXP_REPLACE to replace ," to #" so we can avoid splitting using ,. See approach below:
SELECT
SPLIT(arr,":")[OFFSET(0)] as keyword,
SPLIT(arr,":")[OFFSET(1)] as keyword_value,
FROM sample_data,
UNNEST(SPLIT(REGEXP_REPLACE(REGEXP_REPLACE(JSON_EXTRACT(p_JSON, '$.instances'),r'[\[\]{}]',''),r',"','#"'),'#')) arr
Output:
With the following SQL:
SELECT
CAST(JSON_VALUE(d.Data,'lax $.realSKosten') AS DECIMAL(18,5)) as ks,
CONVERT(float ,JSON_VALUE(d.Data,'lax $.realAKosten')) as ka
FROM UserEntityData as d
I get this result:
[{"ks":1.23000,"ka":1.230000000000000e+000},
{"ks":2.34500,"ka":2.345000000000000e+000}]
With huge resultsets, a big part of the result consists of zeros. Therefore it would be much better, if the resultset would look like this:
[{"ks":1.23,"ka":1.23},
{"ks":2.345,"ka":2.345}]
As shown, with "AS DECIMAL(18,5)" I can shorten it. But the number of decimals is fixed.
Is there a performant way to tell the SQL-Server to remove all zeros at the end, if there are any? Of course without converting it to varchar and doing any text-manipulation.
Not if you want the value to be treated as a numerical value, no. If you're happy with the values being quoted, however, you could use TRIM.
SELECT TRIM('0' FROM CONVERT(varchar(20),ks)) AS ks,
TRIM('0' FROM CONVERT(varchar(20),ka)) AS ka
FROM (VALUES(1.23000,1.23000),
(2.34500,2.34500))V(ks,ka)
FOR JSON AUTO;
Which results in:
[
{
"ks": "1.23",
"ka": "1.23"
},
{
"ks": "2.345",
"ka": "2.345"
}
]
I have json stored in one of the columns in SQL Server and I need to modify it to remove the square brackets from it. The format is as below. Can't seem to find a good way of doing it.
[ { "Message":"Info: this is some message here.", "Active":true } ]
One way is to do it using below query, but this query is very very slow and I need to run on a very large set of data.
select a.value
from dbo.testjson e
cross apply OPENJSON(e.jsontext) as a
where isjson(e.jsontext) = 1
The only other way I can think of is just doing string manipulation but it can be error prone. Could someone help with this?
Ok, figured it out:
select
json_query(
'[{"Message":"Info: this is some message here.","Active":true}]',
'$[0]'
)
This will return the inner message.
You should add the property name, in this case Message, in order to get only that part. Keep in mind that it's case sensitive. Something like;
select json_value('[{"Message":"Info: this is some message here.","Active":true}]', '$[0].Message')
I am using JSON_EXTRACT() to retrieve the date from json.
I want to get the date without double quotes.
Here is the example of what I am doing :
JSON_EXTRACT(JSON_EXTRACT(events, "$.my_member"), "$.my_Number") as xyz
my_number holds date string as "2016-01-01 11:31:25", I want this without the double quotes.
I tried using timestamp as :
timestamp(JSON_EXTRACT(JSON_EXTRACT(events, "$.my_member"), "$.my_Number"))
but it is returning a null value to xyz.
Thanks.
Try
JSON_EXTRACT_SCALAR(JSON_EXTRACT(events, "$.my_member"), "$.my_Number")
Also, you should be able to further "optimize" your expression by building proper JSON Path and using JSON function only ones. See "hint" below
SELECT
JSON_EXTRACT_SCALAR(
'{"my_member":{"my_Number":"2016-01-01 11:31:25"}}',
"$.my_member.my_Number"
)
See more details and also difference between JSON_EXTRACT_SCALAR and JSON_EXTRACT at JSON functions
Run REPLACE
REPLACE(JSON_EXTRACT(JSON_EXTRACT(events, "$.my_member"), "$.my_Number"),"\"","") as xyz
I tried JSON_EXTRACT_SCALAR in MySQL Workbench but I got Error Code: 1305. FUNCTION manifest.JSON_EXTRACT_SCALAR does not exist
Instead I used JSON_UNQUOTE and that did the trick.
I have a column called 'buffer_time' which contains:
'{"after": {"time": "00:01:00", "is_enabled": true}, "before": {"time": "00:04:00", "is_enabled": true}}'
JSON_UNQUOTE(JSON_EXTRACT(buffer_time, '$.after.time'))
gave me: `
00:01:00
Hope that helps.
I am on Oracle 11gR2.
I am trying to extract the text between '[' and ']' including [].
ex:
select regexp_substr('select userid,username from tablename where user_id=[REQ.UID] and username=[REQD.VP.UNAME]','\[(.*)\]') from dual
Output:
[REQ.UID] and username=[REQD.VP.UNAME]
Output needed:
[REQ.UID][REQD.VP.UNAME]
Please let me know how to get the needed output.
Thanks & Regards,
Bishal
Assuming you are just going to have two occurrences of [] then the following should suffice. The ? in the .*? means that it is non-greedy so that it doesn't gobble up the last ].
select
regexp_replace('select userid,username from tablename where user_id=[REQ.UID] and username=[REQD.VP.UNAME]'
,'.*(\[.*?\]).*(\[.*?\]).*','\1\2')
from dual
;
I'm not an Oracle user, but from quick perusal of the docs, I think this should be close:
REGEXP_REPLACE('select userid,username from tablename where user_id=[REQ.UID] and username=[REQD.VP.UNAME]',
'^[^\[]*(\[[^\]]*\])[^\[]*(\[[^\]]*\])$', '\1 \2')
Which looks much nastier than it is.
Pattern is:
^[^\[]* Capture all characters up to (but not including) the first [
(\[[^\]]*\]) Capture into group 1 anything like [<not "]">]
[^\[]* Capture everything up to (nut not including) the next [
(\[[^\]]*\]) Capture into group 2 anything like [<not "]">], at the end of the string
Then the replacement is simple, just <grp 1> <grp 2>