Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading