Skip to main content
Question

How to loop through to corresponding lists using xquery

  • August 23, 2024
  • 2 replies
  • 198 views

erikboehm
Contributor
Forum|alt.badge.img+3

I have a JSON template that I want to fill with values from two corresponding lists using xquery.

The two lists look like this:

keywords_thematic_en = ["test", "trial","error","datapool","API","REST"]
keywords_thematic_de = ["Test", "Versuch","Fehler","Datenpool","API","REST"]
keywords_thematic = {"en":keywords_thematic_en,"de":keywords_thematic_de}

 

The JSON template I want to fill looks like this:

{
"keywords": [
  { "value":keywords_thematic_en,"valueDE": keywords_thematic_de,"type": "THEMATIC" }
]
}

 

In python, this looks like this:

keywords_thematic_en = ["test", "trial","error","datapool","API","REST"]
keywords_thematic_de = ["Test", "Versuch","Fehler","Datenpool","API","REST"]

keywords_thematic = {"en":keywords_thematic_en,"de":keywords_thematic_de}

jsonSnippet = '{"keywords": []}'
jsonSnippetKW=''
for keyword in keywords_thematic["en"]:
    jsonSnippetKW = jsonSnippetKW +'{ "value":'+ keyword + ',"valueDE": '+ keywords_thematic["de"][keywords_thematic["en"].index(keyword)]+',"type": "THEMATIC" }'
    if keywords_thematic["en"].index(keyword) < (len(keywords_thematic["en"])-1):
        jsonSnippetKW = jsonSnippetKW +","
jsonSnippet = jsonSnippet[:14]+ jsonSnippetKW +jsonSnippet[14:]
print(jsonSnippet)

As a result, I get what I want:

{
"keywords": [
{ "value":test,"valueDE": Test,"type": "THEMATIC" },
{ "value":trial,"valueDE": Versuch,"type": "THEMATIC" },
{ "value":error,"valueDE": Fehler,"type": "THEMATIC" },
{ "value":datapool,"valueDE": Datenpool,"type": "THEMATIC" },
{ "value":API,"valueDE": API,"type": "THEMATIC" },
{ "value":REST,"valueDE": REST,"type": "THEMATIC" }
]
}

 

Trying the same using the JSONTemplater and xquery, I only get so far for looping through one list:

for $Keyword in fme:get-list-attribute("KW_themaic.KW_de{}")
   return
   {
      "value": $Keyword,
      "valueDE": $Keyword,
      "type": "THEMATIC"
    }

 

How do I set up a for loop with xquery, looping through the two lists and insert the corresponding values?

debbiatsafe
Safer
Forum|alt.badge.img+20

Hello @erikboehm 

Assuming the two lists will always have the same number of elements, you can loop through one list and refer to the other using the array index notation. The following is XQuery that demonstrates this approach.

let $keywords_en := fme:get-list-attribute("KW_themaic.KW_en{}")
return {
    "keywords": [
    for $keyword_de at $i in fme:get-list-attribute("KW_themaic.KW_de{}")
    return
       {
          "value": $keywords_en[$i],
          "valueDE": $keyword_de,
          "type": "THEMATIC"
       }
    ]
}

I hope this information helps.


erikboehm
Contributor
Forum|alt.badge.img+3
  • Contributor
  • September 12, 2024

@debbiatsafe thank you very much! That’s the perfect solution!


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings