Accessing a JSON Variable within an HTML Custom Prompt

I want to present some data in a custom HTML prompt. The data is stored in KM in the form of a JSON object. The issue is that I'm trying to refer to a specific object inside the main JSON object using the value of an ID field. And while I've found examples of showing the value of a known, simple KM variable in an HTML prompt (using textarea and an id field), I don't think I've seen the same using JSON variables.

I'm basing my work on Dan Thomas's MACRO: Spotlight KM Variables Updated version 1.1:

I understand that the construct:

_varName = getKMVariable("sspResult", true);
_varValue = getKMVariable(_varName);
document.getElementById("divVarName").textContent = _varName;
document.getElementById("txtValue").value = _varValue;

allows him to write:

where the id of the div and the textarea actually print out the contents of the variables.

I wanted to do the same thing, with the following script:

_expertsList = getKMVariable("jExperts", true)
_expertId = getKMVariable("sspResult", true);
_expertReturned =_expertsList.find(item => == _expertId)
document.getElementById("expertName").textContent = _expertReturned.nom
document.getElementById("expertOrigin").textContent = _expertReturned.origine

where jExperts is a JSON variable that I set in KM (it's properly formatted, and I use Spotlight Search Script, to return the id I want to use.)

The macro breaks because it tells me that "find" is not a function:

Error in 'setupUI':
_expertsList.find is not a function. (In '_expertsList.find(item => == _expertId)', '_expertsList.find' is undefined)

My questions:

  • does _expertsList contain an actual JSON object, or is there another way to pass a JSON object to the script from KM
  • is the .find() function (which I believe was introduced in ECMA 6) not supported in the flavor of JS used by KM
  • is there a simpler way of addressing KM variables and JSON objects in a script

As you can see, I know just enough about these things to be a danger to myself and others ...
Thanks for any pointers

For what it's worth, here's a sample of the JSON data i'm using:
I'm getting the id out of the SSP search, and I want to show the "nom", "origine" fields linked to that id.
{ "id" : "1", "missions" : [
{"client" : "ACME", "intitulé" : "Mission Title 1", "numéro" : "3256-02"}, {"client" : "BigCo", "intitulé" : "Boom boom badaboum", "numéro" : "666-00"}
"nom" : "John Doe", "origine" : "ACME", "société" : "John Doe Consulting"},
{ "id" : "2", "missions" : [
{ "client" : "SmallCo", "intitulé" : "Get the widget", "numéro" : "2224-00" }
"nom" : "Jimmy Bob", "origine" : "This other big company", "société" : "Jimmy Bob and Associates" },
{ "nom" : "Suzie Q", "origine" : "ACME", "société" : "Suzie Q and daughters" }

1 Like

All KMVariables are strings.

You can obtain a JS Object from a JSON string with the standard JSON.parse() method.

Assuming that your JSON string represents an Array, as above.

const jsArray = JSON.parse(strKMVariable)

(and .find is an Array method, so you can then use jsArray.find(...)

(it was failing before because it was applied to a string)