Create and send JSON in c# server

Hello. I have got an http server in c# which connects to OH2 through http binding. The server should send JSON to openHAB which provesses them as described here. What should the server look like?
Now it is:

HttpListener server = new HttpListener();
server.Prefixes.Add("http://127.0.0.1/");
server.Prefixes.Add("http://localhost/");
server.Start();
HttpListenerContext context = server.GetContext();
HttpListenerResponse response = context.Response;
TextReader tr = new StreamReader("json.txt");
string msg = tr.ReadToEnd();
byte[] buffer = Encoding.UTF8.GetBytes(msg);
response.ContentLength64 = buffer.Length;´
Stream st = response.OutputStream;
st.Write(buffer, 0, buffer.Length);

context.Response.Close();

It transfers the page “json.txt” but that ist not what I want. I dynamically need to create a json and send it to openHAB. How do I need to change my code? (especially without using any external libraries!)

You’re unlikely to get the help you need asking here.
Your C# code is not related to openHAB and imagine most developers here would probably be more proficient in Java than C#.
Unfortunately I’m also in that group so I’m unable to provide any technical help. Maybe it’s worth a post on Stack Overflow.

Here you are reading the contents of the json.txt file into the msg string. This string is then returned to OpenHAB (or any other connected client). So the variable msg should contain any JSON you want to reply back to OpenHAB.

Disclaimer: I don’t know any C#…

Your question isn’t completely clear. Are you saying you want to generate the JSON dynamically instead of reading it from the “json.txt” file?

// Get the data from somewhere
int myValue = 0;

// Format JSON (in this case, a map with "value" as the key)
string msg = $"{{ \"value\": {myValue} }}";

// send msg, as shown above

Thank you. That’s it. I have already tried it similar but I had a syntax error.
Now there is still a problem when processing it in openHAB. “Cannot get service reference for transformation service of type JSONPATH. Couldn’t transform response because transformationService of type ‘JSONPATH’ is unavailable”. Do you know what that is? The binding in the items is e.g.

{ http="<[http://localhost/athermostatroom:10000:JSONPATH($.thing.athermostatroom)]" }

Why can a transformationservice be unavailable?

Because you didn’t install it? :slight_smile:
go to PaperUI, Addons, Transformations and check if “JSONPath Transformation” is installed.
If not… PUSH THE BUTTON!

Seems consistent :slight_smile: But in PaperUI - Addons I can only switch between bindings, persistence services and user Interfaces. There are no Transformations.

That’s odd…I’ve got a few more choices: