C
C#8mo ago
AngryAnt

System.Text.Json parsing property with dot in it

I have a third party server spewing a json response I need to parse. It is a single-string-property response with the property name containing a dot. How do I go about extracting the string value via System.Text.Json? Ex:
{
"m.server": "value I want to read"
}
{
"m.server": "value I want to read"
}
With jq I would get at it via jq '.["m.server"]' (where if the property was just "server", the syntax would just be jq '.server'), but I have not come across this issue in a .net context before.
4 Replies
IsNotNull
IsNotNull8mo ago
If you are making a class to deserialize it to, you can add an attribute to change the name that it maps to. Have you tried that yet?
AngryAnt
AngryAnt8mo ago
Yep. [JsonPropertyName ("m.server")] unfortunately has no effect for me.
IsNotNull
IsNotNull8mo ago
Hmmm Have you tried parsing it to a JsonDocument instead? Maybe you could access it by name then
AngryAnt
AngryAnt8mo ago
No. I am not familiar with that API. I'll give it a look. Thanks 🙂 That is indeed able to access the value via JsonDocument.Parse (result).RootElement.GetProperty ("m.server").GetString (); - thank you for pointing me there 🙂