-3

Below is the piece of my code which is throwing ex two exception in this line and I have no clue whats wrong in here.

Exceptions thrown are:

  1. JsonReaderException: Input string '08' is not a valid number. Path 'Ape', line 1, position 51.
  2. FormatException: Additional non-parsable characters are at the end of the string.

The Json which I am deserializing line by line is as follows:

{"PartitionKey": "test","RowKey": "first","Ape": 06,"Nepe": "depe","EPA": 323,"Time": "04/23/2012 18:25:43","bits": "test"}
{"PartitionKey": "test","RowKey": "Second","Ape": 107,"Nepe": "TNepe","EPA": 23,"Time": "04/22/2012 18:25:43","bits": "Ttest"}
{"PartitionKey": "test","RowKey": "Third","Ape": 08,"Nepe": "TNepe","EPA": 34,"Time": "04/20/2020 18:25:43","bits": "Jtest"}

There is no problem in reading first two line but error happens at third/last line. I am not sure as what is wrong as I made sure no extra space in middle or end of line and 'Ape' key is of type integer.

here is my code>, the error occurs at Deserializing Json Object. Find the value passed in line before error occurs in the screen shot>

foreach( var line in lines )
        {
            var data = JsonConvert.DeserializeObject<JObject>( line );
            var errors = validator.Validate( data, schema );
            
            if( errors.Count() > 0 )
            {...

enter image description here

Please let me if one knows whats wrong and the fix required. Thanks

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
Ankit Kumar
  • 476
  • 1
  • 11
  • 38
  • You do realise this is not json? Thats to say, the json standard doesn't support leading zeros, also you have mashed a bunch of json together. Why are you in this situation? – TheGeneral Oct 25 '20 at 23:33
  • I do realise that and the format is what I am expecting to read, its not json but since I am reading line by line, the code handels the root element issue and your second question is about 0, I have a line where it reads with leading zero but throws error for next line and that is my whole question here. if there would have been error in the first line, this question would not have been posted. – Ankit Kumar Oct 25 '20 at 23:45

3 Answers3

3

I have a line where it reads with leading zero but throws error for next line and that is my whole question here

If this is the question the answer is simple, Json.Net is reading it as an octal

The octal numeral system, or oct for short, is the base-8 number system, and uses the digits 0 to 7.

So anything with a leading 0 it will consider an octal

For instance, it will parse 03, but it will not parse (and throw an error) on 09

You may even get the right results for 00 - 07. However, if you had 077 (although it will parse) you will get a value of 323, which will most likely not be what you want or you would expect.

Example

77 = int == 77
077 = octal = 323
63 = int = 63
010 = octal = 8
8 = int = 8
9 = int = 9
08 = octal = throw
09 = octal = throw

In short, the json is malformed, you will likely need to fix this at the source. or fix the json before you parse it

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • Thanks for the explanation, although, it appears that the library is a bit confusing parsing those values... I reproduced the code and if you put 77 it will parse to 77, but if you use 077 it will parse to 63, if you put 010 it will parse to 8, if you put 09 it throws the error.... So, nothing really makes sense and this could cause a lot of problems for people using those values and not perceive the error right away – MestreDosMagros Oct 26 '20 at 00:04
  • @ MestreDosMagros yeah so basically 77 = int == 77, 077 = octal = 323 , 63 = int = 63, 010 = octal = 8 – TheGeneral Oct 26 '20 at 00:07
  • The strange part is that the library is assuming different numeric systems that can result in different values and dont give any feedback that this can lead to an big error in the future – MestreDosMagros Oct 26 '20 at 00:11
  • I reproduced the code and 077 is assumed octal and its actually transformed to 63 decimal* – MestreDosMagros Oct 26 '20 at 00:17
  • @MestreDosMagros give me a sec, i think i have solution for you – TheGeneral Oct 26 '20 at 00:18
  • @MestreDosMagros cancel that, I thought i had a solution for you in text.json, the only way i can see of dealing with now is either fixing this at the source, or trying to regex out the 0s before parsing – TheGeneral Oct 26 '20 at 00:35
  • Thanks for all explanation and discussion.. i see whats wrong here and I hope it helps other in future. would request not to downvote this question, i think it address a very common mistake which can be missed. – Ankit Kumar Oct 26 '20 at 07:42
-1

If you put your JSON on this url: https://jsonformatter.curiousconcept.com/# you can see the error that occours in your case, is the integer starting with zeros.
The better explanation you can find in this question: https://stackoverflow.com/questions/27361565/why-is-json-invalid-if-an-integer-begins-with-a-leading-zero#:~:text=6%20Answers&text=A%20leading%200%20indicates%20an,would%20not%20contain%20an%208.

MestreDosMagros
  • 1,000
  • 5
  • 19
-1

I used this JSON validator https://jsonformatter.curiousconcept.com
and it seems there are 2 problems here:
1st: There are 3 root object elements
2nd: Numbers can't start with 0

I believe this is the structure you seek:

[{
    "PartitionKey": "test",
    "RowKey": "first",
    "Ape": 6,
    "Nepe": "depe",
    "EPA": 323,
    "Time": "04/23/2012 18:25:43",
    "bits": "test"
}, {
    "PartitionKey": "test",
    "RowKey": "Second",
    "Ape": 107,
    "Nepe": "TNepe",
    "EPA": 23,
    "Time": "04/22/2012 18:25:43",
    "bits": "Ttest"
}, {
    "PartitionKey": "test",
    "RowKey": "Third",
    "Ape": 8,
    "Nepe": "TNepe",
    "EPA": 34,
    "Time": "04/20/2020 18:25:43",
    "bits": "Jtest"
}]
Korina
  • 3
  • 4
  • Beaten by MestreDosMagros by a minute :-) – Korina Oct 25 '20 at 23:13
  • but if you see my json, the first line has integer starting with 0 and that doesnt thow any error – Ankit Kumar Oct 25 '20 at 23:16
  • I guess its because the error is throw from last to first, check changing only the last integer and the error should become the next from bottom to top – MestreDosMagros Oct 25 '20 at 23:24
  • I updated with what I believe you want your structure to be. It's a valid JSON – Korina Oct 25 '20 at 23:26
  • and root elements are handled as I am parsing line by line.. – Ankit Kumar Oct 25 '20 at 23:28
  • no it does not, it starts from beginning and 1st and 2nd line is parsed successfully. error happened at third line – Ankit Kumar Oct 25 '20 at 23:29
  • still with integers starting with zeros... if you cant change those values, is better to pass the values 06 and 08 as string values than handle this after in your class – MestreDosMagros Oct 25 '20 at 23:30
  • that I can but still I was interested in knowing why its happening for one set of line and not for first line..Had it happened for first line I would have agreed on the error. – Ankit Kumar Oct 25 '20 at 23:34
  • Is this JSON coming from your backend or constructed somewhere else within the client-side code? – Korina Oct 25 '20 at 23:44
  • coming from backend – Ankit Kumar Oct 25 '20 at 23:48
  • if you read "Some parsers do support it though, which may lead to some confusion. Other parsers will recognize it as an invalid sequence and will throw an error, although the exact explanation they give may differ." from the question with the answer i posted, you can expect the error is being managed by the Newtonsoft library by different forms for the diferent lines. also, converting 06 from octal number to decimal is 06, but 08 translate to 10, maybe the converter check this in some way and throw an error to prevent future errors this conversion could cause – MestreDosMagros Oct 25 '20 at 23:50
  • I read the General's answer, tried `03` but the validator still fails it (no matter which RFC spec you chose). If I were you I would start looking into why the backend sends this response. – Korina Oct 26 '20 at 00:01
  • @Korina i've tried with 03 and works as it 'should' ```var json = @"{""PartitionKey"": ""test"",""RowKey"": ""first"",""Ape"": 03,""Nepe"": ""depe"",""EPA"": 323,""Time"": ""04/23/2012 18:25:43"",""bits"": ""test""}"; var data = JsonConvert.DeserializeObject(json); Console.WriteLine(data);``` – MestreDosMagros Oct 26 '20 at 00:06
  • Also, about the `errors.Count()>0` line failing, I don't know your debugging output, but if `error` comes out null from the previous failure, it's reasonable that `errors.Anything()` will also fail. – Korina Oct 26 '20 at 00:08
  • @MestreDosMagros I'm out of ideas :-P – Korina Oct 26 '20 at 00:11