0

i am trying to read a large json file from c# and Deserialize it:

// deserialize JSON directly from a file
using (StreamReader file = File.OpenText(@"C:\Users\hansi\Desktop\csvjson.json"))
{
    JsonSerializer serializer = new JsonSerializer();
    Product product2 = (Product)serializer.Deserialize(file, typeof(Product));
    Console.WriteLine(product2.Name);

}

But it only reads the first object and then close, anyone got answers to this? No Error the program continues, it just only reads the first object in my Json file

Example Json, it continues like this with 150000 products:

{
    "Item No.": 1000,
    "Product Name": "Pendant, Brass",
    "Product Category": "Lig",
    "Type": "Pendant",
    "Collection": "Tynl",
    "Designer": "Paav",
    "Design": 1965,
    "Form": "",
    "Dimension": "",
    "Finish of Main Part": "",
    "New Arrival": "",
    "Tags": "",
    "Estimated Release Date": "Ready to Order",
    "EAN": 571090274,
    "Country": "",
    "Country Of Origin": "CN",
    "Custom Tariff Code": 940520,
    "Product Weight /Kg": "3,3",
    "Gross Weight /Kg": "5,1",
    "Gross Height /Meter": "0,35",
    "Gross Length /Meter": "0,53",
    "Gross Depth /Meter": "0,53",
    "Gross Volume /m3": "",
    "Cord length": "",
    "Max Watt": "",
    "Upholstery": "",
    "Price Group": "",
    "Glides": "",
    "EUR RRP.": 
  },
  {
Hanskrogh
  • 86
  • 6
  • Can you post an example json blob? – Tyler Gauch Jan 09 '20 at 23:18
  • Might you please edit your question to share a full [mcve] including a sample of the JSON? Also what library is `JsonSerializer` from? Is it [tag:json.net]? – dbc Jan 09 '20 at 23:19
  • You are deserializing to an object.. perhaps read into a list of object? – Jawad Jan 09 '20 at 23:20
  • I am very bad at setting up questions in StackOverflow :D but i tried to post my json data, aswell as my proptie class here – Hanskrogh Jan 09 '20 at 23:23
  • 1
    Have you tried wrapping the text in `[]` and the deserializing a list? – Tyler Gauch Jan 09 '20 at 23:23
  • If i do that, i get this error: Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'GubiUpdateApp.Product' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. – Hanskrogh Jan 09 '20 at 23:25
  • 3
    That's not JSON, that's a comma-separated sequence of JSON values concatenated together. To read such a sequence using [tag:json.net], see [Line delimited json serializing and de-serializing](https://stackoverflow.com/q/29729063/3744182). (The same answer works for comma-separated JSON objects as line-separated JSON objects, see [How to deserialize dodgy JSON (with improperly quoted strings, and missing brackets)?](https://stackoverflow.com/a/46797460/3744182) for details.) – dbc Jan 09 '20 at 23:26
  • You need to deserialize into an **array** of objects, not into a single object. Try using `typeof(Product[])`. – Dour High Arch Jan 09 '20 at 23:29
  • @DourHighArch you can't deserialize non-array JSON into an array. One have read all entries separately (as shown in duplicates found by dbc) and do something with each entry (like add to a list). – Alexei Levenkov Jan 09 '20 at 23:34
  • 1
    @Alexei I was responding to Tyler. Also his data is multiple records and shouldn't be a non-array. – Dour High Arch Jan 09 '20 at 23:37
  • @DourHighArch I see... The funny part is based on comments it looks like OP started with valid JSON and decided to remove outer brackets instead of reading as array... Anyway this endup to be valid question with existing answers... also completely different from what OP actually should have asked. It's up to OP to decide if they want to [edit] post (so it can be closed as duplicate of regular "parse JSON" one) or if they already have enough info with current answer + duplicates. – Alexei Levenkov Jan 09 '20 at 23:49

1 Answers1

1

If you cant change the json file do what @dbc is saying and use the line delimited json parsing.

if you can then format it into a proper json array by surrounding it with []. Then change your code to:

var products = serializer.Deserialize(file, type(List<Product>);

Edit: changing to proper json will just save you headaches later

Tyler Gauch
  • 160
  • 1
  • 6