tam tam
tam tam

Reputation: 1900

JSON.NET throwing exception on missing comma

I am using JSON.NET to de-serialize a json file, very rarely there is a file within which a field may not have a trailing comma after its value. this causes an excpetion becaue it expects a comma. I was looking over JSON.NET documentation and could not find an option where I could specify to still parse that field.

Is there a way in JSON.NET to not worry about trailing comma?

Below, if lets say if a field does not contain a comma at its end, an exception is thrown.

 {
        "Rack": "0015",
        "SampleType": "Specimen",
        "SID": "HBSAG-PC",
        "Position": 4,
        "AssayNumber": 149,
    "AssayVersion": 5,
        "Dilution": 1,
        "Replicate": 5
    "Track": 1,
    "Lane": 1,
        "ReagentMasterLot": "08559LF00",
        "ReagentSerialNumber": 65000,
        "Comment": "HBSAG Q IIPC"
    }

Upvotes: 0

Views: 2926

Answers (2)

Chuck Conway
Chuck Conway

Reputation: 16435

The JSON you've posted is invalided. Not because of the trailing comma at the end, but a missing comma in the center

{
    "Rack": "0015",
    "SampleType": "Specimen",
    "SID": "HBSAG-PC",
    "Position": 4,
    "AssayNumber": 149,
    "AssayVersion": 5,
    "Dilution": 1,
    "Replicate": 5
    "Track": 1,
    "Lane": 1,
    "ReagentMasterLot": "08559LF00",
    "ReagentSerialNumber": 65000,
    "Comment": "HBSAG Q IIPC"
}

This line "Replicate": 5"Track": 1, is missing a comma between the '5' and "Track".

The corrected Json looks like this:

{
    "Rack": "0015",
    "SampleType": "Specimen",
    "SID": "HBSAG-PC",
    "Position": 4,
    "AssayNumber": 149,
    "AssayVersion": 5,
    "Dilution": 1,
    "Replicate": 5,
    "Track": 1,
    "Lane": 1,
    "ReagentMasterLot": "08559LF00",
    "ReagentSerialNumber": 65000,
    "Comment": "HBSAG Q IIPC"
}

You can validate your JSON with JSONLint.

Upvotes: 1

Brian Rogers
Brian Rogers

Reputation: 129687

The JSON spec requires that name/value pairs within an object have commas between them. (See also JSON.org). If the commas are missing, the JSON is invalid. Therefore, any standards-conforming parser is going to throw an exception, including JSON.Net. There is no option to ignore missing commas. You need to fix the JSON at the source.

Upvotes: 0

Related Questions