JSON NXSL

Started by MarcusH, August 26, 2022, 04:16:54 PM

Previous topic - Next topic

MarcusH

Hi,

Working on integration NetXMS with GLPI using RestAPI and web service definitions. Everything is working except i can not get a handle arround the JSON response.

Is there no support for JSON arrays in NXSL functions?

For example i have this response and i want to loop these values and count total


[
   {
      "id": 178
   },
   {
      "id": 179
   },
   {
      "id": 181
   }
]

MarcusH

Almost always when you post a question you find an answer shortly after. I Solved my counting issue by using ->replace("[", "")->replace("]", "")->split(",")
With that i get an array with a JSON in each that i can loop through.

Filipp Sudanov

Yes, there is json support in nxsl, there is JsonParse() which would produce JsonObject or JsonArray. Support for jsons which have array at the root level (which is the case in your example) was added just recently, it will be available in next patch release. So after that release is out you could do something like this:

s = """[
   {
      "id": 178
   },
   {
      "id": 179
   },
   {
      "id": 181
   }
]""";
       
p = JsonParse(s);
println(classof(p));       
count = 0;
for (v: p->values) {
  println(classof(v));
  count = count + v->get("id");
}
println(count);


which would produce the following:
Result:

JsonArray
JsonObject
JsonObject
JsonObject
538


*** FINISHED ***

MarcusH

Wonderful, looking forward to the release.

On a sidenote is support for Bearer authentication with token planned for implementation soon on webservice?

Filipp Sudanov

Try putting the token into password field for Bearer authentication. Pls give us an update, if this works.

MarcusH

If use Bearer auth i get "OAuth 2.0 Bearer Access Token not implemented" in web service errorMessage.

For info running 4.1.420 on both server and agent

Victor Kirhenshtein

This message indicates that agent was built with libcurl without bearer authentication support. On what platform you run agent and how did you install it?

MarcusH

Agent is running on Windows, installed with official package

Filipp Sudanov

Looks like it's missing in Windows builds. I've created an issue for this, so one day developers will have a look.

Meanwhile you can put netxms agent on any linux machine and configure it as a proxy - token support should be working on Linux.