2

Example:

{ "Header": { "Test": "Value" } }

I would like to retrieve the Header's value as a String, but not as an object.

Means the value of Header, { "Test": "Value" } is retrieved as a string.

This is part of a very large response, the above sample is simplified for illustration purposes only. Parsing manually will be very difficult to maintain in future.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
s k
  • 4,342
  • 3
  • 42
  • 61
  • Is serializing back to a string whatever that is deserialized at that level an option? – Lasse V. Karlsen Jan 31 '20 at 08:05
  • That is not an option. Because the value is digitally signed. I need to retrieve as it is. – s k Jan 31 '20 at 08:06
  • Well, then you're going to be in trouble. You probably need to do manual "deserialization". JSON (de)serializers does not care about whitespace, which they shouldn't. – Lasse V. Karlsen Jan 31 '20 at 08:09
  • Are you sure they did not define the signature as being on some kind of canonical form, like "serialize without linefeeds or indentation, but put spaces inside objects, after colons and commas"? – Lasse V. Karlsen Jan 31 '20 at 08:11
  • Lasse V. Karisen, yes the object is serialized in one line, without any CR and LF. – s k Jan 31 '20 at 08:17
  • So can't you mimic that serialization format and get the same output? – Lasse V. Karlsen Jan 31 '20 at 08:21
  • Can, but if that is the case, I would rather write my own parser instead. – s k Jan 31 '20 at 08:23
  • Most likely the file here has a very simple form with the wrapper object very static. I believe the only way they can guarantee a specific format for the innermost object to be kept is to first serialize the inner object, then calculate the signature, and then manually construct the outer JSON object using string concatenation or similar. You should be able to detect and extract the inner object with a Regex or simple string manipulation. – Lasse V. Karlsen Jan 31 '20 at 08:27
  • The file was constructed with C++ code. I think they just construct the JSON string themselves without using any library. Otherwise we will put the entire Header into quotes "" and make it a string. :( – s k Jan 31 '20 at 08:30
  • Could you not just deserialize the JSON, pick out which objects you want, then serialize it into a string? – RoadRunner Jan 31 '20 at 08:58

1 Answers1

0

You can try with JsonPath, as per example Query Json select

It says that:

JObject o = JObject.Parse(@"{
    "Header": { "Test": "Value" }
}");

And then query with JToken

JToken header = o.SelectToken("$..Header");
Console.Writeline(header);
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • When I do this, the JToken is actually formatted, means the output is indented and arranged into multiple lines, not the original string anymore. – s k Jan 31 '20 at 08:04
  • The problem is that JSON deserializers are not meant to solve the problem you have. JSON is a structured text based file format, but whitespace outside strings should not matter. If you need to get the exact *original* JSON for a subproperty you need to basically write your deserializer yourself. Is what I believe. – Lasse V. Karlsen Jan 31 '20 at 08:06