question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Only parses part of JSON to CSV

See original GitHub issue

Hi,

I have a situation where not my whole json gets parsed into csv-format.

Code

        var csvData = new StringBuilder();
        using (var jsonReader = ChoJSONReader.LoadText(jsonString))
        {
            using (var csvWriter = new ChoCSVWriter(csvData)
                .WithFirstLineHeader()
                .WithDelimiter(",")
                .QuoteAllFields()
                .Configure(c => c.UseNestedKeyFormat = true)
                )
            {
                csvWriter.Write(jsonReader);
            }
        }
        var finalCsvString = csvData.ToString();

I have an example json below containing 2 objects in an array. Only the first object gets parsed. In my real life example I have multiple objects under the ones supplied here. As long as I have the first object in the json, then that object is the only object that gets parsed. If I remove it, all other objects suddenly gets parsed flawlessly. So my guess is that it must contain som kind of escape?

For me it is unclear if it is the reader or the writer that fails.

[
  {
    "UnitId": "id1",
    "ProductNumber": "pn1",
    "SiteCode": "1",
    "SerialNumber": null,
    "Firmware": null,
    "FilePaths": [
      null
    ],
    "Orders": [
      {
        "OrderId": "oid1",
        "State": "completed",
        "OrderType": "order",
        "OrderSubtype": "porder",
        "ProductNumber": "pn1",
        "ProductLevelIn": "1",
        "ProductLevelOut": "2",
        "TestNameOut": "T1",
        "WorkSize": 1,
        "CreatedDatetime": "2021-01-28T09:52:45",
        "CompletedDatetime": "2021-01-28T10:57:29",
        "Firmware": "fw1",
        "TestRuns": [
          {
            "Passed": 0,
            "StationId": "st1",
            "OperatorId": "oi1",
            "StartedDatetime": "2021-01-28T10:23:06",
            "TestSystemVersion": "tsv1",
            "TestSeqVersion": "tsqv1",
            "TestStubVersion": "tsbv1",
            "ErrorCode": "ec1",
            "ErrorDescription": "ed1",
            "AdditionalErrorInfo": "",
            "Created": "2021-01-28T10:23:06",
            "TestEquipment": null
          }
        ]
      }
    ],
    "Subunits": [
      null
    ]
  },
  {
    "UnitId": "id1",
    "ProductNumber": "pn2",
    "SiteCode": "2",
    "SerialNumber": null,
    "Firmware": null,
    "FilePaths": [
      "fp2"
    ],
    "Orders": [
      {
        "OrderId": "oid2",
        "State": "completed",
        "OrderType": "order",
        "OrderSubtype": "rorder",
        "ProductNumber": "pn2",
        "ProductLevelIn": "1",
        "ProductLevelOut": "1",
        "TestNameOut": "T2",
        "WorkSize": 2,
        "CreatedDatetime": "2021-03-30T12:16:38",
        "CompletedDatetime": "2021-03-30T15:06:00",
        "Firmware": "",
        "TestRuns": [
          {
            "Passed": 0,
            "StationId": "st2",
            "OperatorId": "oi2",
            "StartedDatetime": "2021-03-30T13:26:26",
            "TestSystemVersion": "tsv1",
            "TestSeqVersion": "tsqv1",
            "TestStubVersion": "tsbv1",
            "ErrorCode": "ec2",
            "ErrorDescription": "ed2",
            "AdditionalErrorInfo": "aei2",
            "Created": "2021-03-30T08:26:26",
            "TestEquipment": [
              {
                "TeqId": "ti1",
                "TeqPartNumber": "tpn1",
                "TeqRevision": 1
              }
            ]
          }
        ]
      }
    ],
    "Subunits": [
      null
    ]
  }
]

Thank you kindly!

BR, MO

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Cinchoocommented, Nov 2, 2021

Use .ThrowAndStopOnMissingField(false) on CSVWriter, as the 2 objects are having mixed structure.

Here is working sample: https://dotnetfiddle.net/5Ppv1t

0reactions
mikaelollhagecommented, Nov 3, 2021

Thank you my friend. It is a generic method that serves several completely different models in this case, so I will go aheadwith the full scan instead.

Thanks again for your amazing help!

BR, MO

Read more comments on GitHub >

github_iconTop Results From Across the Web

python json to csv , code can only parse part of json data
python json to csv , code can only parse part of json data ... the code can only parse CPEVENT, other variables such...
Read more >
JSON CSV or TSV
Online tool for converting JSON to CSV or TSV. Convert JSON to Excel. ... Parsing CSVJSON is done by processing one line at...
Read more >
Parsing a JSON File into CSV - Microsoft Q&A
Parsing a JSON File into CSV · 1. Point the source dataset to the input .json file and select Array of document as...
Read more >
Convert JSON to CSV in Python
Converting JSON to CSV​​ For simple JSON data consisting of key and value pairs, keys will be headers for the CSV file and...
Read more >
Can I convert a JSON property (not full JSON) to csv with ...
Can I convert a JSON property (not full JSON) to csv with DataWeave without parsing it first to an Apex class? Ask Question....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found