How to get values from JObject using SelectToken when property name contains dot

Frank Mehlhop 66 Reputation points
2022-07-26T14:29:34.723+00:00

I got a json (see below) in a JObject named hits.
I can read single values from this object like this:

string path = hits.SelectToken("_source.path").Value<string>();  

But I can't figure out, how to read the value from "highlight.attachment.content". It seems to be a List because "[]".

I tried many ways which are not working.
There is a exception or the result is null.

Some examples of it I leave below.

How can I get the content of the List in highlight.attachment.content using SelectTokens?

{  
   "_index":"attachments",  
   "_id":"43",  
   "_source":{  
      "path":"C:\\Basis\\\doc\\00000052.pdf",  
      "attachment":{  
         "content_type":"application/pdf",  
         "format":"application/pdf; version=1.3",  
      }  
   },  
   "highlight":{  
      "attachment.content":[  
         "Grundstück\nGemarkung : Musterstadt - Testdorf\nFlur : 004\nFlurstück : 18/1\nGröße : 2492 m²\n\nvon Frau Julia <em>Berger</em>"  
      ]  
   }  
}  

here some code which is not working:

var a = hits.SelectTokens("highlight.attachment.content").Values<string>();  
var a = hits.SelectTokens("highlight.attachment.content").Values<string>();  
var a = hits.SelectTokens("highlight.attachment.content[]").Values<string>();  
var a = hits.SelectToken("highlight.attachment.content[*]");  
var a = hits.SelectTokens("highlight.attachment.content[*]").Values<string>();  
var a = hits.SelectTokens("highlight.attachment.content[0]").Values<string>();  
var a = hits.SelectTokens("highlight.attachment.content[][0]").Values<string>();  
var h = hits.SelectTokens("highlight.attachment.content[*]").Values<List<string>>();  
var h = hits.SelectToken("highlight.attachment.content").Value<List<string>>();  
var h = hits.SelectToken("highlight.attachment.content[]").Value<List<string>>();  
var h = (List<string>)hits.SelectToken("highlight.attachment.content").Value<List<string>>();  
var h = (List<string>)hits.SelectToken("highlight.attachment.content[]").Value<List<string>>();  
var h = hits.SelectTokens("highlight.attachment.content[*][]").Values<List<string>>();  
JArray array = (JArray)hits["highlight.attachment.content"];  
var high = hits.SelectToken("highlight.attachment.content");                  
var first = (JObject)hits["highlight.attachment"]["content"][0];  
var highlight = (JObject)hits["highlight.attachment"];  
var first = (JObject)hits["highlight.attachment.content[]"];  
var first = (JObject)hits["highlight.attachment.content[*]"][0];  
Developer technologies C#
{count} vote

Accepted answer
  1. Reza Aghaei 4,986 Reputation points MVP Volunteer Moderator
    2022-07-26T15:12:06.95+00:00

    Since the property name attachment.content contains . you can escape it like this:

    var tokens = jObj.SelectToken("highlight['attachment.content']");  
    

    For more information see:

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-07-26T15:23:32.167+00:00

    Using .NET Core reading the file. Full source

    using System.Text.Json.Serialization;  
      
    namespace JObjectQuestion.Classes  
    {  
      
        public class RootData  
        {  
            public string _index { get; set; }  
            public string _id { get; set; }  
            public _Source _source { get; set; }  
            public Highlight highlight { get; set; }  
        }  
      
        public class _Source  
        {  
            public string path { get; set; }  
            public Attachment attachment { get; set; }  
        }  
      
        public class Attachment  
        {  
              
            public string content_type { get; set; }  
            public string format { get; set; }  
        }  
      
        public class Highlight  
        {  
            [JsonPropertyName("attachment.content")]  
            public string[] attachmentcontent { get; set; }  
        }  
      
    }  
      
    

    Read the data

    var json = File.ReadAllText("data.json");  
    RootData result = JsonSerializer.Deserialize<RootData>(json);  
    var content = result.highlight.attachmentcontent;  
    

    224972-screenshot.png


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.