clean CSV column value as JSON to C# object

I’ve got some text written in a CSV column that is supposed to represent a JSON string:

{
"text": "foil text1",
"image": "existing_image_uploaded.png",
    "score": false
},

{
"text": "foil text2",
"image": "existing_image_uploaded2.png",
    "score": true
}

This CSV text comes out as the following string:

 var foils = "{\n    \"text\": \"foil text1\",\n    \"image\": \"existing_image_uploaded.png\",\n        
 \"score\": false\n},\n\n{\n    \"text\": \"foil text2\",\n    \"image\": 
 \"existing_image_uploaded2.png\",\n        \"score\": true\n}"

I would like to convert this text to a List of the following class

public class FoilJSON{
    public string text {get;set;}
    public string image {get;set;}
    public bool score {get;set;}
}

This is the way I would like to convert the JSON to a List of FoilJSON

var converted = JsonSerializer.Deserialize<List<FoilJSON>>(foils);

However the string foils is not in a proper JSON format to convert to a base class.
Is there a C# library or method to remove all the CSV garbage within the foils string?

>Solution :

var foils = "[{\n    \"text\": \"foil text1\",\n    \"image\": \"existing_image_uploaded.png\",\n        
 \"score\": false\n},\n\n{\n    \"text\": \"foil text2\",\n    \"image\": 
 \"existing_image_uploaded2.png\",\n        \"score\": true\n}]"

You have to have square brackets to have a list otherwise you simply have two objects separated by a comma… doesn’t really make it a correct json

Leave a Reply