Postupdate a result of a loop to a item


i try to write a result of my loop in a item, i createt for each result in my loop a item.

String test1_title
String test1_year
String test2_title
String test2_year
String test3_title
String test3_year
String test4_title
String test4_year
String test5_title
String test5_year

now i want thats my loop autofills the results with the items name. but i dont know how to do this

my loop

val i=0
while ( i < arLength ) {

    val String  test  = transform("JSONPATH","$.result.movies.[" + i + "].title", json_genre_movies)
    logInfo("rules","Ergebnisse" + test) // i = anzahl des ergebnisses

    //logInfo("rules","loop test end...." + i) // i = anzahl des ergebnisses
    //logInfo("rules","loop test end...." + transform("JSONPATH","$.result.movies.[" + i + "].title",json_genre_movies))
    //logInfo("rules","loop test end...." + transform("JSONPATH","$.result.movies.[" + i + "].year",json_genre_movies))

i found the solution

val i=0
while ( i < arLength ) {
    val movietitle = "test" + i + "_title"
    val movietyear = "test" + i + "_year"
    val moviegenre = "test" + i + "_genre"

    val String genre_movies_title  = transform("JSONPATH","$.result.movies.[" + i + "].title", json_genre_movies)
    val String genre_movies_year  = transform("JSONPATH","$.result.movies.[" + i + "].year", json_genre_movies)
    val String genre_movies_genre  = transform("JSONPATH","$.result.movies.[" + i + "].genre", json_genre_movies)

    if (genre_movies_genre.startsWith("{"))
    {   var String genre_movie2_replace = genre_movies_genre.replace('"','')
        var String genre_movie2_replace_1 = genre_movie2_replace.replace('[','')
        var String genre_movie2_replace_final = genre_movie2_replace_1.replace(']','')

That’s a string variable.

That’s not going to work.
String variables are not Items, string variables do not have a postUpdate() method.

How to access an Item when you have its name in a string variable

If you’re entering your rule via GUI rather than file, you might find this thread useful

1 Like

Thanks for your reply.

I testet the rule above, and IT works well with my Code.but i think you are right, the way you Posted is better.

Thank you

is it not possible to use in a loop a if condition?

my if condition dont work.

val tvshowtitle = ScriptServiceUtil.getItemRegistry.getItem("Item_actor1_tvshow" + i + "_title_search") as GenericItem

    val String genre_tvshows_title  = transform("JSONPATH","$.result.tvshows.[" + i + "].title", json_actor1_tvshows)

if (genre_tvshows_title.startsWith("{"))
    { tvshowtitle.postUpdate("UNDEF") }
    { tvshowtitle.postUpdate(genre_tvshows_title) }