52

As a follow up to my question here: JSON.NET: Obtain JObject from JProperty Value ...

I read links and digested the above comments and advice. Now I wonder: Is there a good reason why there is no "direct" (i.e., easy) way to turn the Value of a JProperty object into a JObject? It seems like a lot of work to get to a JToken and then have to construct if statements, etc. This isn't to complain about the extra work; rather, it's to admit that I still think I don't understand the true purpose of JToken. At one time, all FedEx packages first routed to Memphis: It seems that all objects can/should go to JToken first then be parceled to the actual target object type. Is that a way to think about it?

In other words, is there ever a good reason to use JToken--or is it just that so many other functions return a JToken and then you have to just deal with that? The JSON.NET manual gives ways to cast JToken to other types (http://www.newtonsoft.com/json/help/html/Operators_T_Newtonsoft_Json_Linq_JToken.htm) but doesn't mention going from JToken to JObject...

I find that JObject is usually what I want to have in order to work with JSON and to map from JSON to my .NET classes and back again--as well as doing a host of other operations. I still wonder what is the compelling reason to ever use a JToken object?

4
  • 7
    JToken represents any possible token in a JSON file. If you have some JSON and don't know in advance what might be inside, you can parse it with JToken.Parse() and get a result as long as the JSON is valid. JObject.Parse(), JArray.Parse() and JValue.Parse() will throw if the root JSON token is not of the expected type. Commented Jul 5, 2016 at 20:30
  • 1
    To round-out your otherwise excellent summary, what exactly is a JSON token (I for one am still new to JSON)? Commented Jul 5, 2016 at 20:58
  • 3
    From the JSON standard (which is really quite simple), it would be an object, array, value, string or number. Commented Jul 5, 2016 at 21:01
  • Please somehow move your comment to answer so I can give you credit. Thanks! Commented Jul 5, 2016 at 21:02

2 Answers 2

105

From the standard, JSON is built out of the following five types of token:

  • object: an unordered set of name/value pairs.
  • array: an ordered collection of values.
  • value: a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.
  • string
  • number.

JToken is an abstract base class that represents any one of these possible tokens. If you have some JSON and don't know in advance what might be inside, you can parse it with JToken.Parse() and get a result as long as the JSON is well-formed. JObject.Parse() and JArray.Parse() will throw if the root JSON token is not of the expected type. And there is no JValue.Parse() to parse a JSON string you know to represent an "atomic" value, requiring the use of JToken.Parse() in such a case.

Similarly, JToken.FromObject() may be used to serialize any sort of c# object to a JToken hierarchy without needing to know in advance the resulting JSON type. This can be useful e.g. when writing generic serialization-related code.

Sign up to request clarification or add additional context in comments.

3 Comments

I don't see the definition of token in the JSON standard you referenced. If a value is a superset of a token (token or true or false or null), why should we care about tokens when parsing JSON?
@AlanEvangelista - Seems there's an inconsistency in terminology between Newtonsoft and json.org. json.org uses "value" to mean JSON of any type. Newtonsoft uses JValue for a primitive value (true/false/null/string/number), and "token" for JSON of any type.
@dbc: Thanks for solving a mystery that seemed desperately difficult to find an answer for. Most searches relating to "token" and "JSON" end up returning results related to JWTs. I wasn't able to find any definition of "token" on the newtonsoft.com website, although mention is made of it. The JSON standard linked to above is similar: it mentions "tokens" but doesn't define the term. It seemed like the best kept secret of JSON practioners.
0

Another reason to use JToken is in a unit test. If you get a complex string back and you want to validate it serialized correctly, it's hard to confirm you have an array of 4 objects in a string. You can create a JObject and then validate that there is a Key "Name" and it's string value is "MyObject". That there is a Key "Data" and it's JToken is a JArray with count 4.

You can use ContainsKey to confirm you customized property names as you expected.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.