Working with NewtonSoft's Json library and using JObject to access various elements in complex json records. Below is a modified extract of one for the purposes of this question.
Accessing PID.2.1 using a literal path is successful, but replacing the path with a variable that contains the same path returns null.
I've read the documentation and I'm not sure why the approach using the variable does not work. What am I missing? Any insight is appreciated.
Here is a simple example demonstrating the issue.
string json = @"{
""organizationId"": ""FFAE1D00-D7EF-E123-B00D-D12345D0000"",
""hl7"": {
""HL7Message"": {
""PID"": {
""PID.1"": {
""PID.1.1"": 1
},
""PID.2"": {
""PID.2.1"": ""1111111"",
""PID.2.2"": null,
""PID.2.3"": null,
""PID.2.4"": ""SXA SYS ID""
}
}
}
}
}";
JObject MessageObject = JObject.Parse(json);
var test = MessageObject["hl7"]["HL7Message"]["PID"]["PID.2"]["PID.2.1"] // this finds the property
string val = test.Value<string>(); // this returns the string "1111111"
// this does not find the property
string path = "[\"hl7\"][\"HL7Message\"][\"PID\"][\"PID.2\"][\"PID.2.1\"]";
var otherTest = MessageObject[path]; // this returns null
Thank you.