ROKU: Associative Array with multiple values - arrays

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.

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

Flutter : How to get value in Object JSON?

Good healty everyone, I want to ask something about managing data from JSON. I've JSON format like below :
{
"msg": "Success"
"data": [
{
"id": "1",
"notelp": "0000000000",
"user": "no1"
},
{
"id": "2",
"notelp": "1111111111",
"user": "no2"
},
],}
and I want to get value from variable "notelp", I expect output like this {"0000000000", "1111111111",} how to get it ?
I tried this before, but still can't get what I want,
if (response.statusCode == 200) {
final result = json.decode(response.body);
debugPrint('dataTelp: ${result['data']['notelp']}');
}
thank you guys to help me to solve it and stay save.
You can use the forEach or another looping method to get data from a list.
List outPut = [];
List data = response['data'];
data.forEach((element) {
outPut.add(element['notelp']);
});
print(outPut);

How to insert array/list of objects in existing MongoDB document?

I have a mongo collection where docs have been already stored. The structure is of a single doc is something like this:
"_id":ObjectId("55c3043ab165fa6355ec5c9b"),
"address":{
"building":"522",
"coord":[
-73.95171,
40.767461
],
"street":"East 74 Street",
"zipcode":"10021"
}
}
Now I want to update the doc by inserting a new field "persons" with value being a list of objects [{"name":"marcus", "contact":"420"}, {"name":"modiji", "contact":"111"}], so after insertion the above doc should look like this:
"_id":ObjectId("55c3043ab165fa6355ec5c9b"),
"address":{
"building":"522",
"coord":[
-73.95171,
40.767461
],
"street":"East 74 Street",
"zipcode":"10021"
},
"persons":[
{
"name":"marcus",
"contact":"420"
},
{
"name":"modiji",
"contact":"111"
}
]
}
Can anyone please help me with then correct $set syntax? Also, it would be really helpful if anyone can suggest an efficient way to update a key's value, which is a list of objects so that I can push some new objects inside the existing list.
You can use the updateOne command along with $set operator to achieve it.
db.<Collection-Name>.updateOne({
"_id":ObjectId("55c3043ab165fa6355ec5c9b")
}, {
"$set": {
"persons":[
{
"name":"marcus",
"contact":"420"
},
{
"name":"modiji",
"contact":"111"
}
]
}
})
If you want to push additional data into the array, you can use the below command.
db.<Collection-Name>.updateOne({
"_id":ObjectId("55c3043ab165fa6355ec5c9b")
}, {
"$push": {
"persons": {
"name":"sample",
"contact":"1234"
}
}
})
To push multiple arrays of objects in a single command, use the below query
db.<Collection-Name>.updateOne({
"_id":ObjectId("55c3043ab165fa6355ec5c9b")
}, {
"$push": {
"persons": {
"$each": [
{
"name":"sample1",
"contact":"5678"
},
{
"name":"sample2",
"contact":"90123"
}
]
}
}
})

Retrieve information of last element of an array painless-ly in elasticsearch

While updating a doc (using update API), I need to extract a field of last entered element of an array, and then use it while adding a new element to the same array.
For example:
{
"_id": "guid",
"timestamp": "time",
"conversation": [
{
"previousTopic": "A",
"currentTopic": "B",
"score": 80
},
{
"previousTopic": "B",
"currentTopic": "C",
"score": 85
}
]
}
Now, while inserting a new element to this array using the update API, first extract the "currentTopic" field of the last element (which is C in this case) and then insert this as "previousTopic" of the next element.
I know how to use basic update API which would insert a new element to the array of the document:
POST test/_doc/{doc_id}/_update
{
"script" : {
"source": "ctx._source.communication.add(params.newcom)",
"lang": "painless",
"params" : {
"newcomm" : {
"previousTopic": {extracted value will go here}
"currentTopic" : "D"
"score" : 89 }
}
}
}
I was able to do this flawlessly using painless scripting.
POST test/_doc/nbsadmnsabdmas/_update
{
"script" : {
"lang": "painless",
"source" :
"""
// find the length of array
def count = ctx._source.conversation.length;
// get to the last element and extract
def temp = ctx._source.conversation[count-1].currentTopic;
// add a new element to the array
ctx._source.communication.add(['previousTopic':temp,'currentTopic':'D',
'score':90]);
"""
}
}

Typescript filter array of object by another array of strings

i stuck at my typescript filter function.
I have an array of objects:
[
{
"id": 12345,
"title": "Some title",
"complexity": [
{
"slug": "1" // my search term
"name": "easy"
}, {
"slug": "2" // my search term
"name": "middle"
},
{...}
And i have a array of strings with the allowed complexity:
public allowedComplexityArray:Array<string> = ["1"];
My Task: I only want to show objects with the allowed complexity of "1".
But somehow my function dont work and i dont know why:
allowedMeals = meals.filter(meal => {
return meal.complexity.every(complexityObj => that.allowedComplexityArray.indexOf(complexityObj.slug) > -1)
});
try:
let allowedMeals = data.filter(meal => {
return meal.complexity.findIndex(complexityObj =>
allowedComplexityArray.findIndex(m => m == complexityObj.slug) > -1) > -1
});
I'm using findIndex instead of filter in the return clause so it does not need to scan the whole array every time.

Resources