How can I deserialize an invalid json ? Truncated list of objects









up vote
3
down vote

favorite
1












My json file is mostly an array that contain objects but the list is incomplete, so I can't use the last entry. I would like to deserialize the rest of the file while discarding the last invalid entry



[ "key" : "value1" , "key " : "value2", { "key 


Please tell me if there is a way using Newtonsoft.Json library, or do I need some preprocessing.



Thank you!










share|improve this question























  • JSON is not HTML, there's no good reason to fatten libraries implementing workarounds for invalid data. You are probably using a computer language to process JSON; it shouldn't be difficult to implement preprocessing with string manipulation functions from such lang (C#?).
    – Álvaro González
    Apr 12 '16 at 14:57















up vote
3
down vote

favorite
1












My json file is mostly an array that contain objects but the list is incomplete, so I can't use the last entry. I would like to deserialize the rest of the file while discarding the last invalid entry



[ "key" : "value1" , "key " : "value2", { "key 


Please tell me if there is a way using Newtonsoft.Json library, or do I need some preprocessing.



Thank you!










share|improve this question























  • JSON is not HTML, there's no good reason to fatten libraries implementing workarounds for invalid data. You are probably using a computer language to process JSON; it shouldn't be difficult to implement preprocessing with string manipulation functions from such lang (C#?).
    – Álvaro González
    Apr 12 '16 at 14:57













up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





My json file is mostly an array that contain objects but the list is incomplete, so I can't use the last entry. I would like to deserialize the rest of the file while discarding the last invalid entry



[ "key" : "value1" , "key " : "value2", { "key 


Please tell me if there is a way using Newtonsoft.Json library, or do I need some preprocessing.



Thank you!










share|improve this question















My json file is mostly an array that contain objects but the list is incomplete, so I can't use the last entry. I would like to deserialize the rest of the file while discarding the last invalid entry



[ "key" : "value1" , "key " : "value2", { "key 


Please tell me if there is a way using Newtonsoft.Json library, or do I need some preprocessing.



Thank you!







json json.net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 12 '16 at 14:59









Álvaro González

104k30180270




104k30180270










asked Apr 12 '16 at 14:54









user5441518

906




906











  • JSON is not HTML, there's no good reason to fatten libraries implementing workarounds for invalid data. You are probably using a computer language to process JSON; it shouldn't be difficult to implement preprocessing with string manipulation functions from such lang (C#?).
    – Álvaro González
    Apr 12 '16 at 14:57

















  • JSON is not HTML, there's no good reason to fatten libraries implementing workarounds for invalid data. You are probably using a computer language to process JSON; it shouldn't be difficult to implement preprocessing with string manipulation functions from such lang (C#?).
    – Álvaro González
    Apr 12 '16 at 14:57
















JSON is not HTML, there's no good reason to fatten libraries implementing workarounds for invalid data. You are probably using a computer language to process JSON; it shouldn't be difficult to implement preprocessing with string manipulation functions from such lang (C#?).
– Álvaro González
Apr 12 '16 at 14:57





JSON is not HTML, there's no good reason to fatten libraries implementing workarounds for invalid data. You are probably using a computer language to process JSON; it shouldn't be difficult to implement preprocessing with string manipulation functions from such lang (C#?).
– Álvaro González
Apr 12 '16 at 14:57













2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










You can use the JsonReader class and try to parse as far as you get. Something like the code below will parse as many properties as it gets and then throw an exception. This is of course if you want to deserialize into a concrete class.



public Partial FromJson(JsonReader reader)

while (reader.Read())

// Break on EndObject
if (reader.TokenType == JsonToken.EndObject)
break;

// Only look for properties
if (reader.TokenType != JsonToken.PropertyName)
continue;

switch ((string) reader.Value)

case "Id":
reader.Read();
Id = Convert.ToInt16(reader.Value);
break;

case "Name":
reader.Read();
Name = Convert.ToString(reader.Value);
break;




return this;



Code taken from the CGbR JSON Target.






share|improve this answer



























    up vote
    3
    down vote













    Looks like on Json.NET 8.0.3 you can stream your string from a JsonTextReader to a JTokenWriter and get a partial result by catching and swallowing the JsonReaderException that gets thrown when parsing the truncated JSON:



    JToken root;
    string exceptionPath = null;
    using (var textReader = new StringReader(badJson))
    using (var jsonReader = new JsonTextReader(textReader))
    using (JTokenWriter jsonWriter = new JTokenWriter())

    try

    jsonWriter.WriteToken(jsonReader);

    catch (JsonReaderException ex)

    exceptionPath = ex.Path;
    Debug.WriteLine(ex);

    root = jsonWriter.Token;


    Console.WriteLine(root);
    if (exceptionPath != null)

    Console.WriteLine("Error occurred with token: ");
    var badToken = root.SelectToken(exceptionPath);
    Console.WriteLine(badToken);



    This results in:




    [

    "key": "value1"
    ,

    "key ": "value2"
    ,

    ]



    You could then finish deserializing the partial object with JToken.ToObject. You could also delete the incomplete array entry by using badToken.Remove().



    It would be better practice not to generate invalid JSON in the first place though. I'm also not entirely sure this is documented functionality of Json.NET, and thus it might not work with future versions of Json.NET. (E.g. conceivably Newtonsoft could change their algorithm such that JTokenWriter.Token is only set when writing is successful.)






    share|improve this answer
















    • 1




      Amazing! And still works in Json.NET 11.0.2!
      – Sergey Kraev
      May 30 at 6:30










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36576928%2fhow-can-i-deserialize-an-invalid-json-truncated-list-of-objects%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    You can use the JsonReader class and try to parse as far as you get. Something like the code below will parse as many properties as it gets and then throw an exception. This is of course if you want to deserialize into a concrete class.



    public Partial FromJson(JsonReader reader)

    while (reader.Read())

    // Break on EndObject
    if (reader.TokenType == JsonToken.EndObject)
    break;

    // Only look for properties
    if (reader.TokenType != JsonToken.PropertyName)
    continue;

    switch ((string) reader.Value)

    case "Id":
    reader.Read();
    Id = Convert.ToInt16(reader.Value);
    break;

    case "Name":
    reader.Read();
    Name = Convert.ToString(reader.Value);
    break;




    return this;



    Code taken from the CGbR JSON Target.






    share|improve this answer
























      up vote
      2
      down vote



      accepted










      You can use the JsonReader class and try to parse as far as you get. Something like the code below will parse as many properties as it gets and then throw an exception. This is of course if you want to deserialize into a concrete class.



      public Partial FromJson(JsonReader reader)

      while (reader.Read())

      // Break on EndObject
      if (reader.TokenType == JsonToken.EndObject)
      break;

      // Only look for properties
      if (reader.TokenType != JsonToken.PropertyName)
      continue;

      switch ((string) reader.Value)

      case "Id":
      reader.Read();
      Id = Convert.ToInt16(reader.Value);
      break;

      case "Name":
      reader.Read();
      Name = Convert.ToString(reader.Value);
      break;




      return this;



      Code taken from the CGbR JSON Target.






      share|improve this answer






















        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        You can use the JsonReader class and try to parse as far as you get. Something like the code below will parse as many properties as it gets and then throw an exception. This is of course if you want to deserialize into a concrete class.



        public Partial FromJson(JsonReader reader)

        while (reader.Read())

        // Break on EndObject
        if (reader.TokenType == JsonToken.EndObject)
        break;

        // Only look for properties
        if (reader.TokenType != JsonToken.PropertyName)
        continue;

        switch ((string) reader.Value)

        case "Id":
        reader.Read();
        Id = Convert.ToInt16(reader.Value);
        break;

        case "Name":
        reader.Read();
        Name = Convert.ToString(reader.Value);
        break;




        return this;



        Code taken from the CGbR JSON Target.






        share|improve this answer












        You can use the JsonReader class and try to parse as far as you get. Something like the code below will parse as many properties as it gets and then throw an exception. This is of course if you want to deserialize into a concrete class.



        public Partial FromJson(JsonReader reader)

        while (reader.Read())

        // Break on EndObject
        if (reader.TokenType == JsonToken.EndObject)
        break;

        // Only look for properties
        if (reader.TokenType != JsonToken.PropertyName)
        continue;

        switch ((string) reader.Value)

        case "Id":
        reader.Read();
        Id = Convert.ToInt16(reader.Value);
        break;

        case "Name":
        reader.Read();
        Name = Convert.ToString(reader.Value);
        break;




        return this;



        Code taken from the CGbR JSON Target.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 12 '16 at 15:27









        Toxantron

        1,625515




        1,625515






















            up vote
            3
            down vote













            Looks like on Json.NET 8.0.3 you can stream your string from a JsonTextReader to a JTokenWriter and get a partial result by catching and swallowing the JsonReaderException that gets thrown when parsing the truncated JSON:



            JToken root;
            string exceptionPath = null;
            using (var textReader = new StringReader(badJson))
            using (var jsonReader = new JsonTextReader(textReader))
            using (JTokenWriter jsonWriter = new JTokenWriter())

            try

            jsonWriter.WriteToken(jsonReader);

            catch (JsonReaderException ex)

            exceptionPath = ex.Path;
            Debug.WriteLine(ex);

            root = jsonWriter.Token;


            Console.WriteLine(root);
            if (exceptionPath != null)

            Console.WriteLine("Error occurred with token: ");
            var badToken = root.SelectToken(exceptionPath);
            Console.WriteLine(badToken);



            This results in:




            [

            "key": "value1"
            ,

            "key ": "value2"
            ,

            ]



            You could then finish deserializing the partial object with JToken.ToObject. You could also delete the incomplete array entry by using badToken.Remove().



            It would be better practice not to generate invalid JSON in the first place though. I'm also not entirely sure this is documented functionality of Json.NET, and thus it might not work with future versions of Json.NET. (E.g. conceivably Newtonsoft could change their algorithm such that JTokenWriter.Token is only set when writing is successful.)






            share|improve this answer
















            • 1




              Amazing! And still works in Json.NET 11.0.2!
              – Sergey Kraev
              May 30 at 6:30














            up vote
            3
            down vote













            Looks like on Json.NET 8.0.3 you can stream your string from a JsonTextReader to a JTokenWriter and get a partial result by catching and swallowing the JsonReaderException that gets thrown when parsing the truncated JSON:



            JToken root;
            string exceptionPath = null;
            using (var textReader = new StringReader(badJson))
            using (var jsonReader = new JsonTextReader(textReader))
            using (JTokenWriter jsonWriter = new JTokenWriter())

            try

            jsonWriter.WriteToken(jsonReader);

            catch (JsonReaderException ex)

            exceptionPath = ex.Path;
            Debug.WriteLine(ex);

            root = jsonWriter.Token;


            Console.WriteLine(root);
            if (exceptionPath != null)

            Console.WriteLine("Error occurred with token: ");
            var badToken = root.SelectToken(exceptionPath);
            Console.WriteLine(badToken);



            This results in:




            [

            "key": "value1"
            ,

            "key ": "value2"
            ,

            ]



            You could then finish deserializing the partial object with JToken.ToObject. You could also delete the incomplete array entry by using badToken.Remove().



            It would be better practice not to generate invalid JSON in the first place though. I'm also not entirely sure this is documented functionality of Json.NET, and thus it might not work with future versions of Json.NET. (E.g. conceivably Newtonsoft could change their algorithm such that JTokenWriter.Token is only set when writing is successful.)






            share|improve this answer
















            • 1




              Amazing! And still works in Json.NET 11.0.2!
              – Sergey Kraev
              May 30 at 6:30












            up vote
            3
            down vote










            up vote
            3
            down vote









            Looks like on Json.NET 8.0.3 you can stream your string from a JsonTextReader to a JTokenWriter and get a partial result by catching and swallowing the JsonReaderException that gets thrown when parsing the truncated JSON:



            JToken root;
            string exceptionPath = null;
            using (var textReader = new StringReader(badJson))
            using (var jsonReader = new JsonTextReader(textReader))
            using (JTokenWriter jsonWriter = new JTokenWriter())

            try

            jsonWriter.WriteToken(jsonReader);

            catch (JsonReaderException ex)

            exceptionPath = ex.Path;
            Debug.WriteLine(ex);

            root = jsonWriter.Token;


            Console.WriteLine(root);
            if (exceptionPath != null)

            Console.WriteLine("Error occurred with token: ");
            var badToken = root.SelectToken(exceptionPath);
            Console.WriteLine(badToken);



            This results in:




            [

            "key": "value1"
            ,

            "key ": "value2"
            ,

            ]



            You could then finish deserializing the partial object with JToken.ToObject. You could also delete the incomplete array entry by using badToken.Remove().



            It would be better practice not to generate invalid JSON in the first place though. I'm also not entirely sure this is documented functionality of Json.NET, and thus it might not work with future versions of Json.NET. (E.g. conceivably Newtonsoft could change their algorithm such that JTokenWriter.Token is only set when writing is successful.)






            share|improve this answer












            Looks like on Json.NET 8.0.3 you can stream your string from a JsonTextReader to a JTokenWriter and get a partial result by catching and swallowing the JsonReaderException that gets thrown when parsing the truncated JSON:



            JToken root;
            string exceptionPath = null;
            using (var textReader = new StringReader(badJson))
            using (var jsonReader = new JsonTextReader(textReader))
            using (JTokenWriter jsonWriter = new JTokenWriter())

            try

            jsonWriter.WriteToken(jsonReader);

            catch (JsonReaderException ex)

            exceptionPath = ex.Path;
            Debug.WriteLine(ex);

            root = jsonWriter.Token;


            Console.WriteLine(root);
            if (exceptionPath != null)

            Console.WriteLine("Error occurred with token: ");
            var badToken = root.SelectToken(exceptionPath);
            Console.WriteLine(badToken);



            This results in:




            [

            "key": "value1"
            ,

            "key ": "value2"
            ,

            ]



            You could then finish deserializing the partial object with JToken.ToObject. You could also delete the incomplete array entry by using badToken.Remove().



            It would be better practice not to generate invalid JSON in the first place though. I'm also not entirely sure this is documented functionality of Json.NET, and thus it might not work with future versions of Json.NET. (E.g. conceivably Newtonsoft could change their algorithm such that JTokenWriter.Token is only set when writing is successful.)







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Apr 12 '16 at 15:23









            dbc

            52.8k867119




            52.8k867119







            • 1




              Amazing! And still works in Json.NET 11.0.2!
              – Sergey Kraev
              May 30 at 6:30












            • 1




              Amazing! And still works in Json.NET 11.0.2!
              – Sergey Kraev
              May 30 at 6:30







            1




            1




            Amazing! And still works in Json.NET 11.0.2!
            – Sergey Kraev
            May 30 at 6:30




            Amazing! And still works in Json.NET 11.0.2!
            – Sergey Kraev
            May 30 at 6:30

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36576928%2fhow-can-i-deserialize-an-invalid-json-truncated-list-of-objects%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            Edmonton

            Crossroads (UK TV series)