Skip to main content

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_thematici"en"]:
jsonSnippetKW = jsonSnippetKW +'{ "value":'+ keyword + ',"valueDE": '+ keywords_thematici"de"]"keywords_thematici"en"].index(keyword)]+',"type": "THEMATIC" }'
if keywords_thematici"en"].index(keyword) < (len(keywords_thematici"en"])-1):
jsonSnippetKW = jsonSnippetKW +","
jsonSnippet = jsonSnippete:14]+ jsonSnippetKW +jsonSnippete14:]
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?

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_ene$i],
"valueDE": $keyword_de,
"type": "THEMATIC"
}
]
}

I hope this information helps.


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


Reply