Unable to Parse JSON object with double quotes - snowflake-cloud-data-platform

I am unable to parse nested JSON with in double quotes here is sample object. Flatten do not getting "RawRequest" key due it quoted in double quotes although its valid json:
"Business ID":"Sajidbutta",
"Acout":"Saji_12"
"Report": {
"ReportPDF": {
"RawRequest": "{ \"subcode\" : \"35656\", \"Hoppoes\" :\"Hello\" ,\"Hoppoes\":[{\"tsn\" : \"44544545\", \"title\" : \"Owner\"}] }"
}
}
}

You have two issues.
First, you're missing a comma between "Saji_12" and "Report":
"Acout":"Saji_12" "Report":
The second one is that you're missing the escaping \ in front of the " in the JSON string. Consider the following
{
"Business ID": "Sajidbutta",
"Acout": "Saji_12",
"Report": {
"ReportPDF": {
"RawRequest": "{ \"subcode\" : \"35656\", \"Hoppoes\" :\"Hello\" ,\"Hoppoes\":[{\"tsn\" : \"44544545\", \"title\" : \"Owner\"}] }"
}
}
}
If the "RawRequest" is meant to actually be a json, then the following is appropriate (note the lack of quotation marks after the RawRequest key:
{
"Business ID": "Sajidbutta",
"Acout": "Saji_12",
"Report": {
"ReportPDF": {
"RawRequest": {
"subcode": "35656",
"Hoppoes": "Hello",
"Hoppoes": [
{
"tsn": "44544545",
"title": "Owner"
}
]
}
}
}
}
Note also the duplicate key "Hoppoes" :)

Related

Robotframework JSON value iteration

I'm trying to extract the following data:
["Gunpla","Wood Craft"]
from this JSON
{
"hobby": {
"hobbyList": [
{
"hobbyType": "Art",
"hobbyTypeList": [
{
"hobbyText": "Gunpla",
"hobbyIndex": "1",
"hobbyOrigin": "JPN"
}
]
},
{
"hobbyType": "Art",
"hobbyTypeList": [
{
"hobbyText": "Wood Craft" ,
"hobbyIndex": "2",
"hobbyOrigin": "USA"
}
]
}
]
}
}
Using this simple query. I'm having difficulty extracting "HobbyText" from HobbyTypeListArray.
FOR ${ob_list} IN #{json['hobby']['hobbyList']}
log ${ob_list["hobbyTypeList"][]}
END
Tried to query it explicitly using this JSONPath
$.hobby.hobbyList[.hobbyTypeList[.hobbyText
and expected output should be the same as above mentioned. However, after running it on RBF it returns a parse error
Parse error at 1:33 near token . (.)
Declare index and call selected obj.
FOR ${ob_list} IN #{json['hobby']['hobbyList']}
log ${ob_list["hobbyTypeList"][${index}]['hobbyText]'}
END

Merging 2 json files into new json with no duplicates

My dedicated servers are generating 2 laptime arrays and I would like to use a script to merge them into a single, new json file, with duplicate "steamids" removed (and kept grouped together as they still are) and both arrays under a single loggedTimes {} (so I can feed it to a html script that produces laptimes and a leaderboard). In other words, I want the structure to remain.
The first laptime file and the second laptime file go through the following command
jq 'reduce . as $item ({}; . * $item)' laptimes_data_ams.json laptimes_data_kow.json > laptimes.json
to then generate the (badly) merged laptime file.
I can get a file reduced but can't get any further than that. I checked threads by other around here and whenever I try their suggestions the script just refuses to work. Anybody available to lend me a hand in generating a working script to keep this final structure post-merge?
{
"loggedTimes" : {
steamids" : {
"idnumber1" : "name1",
"idnumber2" : "name2"
},
"vehicles" : {
"vehiclenumber1" : {
"laptimes" : {
"idnumber1" : {
"lapTime" : time1,
"logtime" : log1,
"name" : "name 1",
"rank" : rank1,
"refId" : id1,
"vehicleid" : vehiclenumber1,
"wet" : 0
},
"idnumber2" : {
"lapTime" : time2,
"logtime" : log2,
"name" : "name 2",
"rank" : rank2,
"refId" : id2,
"vehicleid" : vehiclenumber1,
"wet" : 0
}
}
}
"vehiclesnumber2" : {
//you get the idea by now
}
}
}
You haven't specified how the merge is to be performed, but one option would be to let the key-value pairs in the second file dominate. In that case, you could write:
jq -n '
input as $one
| input as $two
| ($one + $two)
| .loggedTimes.steamids = ($one.loggedTimes.steamids + $two.loggedTimes.steamids)
' 1.json 2.json
With your input, this produces output from which the following is an extract:
{
"loggedTimes": {
"steamids": {
"76561197960277005": "[DECOCO]koker_SZ",
"76561197960436395": "JOJO",
...
},
"vehicles": {
"-1142039519": {
"lapTimes": {}
},
"-1201605905": {
"lapTimes": {
"76561197984026143": {
"lapTime": 609101,
"logtime": 1606516985,
"name": "Night Dick",
"rank": 1,
"refId": 36032,
"vehicleId": -1201605905,
"wet": 0
}
}
}
...
}
}
}

ElasticSearch - Append to integer array

I am new to ES and but I'm getting the hang of it.
It's a really powerful piece of software, but I have to say that the documentation is really lacking and confusing some times.
Here's my question:
I have an integer array, that looks like this:
"hits_history" : [0,0]
I want to append an integer to that array via an "update_by_query" call, I searched and found this link: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html
which has this example:
POST test/type1/1/_update
{
"script" : {
"inline": "ctx._source.tags.add(params.tag)",
"lang": "painless",
"params" : {
"tag" : "blue"
}
}
}
so I tried:
curl -XPOST 'localhost:9200/example/example/_update_by_query?pretty' -H 'Content-Type: application/json' -d'
{
"script": {
"inline": "ctx._source.hits_history.add(params.hits)",
"params": {"hits": 0}
},
"query": {
"match_all": {}
}
}
'
but it gave me this error:
"ctx._source.hits_history.add(params.hits); ",
" ^---- HERE"
"type" : "script_exception",
"reason" : "runtime error",
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "Unable to find dynamic method [add] with [1] arguments for class [java.lang.Integer]."
So, I looked further and found this: https://www.elastic.co/guide/en/elasticsearch/guide/current/partial-updates.html
which has this example:
We can also use a script to add a new tag to the tags array.
POST /website/blog/1/_update
{
"script" : "ctx._source.tags+=new_tag",
"params" : {
"new_tag" : "search"
}
}
So I tried it:
curl -XPOST 'localhost:9200/example/example/_update_by_query?pretty' -H 'Content-Type: application/json' -d'
{
"script": {
"inline": "ctx._source.hits_history += 0;"
},
"query": {
"match_all": {}
}
}
'
Result:
"type" : "script_exception",
"reason" : "runtime error",
"caused_by" : {
"type" : "class_cast_exception",
"reason" : "Cannot apply [+] operation to types [java.util.ArrayList] and [java.lang.Integer]."
So, how can I append items to the arrayList? Is there a more up-to-date documentation I should look into?
What I wanted to do was simply something like this:
ctx._source.hits_history.add(ctx._source.today_hits);
ctx._source.today_hits = 0;
Thank you
You should store first value as array (containing one value).
Then you can use add() method.
POST /website/blog/1/_update
{
"script" : "if (ctx._source.containsKey('tags')) { ctx._source.tags.add('next') } else { ctx._source.tags = ['first'] }"
}

ElasticSearch - why ScriptDocValues returns unique values?

I have simple mapping - one string field and one string[] field.
The array of strings contains duplicate values, and I get those duplicate values in query:
{ "query" : { "term" : {"id" : "579a252585b8c5c428fa0a3c"} } }
Returns a single valid hit:
{
"id" : "579a252585b8c5c428fa0a3c",
"touches" : [ "5639abfb5cba47087e8b4571", "5639abfb5cba47087e8b4571", "5639abfb5cba47087e8b4571", "5639abfb5cba47087e8b457b", "5639abfb5cba47087e8b457b"
}
But in metric script aggregation:
"aggs": {
"path": {
"scripted_metric": {
"map_script": "_agg['result'] = doc['touches'].values"
}
}
}
retuns
"aggregations" : {
"path" : {
"value" : [ { }, {
"result" : [ "5639abfb5cba47087e8b4571", "5639abfb5cba47087e8b457b" ]
}, { }, { }, { } ]
}
}
that element is org.elasticsearch.index.fielddata.ScriptDocValues$Strings, casting it toString() returns a json-encoded 2-element array.
So, the question:
Why does ScriptDocValues$Strings return only unique array values and how to get the initial array in script aggregation?
Thanks.
UPD
I found that for numerical values (in particular floats) everything works perfect.

ROKU: Associative Array with multiple values

I have a 2 part question that I am hoping someone can help me with.
I am trying to figure out how to pass an array of years from my server to my ROKU code. It is going to be a variable number of years, meaning there won't always be the same number of elements in the array.
What I need to figure out is how to set up the array, and then how to parse it in BrightScript.
Here is an example of what I have so far. I have the count, but still need to add all the years.
The list of years is 1998,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014
{
"Items" : [
{
"name": "count",
"value": "13"
}
]}
Once it's received in my ROKU code, I will need to loop over the years, and display them, one per line. (I already have the display part done. I just need help knowing how to get at the array elements.)
Since there is so little useful documentation out there for BrightScript arrays, I'm hoping someone with more experience will be able to answer this, and that it will help someone else in the future.
You don't need to do any of that. Just send the JSON array and Roku will parse it:
BrightScript Debugger> myJSON = "[1998,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014]"
BrightScript Debugger> ? type(myJSON)
String
BrightScript Debugger> arr = parseJSON(myJSON)
BrightScript Debugger> ? type(arr)
roArray
It's perfectly valid to have array as top-level element of the json text ("A JSON text is a serialized object or array." per RFC 4627). You can also do it with a dummy object (but why?):
arr = parseJSON(" {"years": [1998, 2003]} ").years
Here is how I solved it:
Array:
{
"Items" : [
{ "value": "2014" } ,
{ "value": "2013" } ,
{ "value": "2012" } ,
{ "value": "2011" } ,
{ "value": "2010" } ,
{ "value": "2009" } ,
{ "value": "2008" } ,
{ "value": "2007" } ,
{ "value": "2006" } ,
{ "value": "2005" } ,
{ "value": "2004" } ,
{ "value": "2003" } ,
{ "value": "1998" }
]
}
BrightScript code to parse it:
arr = CreateObject("roArray",json.Items.count(),false)
for each item in json.Items
thisItem = {
value: item.value
}
arr.push(thisItem)
end for
return arr
I hope this helps someone else in the future.

Resources