Extract json data string with applescript - json

I have almost the same problem.
I'm using the following syntax:
set LINK to ("https://XXXXXXXXXXXXXXXXXXXX.com")
set api to ("XXXXXXXXXXXX")
set secret to ("XXXXXXXXXXX")
set RR to do shell script "curl -u '" & api & ":" & secret & "' https:/XXXXXXXurl='" & LINK & "'"
tell application "JSON Helper"
set RRR to (read JSON from RR)
end tell
and I get this :
{results:{{tagging_id:missing value,
image:"https://XXXXXXXXXXXXXXXXX.com",
tags:{{tag:"symbol", confidence:26.962820053101}, {tag:"art",
confidence:26.631685256958}, {tag:"graphic",
confidence:26.29425239563}, {tag:"design",
confidence:25.914730072022}, {tag:"icon", confidence:21.400810241699},
{tag:"cartoon", confidence:20.534477233887}, {tag:"drawing",
confidence:18.931232452393}, {tag:"decoration",
confidence:18.834970474243}, {tag:"silhouette",
confidence:17.404066085815}, {tag:"shape",
confidence:14.910034179688}, {tag:"element",
confidence:14.898452758789}, {tag:"sign", confidence:14.306094169617},
{tag:"ornament", confidence:13.806978225708}, {tag:"human",
confidence:13.510297775269}, {tag:"black",
confidence:13.444707870483}, {tag:"logo", confidence:13.409428596497},
{tag:"pattern", confidence:13.006916999817}, {tag:"retro",
confidence:12.30048751831}, {tag:"star", confidence:11.705702781677},
{tag:"color", confidence:11.694045066834}, {tag:"card",
confidence:11.57892036438}, {tag:"gold", confidence:11.519770622253},
{tag:"banner", confidence:11.03851890564}, {tag:"heat",
confidence:10.58930683136}, {tag:"blaze", confidence:10.454214096069},
{tag:"circle", confidence:10.329438209534}, {tag:"power",
confidence:10.084959030151}, {tag:"decorative",
confidence:10.032000541687}, {tag:"bright",
confidence:10.019249916077}, {tag:"orange", confidence:9.9829454422},
{tag:"concepts", confidence:9.782785415649}, {tag:"style",
confidence:9.65638256073}, {tag:"outline", confidence:9.480903625488},
{tag:"character", confidence:9.435729026794}, {tag:"floral",
confidence:9.367718696594}, {tag:"flower", confidence:9.236577987671},
{tag:"heraldry", confidence:9.019858360291}, {tag:"curve",
confidence:8.756514549255}, {tag:"love", confidence:8.692363739014},
{tag:"colorful", confidence:8.608165740967}, {tag:"leaf",
confidence:8.568987846375}, {tag:"3d", confidence:8.527961730957},
{tag:"fire", confidence:8.443150520325}, {tag:"hand",
confidence:8.4037733078}, {tag:"clip art", confidence:8.348271369934},
{tag:"heart", confidence:8.291186332703}, {tag:"backdrop",
confidence:8.250628471375}, {tag:"figure", confidence:8.186657905579},
{tag:"person", confidence:8.040968894958}, {tag:"yellow",
confidence:7.957674980164}, {tag:"colors", confidence:7.941210269928},
{tag:"frame", confidence:7.811089992523}, {tag:"golden",
confidence:7.745839118958}, {tag:"wallpaper",
confidence:7.666860580444}, {tag:"head", confidence:7.567168235779},
{tag:"paisley", confidence:7.478963375092}, {tag:"vintage",
confidence:7.451002120972}, {tag:"light", confidence:7.361023902893},
{tag:"ornate", confidence:7.328865528107}, {tag:"fantasy",
confidence:7.22393655777}, {tag:"cute", confidence:7.183915138245},
{tag:"holiday", confidence:7.175530433655}, {tag:"science",
confidence:7.122048377991}, {tag:"animal", confidence:7.087336540222},
{tag:"spring", confidence:7.072511672974}, {tag:"medical",
confidence:7.06857919693}}}}}
My problem is : I'm a newbie and I just want to get tags that have a confidence greater than 12. I would like to get something like this :
symbol,art,graphic,design,icon,cartoon,drawing,decoration,[...]retro
What should I add at the end of my syntax ?
Thx for your help!

Please try this, I added comments to the lines, the result is in the variable filteredTags which is a list of strings.
set resultDict to {results:{{tagging_id:missing value, image:"https://XXXXXXXXXXXXXXXXX.com", tags:{{tag:"symbol", confidence:26.962820053101}, {tag:"art", confidence:26.631685256958}, {tag:"graphic", confidence:26.29425239563}, {tag:"design", confidence:25.914730072022}, {tag:"icon", confidence:21.400810241699}, {tag:"cartoon", confidence:20.534477233887}, {tag:"drawing", confidence:18.931232452393}, {tag:"decoration", confidence:18.834970474243}, {tag:"silhouette", confidence:17.404066085815}, {tag:"shape", confidence:14.910034179688}, {tag:"element", confidence:14.898452758789}, {tag:"sign", confidence:14.306094169617}, {tag:"ornament", confidence:13.806978225708}, {tag:"human", confidence:13.510297775269}, {tag:"black", confidence:13.444707870483}, {tag:"logo", confidence:13.409428596497}, {tag:"pattern", confidence:13.006916999817}, {tag:"retro", confidence:12.30048751831}, {tag:"star", confidence:11.705702781677}, {tag:"color", confidence:11.694045066834}, {tag:"card", confidence:11.57892036438}, {tag:"gold", confidence:11.519770622253}, {tag:"banner", confidence:11.03851890564}, {tag:"heat", confidence:10.58930683136}, {tag:"blaze", confidence:10.454214096069}, {tag:"circle", confidence:10.329438209534}, {tag:"power", confidence:10.084959030151}, {tag:"decorative", confidence:10.032000541687}, {tag:"bright", confidence:10.019249916077}, {tag:"orange", confidence:9.9829454422}, {tag:"concepts", confidence:9.782785415649}, {tag:"style", confidence:9.65638256073}, {tag:"outline", confidence:9.480903625488}, {tag:"character", confidence:9.435729026794}, {tag:"floral", confidence:9.367718696594}, {tag:"flower", confidence:9.236577987671}, {tag:"heraldry", confidence:9.019858360291}, {tag:"curve", confidence:8.756514549255}, {tag:"love", confidence:8.692363739014}, {tag:"colorful", confidence:8.608165740967}, {tag:"leaf", confidence:8.568987846375}, {tag:"3d", confidence:8.527961730957}, {tag:"fire", confidence:8.443150520325}, {tag:"hand", confidence:8.4037733078}, {tag:"clip art", confidence:8.348271369934}, {tag:"heart", confidence:8.291186332703}, {tag:"backdrop", confidence:8.250628471375}, {tag:"figure", confidence:8.186657905579}, {tag:"person", confidence:8.040968894958}, {tag:"yellow", confidence:7.957674980164}, {tag:"colors", confidence:7.941210269928}, {tag:"frame", confidence:7.811089992523}, {tag:"golden", confidence:7.745839118958}, {tag:"wallpaper", confidence:7.666860580444}, {tag:"head", confidence:7.567168235779}, {tag:"paisley", confidence:7.478963375092}, {tag:"vintage", confidence:7.451002120972}, {tag:"light", confidence:7.361023902893}, {tag:"ornate", confidence:7.328865528107}, {tag:"fantasy", confidence:7.22393655777}, {tag:"cute", confidence:7.183915138245}, {tag:"holiday", confidence:7.175530433655}, {tag:"science", confidence:7.122048377991}, {tag:"animal", confidence:7.087336540222}, {tag:"spring", confidence:7.072511672974}, {tag:"medical", confidence:7.06857919693}}}}}
-- get the value for key `results`
set theResults to resultDict's results
-- check if the array is not empty
if (count theResults) = 0 then return
-- get the value for key `tags` of the first item of `theResults`
set theTags to theResults's first item's tags
-- create the list for the result
set filteredTags to {}
-- in the repeat loop get the value for key `confidence` and
-- add the tag to the result list if the value > 12
repeat with aTag in theTags
if aTag's confidence > 12.0 then set end of filteredTags to aTag's tag
end repeat

Related

Stuck trying to display info on a CLI from an API that uses a hash and a nested hash

Trying to display second level information about characters from this Futurama API.
Currently using this code to get information:
def self.character
uri = URI.parse(URL)
response = Net::HTTP.get_response(uri)
data = JSON.parse(response.body)
data.each do |c|
Character.new(c["name"], c["gender"], c["species"], c["homePlanet"], c["occupation"], c["info"], c["sayings"])
end
end
I'm then stuck either returning (gender and species) from the nested hash (if character id > 8) or the original hash (character id < 8) when using this code:
def character_details(character)
puts "Name: #{character.name["first"]} #{character.name["middle"]} #{character.name["last"]}"
puts "Species: #{character.info["species"]}"
puts "Occupation: #{character.homePlanet}"
puts "Gender: #{character.info["gender"]}"
puts "Quotes:"
character.sayings.each_with_index do |s, i|
iplusone = i + 1
puts "#{iplusone}. #{s} "
end
end
Not sure where or what logic to use to get the correct information to display.
Maybe you have a problem when save c['info] in Character.new(c["name"], c["gender"], c["species"], c["homePlanet"], c["occupation"], c["info"], c["sayings"])
I'm running your code and I see info does not exist in the response of API, the gender should be accessed in character.gender
irb(main):037:0> character.gender
=> "Male"
irb(main):039:0> character.species
=> "Human"
I don't understand this comment: (if character id > 8) or the original hash (character id < 8) Can you explain us what u need do?

Double Quotes in temporary JSON variable on MySQL using R

I have a table in MYSQL that contains the user interactions with a Web Page, I needed to extract the rows for the users where the date of that interaction is lower than a certain benchmark date and that benchmark date is different for each customer (I extract that date from a different database).
My approach was to set a json variable in which the key is a user and the value is the benchmark date, and used it in the query to extract the intended fields.
Example in R:
#MainDF contains the user and the benchmark date from a different database
json_str <- mapply(function(uid, bench_date){
paste0(
'{','"',cust,'"', ':', '"', bench_date, '"','}'
)
}, MainDF[, 'uid'],
MainDF[, 'date']
)
json_str <- paste0("'", '[', paste0(json_str , collapse = ','), ']', "'")
temp_var <- paste('set #test=', json_str)
The intention was to make temp_var to be like:
set #test= '{"0001":"2010-05-05",
"0012":"2015-05-05",
"0101":"2018-07-20"}'
but it actually looks like :
set #test= '{\"0001\":\"2010-05-05\",
\"0012\":\"2015-05-05\",
\"0101\":\"2018-07-20\"}'
then create the main query:
main_Q <- "select user_id, date
from interaction
where 1=1
and json_contains(json_keys(#test), concat('\"',user_id,'\"')) = 1
and date <= json_unquote(json_extract(#test,
concat('$.','\"',user_id, '\"')
)
)
"
For the execution, first, set the temporal variable and then execute the main query
dbSendQuery(connection, temp_var)
resp <- dbSendQuery(connection, main_Q )
target_df <- fetch(resp, n=-1)
dbClearResult(resp )
When I test a fraction of it in a SQL IDE it does works. However, in R it doesn't return anything.
I think that the issue is that R escape the double quotes in temp_var and SQL end up reading
set #test= '{\"0001\":\"2010-05-05\",
\"0012\":\"2015-05-05\",
\"0101\":\"2018-07-20\"}'
which is not won't work.
For example if I execute:
set #test= '{"0001":"2010-05-05",
"0012":"2015-05-05",
"0101":"2018-07-20"}'
select json_keys(#test)
it will return an array with the keys, but that is not the case with
set #test= '{\"0001\":\"2010-05-05\",
\"0012\":\"2015-05-05\",
\"0101\":\"2018-07-20\"}'
select json_keys(#test)
I am not sure how to solve the issue, but I need double quotes to specify the JSON. Is there any other approach that I should try or a way to make this work?
First, I think it is generally better to use a well-known library/package for converting to/from JSON, for several reasons.
This gives you a string that you should be able to place just about anywhere.
json_str <- jsonlite::toJSON(setNames(as.list(MainDF$date), MainDF$uid), auto_unbox=TRUE)
json_str
# {"0001":"2010-05-05","0012":"2015-05-05","0101":"2018-07-20"}
And while looking at the object on the R console will give the escaped-doublequotes,
as.character(json_str)
# [1] "{\"0001\":\"2010-05-05\",\"0012\":\"2015-05-05\",\"0101\":\"2018-07-20\"}"
that is merely R's representation (shows all strings within double-quotes, and therefore needs to escape any double-quotes within the string).
Adding it into some script should be straight-forward:
cat(paste('set #test=', sQuote(json_str)), '\n')
# set #test= '{"0001":"2010-05-05","0012":"2015-05-05","0101":"2018-07-20"}'
I'm assuming that having each on its own row is not critical. If it is, and indentation is important, perhaps this is more your style:
spaces <- strrep(' ', 2+nchar('set #test = '))
cat(paste0('set #test = ', sQuote(gsub(",", paste0(",\n", spaces), json_str))), '\n')
# set #test = '{"0001":"2010-05-05",
# "0012":"2015-05-05",
# "0101":"2018-07-20"}'
Data:
MainDF <- read.csv(stringsAsFactors=FALSE, colClasses='character', text='
uid,date
0001,2010-05-05
0012,2015-05-05
0101,2018-07-20')

Querying cassandra error no viable alternative at input 'ALLOW'

I'm trying to run a query on Cassandra through spark.
When running this command:
val test = sc.cassandraTable[Person](keyspace,table)
.where("name=?","Jane").collect
I get the appropriate output for the query.
When I try to use the where statement to enter the query as a whole string I get an error.
I receive the query as a json:
{"clause": " name = 'Jane' "}
then turn it into a string.
When running
val query = (json \ "clause").get.as[String]
//turns json value into a string
val test = sc.cassandraTable[Person](keyspace,table)
.where(query).collect
I get the following error:
java.io.IOException: Exception during preparation of SELECT "uuid", "person", "age" FROM "test"."users" WHERE token("uuid") > ? AND token("uuid") <= ? AND name = Jane ALLOW FILTERING: line 1:232 no viable alternative at input 'ALLOW' (...<= ? AND name = [Jane] ALLOW...)
at com.datastax.spark.connector.rdd.CassandraTableScanRDD.createStatement(CassandraTableScanRDD.scala:288)
at com.datastax.spark.connector.rdd.CassandraTableScanRDD.com$datastax$spark$connector$rdd$CassandraTableScanRDD$$fetchTokenRange(CassandraTableScanRDD.scala:302)
at com.datastax.spark.connector.rdd.CassandraTableScanRDD$$anonfun$18.apply(CassandraTableScanRDD.scala:328)
at com.datastax.spark.connector.rdd.CassandraTableScanRDD$$anonfun$18.apply(CassandraTableScanRDD.scala:328)
at scala.collection.Iterator$$anon$12.nextCur(Iterator.scala:434)
at scala.collection.Iterator$$anon$12.hasNext(Iterator.scala:440)
at com.datastax.spark.connector.util.CountingIterator.hasNext(CountingIterator.scala:12)
at scala.collection.Iterator$class.foreach(Iterator.scala:893)
at com.datastax.spark.connector.util.CountingIterator.foreach(CountingIterator.scala:4)
at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:59)
I suspect that when I turn the json value " name = 'Jane' " into a string, I lose the single quotes hence I get " name = Jane " which of course raises an error. I tried escaping the single quotes with \ and with a second pair of single quotes around the name Jane {"clause": " name = ''Jane'' "}. It doesn't solve the issue.
Edit: After further testing it's definitely the json that loses the single quotes and CQL needs them to perform the query. Can anyone suggest a way to escape/save the presence of the single quotes? I tried escaping with \ double single quotes '' . Is there a way to use JSON to provide proper whole CQL statements?
Please use Unicode character \u0027.

vimscript: commands that work in mappings, but not in functions

How can I rewrite these 2 commands, which work fine in a mapping, so that they will work in a function?
:if has_key(glos,#g)==1<cr>:let #j=eval('glos.'.#g)<cr>
The function concerned is executed by vim without comment, but #j remains unchanged, as if they had failed, but no message/error is generated.
Here is the complete code involved, the command that loads the dictionary, the function that does not work, and the mapping from that function that does.
" read the glossary into the dictionary, glos
let glos=eval(join(readfile("glossary.dict")))
" 2click item of interest and this will
" send image filepath to xv
" if item all-caps find same at start of its line
" If capitalized at eol find same at start of its line
" if all lower-case at eol find next occurrence of same
" look lower-case or capitalized word up in glossary.txt
" find _\d\+ (page no.) alone on its line in text
com! F call F()
function! F()
normal "ayiw"cyE"by$
let #c=substitute(#c,"[,.?':;!]\+","","g")
if #c=~'images\/ss\d\d\d*'
let #i='!display -geometry +0+0 '.#c.' &'
pkill display
#i
elseif #c==toupper(#c)
let #n=search('^'.#c,'sw')
elseif #c!=#b
let #f=3
let #g=tolower(#c)
while #f>0
try
let #j=eval('glos.'.#g)
catch
let #f=#f-1
let #g=strpart(#g,0,strlen(#g)-1)
continue
endtry
break
endwh
if #f>0
let #h=substitute(#j," glosimgs.*",'','')
if #h!=#j
let #i='!xv -geometry +0+380 '.substitute(#j,'^.\{-}\( glosimgs.*\)$','\1','').' &'
!pkill xv
#i
endif
echo #h
else
echo 'No matching entry for '.#c
endif
elseif #c=~'\u\l\+$'
let #n=search('^'.#c,'sw')
elseif #c=~'\l\+$'
norm *
elseif #c=~'^_\w\+$'
let #/='^'.#c.'$'
norm nzz
endif
endfunction
map <silent> <2-LeftMouse> "ayiw"cyE"by$:let #c=substitute(#c,"[,.?':;!]\+","","g")<cr>:if #c=~'images\/ss\d\d\d*'<cr>:let #i='!display -geometry +0+0 '.#c.' &'<cr>:pkill display<cr>:#i<cr>:elseif #c==toupper(#c)<cr>:let #n=search('^'.#c,'sw')<cr>:elseif #c!=#b<cr>:let #f=3<cr>:let #g=tolower(#c)<cr>:while #f>0<cr>:try<cr>:let #j=eval('glos["'.#g.'"]')<cr>:catch<cr>:let #f=#f-1<cr>:let #g=strpart(#g,0,strlen(#g)-1)<cr>:continue<cr>:endtry<cr>:break<cr>:endwh<cr>:if #f>0<cr>:let #h=substitute(#j," glosimgs.*",'','')<cr>:if #h!=#j<cr>:let #i='!xv -geometry +0+380 '.substitute(#j,'^.\{-}\( glosimgs.*\)$','\1','').' &'<cr>:!pkill xv<cr>:#i<cr>:endif<cr><cr<cr>>:echo #h<cr>:else<cr>:echo 'No matching entry for '.#c<cr>:endif<cr>:elseif #c=~'\u\l\+$'<cr>:let #n=search('^'.#c,'sw')<cr>:elseif #c=~'\l\+$'<cr>:norm *<cr>:elseif #c=~'^_\w\+$'<cr>:let #/='^'.#c.'$'<cr>:norm nzz<cr>:endif<cr>
Specifically, I should have written:
:if has_key(**g:**glos,#g)==1:let #j=eval('**g:**glos.'.#g)
:h g: goes straight to the heart of the matter; in a function all references are local to that function, so references to any variable outside the function must be global, by prepending 'g:' to the variable name. As I created the dictionary independent of the function, the function must reference it as a global item.
Of course, if you are not aware of 'g:', it is rather difficult to find that help reference, but that's a frequent problem using help.
And, of course, the ** surrounding g: aren't required, that's what this site gives you in lieu of bolded text, apparently.

A shorter non-repeating alphanumeric code than UUID in MySQL

Is it possible for MySQL database to generate a 5 or 6 digit code comprised of only numbers and letters when I insert a record? If so how?
Just like goo.gl, bit.ly and jsfiddle do it. For exaple:
http://bit.ly/3PKQcJ
http://jsfiddle.net/XzKvP
cZ6ahF, 3t5mM, xGNPN, xswUdS...
So UUID_SHORT() will not work because it returns a value like 23043966240817183
Requirements:
Must be unique (non-repeating)
Can be but not required to be based off of primary key integer value
Must scale (grow by one character when all possible combinations have been used)
Must look random. (item 1234 cannot be BCDE while item 1235 be BCDF)
Must be generated on insert.
Would greatly appreciate code examples.
Try this:
SELECT LEFT(UUID(), 6);
I recommend using Redis for this task, actually. It has all the features that make this task suitable for its use. Foremost, it is very good at searching a big list for a value.
We will create two lists, buffered_ids, and used_ids. A cronjob will run every 5 minutes (or whatever interval you like), which will check the length of buffered_ids and keep it above, say, 5000 in length. When you need to use an id, pop it from buffered_ids and add it to used_ids.
Redis has sets, which are unique items in a collection. Think of it as a hash where the keys are unique and all the values are "true".
Your cronjob, in bash:
log(){ local x=$1 n=2 l=-1;if [ "$2" != "" ];then n=$x;x=$2;fi;while((x));do let l+=1 x/=n;done;echo $l; }
scale=`redis-cli SCARD used_ids`
scale=`log 16 $scale`
scale=$[ scale + 6]
while [ `redis-cli SCARD buffered_ids` -lt 5000 ]; do
uuid=`cat /dev/urandom | tr -cd "[:alnum:]" | head -c ${1:-$scale}`
if [ `redis-cli SISMEMBER used_ids $uuid` == 1]; then
continue
fi
redis-cli SADD buffered_ids $uuid
done
To grab the next uid for use in your application (in pseudocode because you did not specify a language)
$uid = redis('SPOP buffered_ids');
redis('SADD used_ids ' . $uid);
edit actually there's a race condition there. To safely pop a value, add it to used_ids first, then remove it from buffered_ids.
$uid = redis('SRANDMEMBER buffered_ids');
redis('SADD used_ids ' . $uid);
redis('SREM buffered_ids ' . $uid);