0

The DataTable contains single column EFFDATE.

EFFDATE
2015-06-15
2014-10-21
2014-07-17
2014-07-16
2014-06-17
2014-03-13

I have the following code to convert DataTable dttbl to JSON.

JsonConvert.SerializeObject(dttbl, Formatting.Indented)

The output from conversion is

[
  {
    "EFFDATE": "2015-06-15"
  },
  {
    "EFFDATE": "2014-10-21"
  },
  {
    "EFFDATE": "2014-07-17"
  },
  {
    "EFFDATE": "2014-07-16"
  },
  {
    "EFFDATE": "2014-06-17"
  },
  {
    "EFFDATE": "2014-03-13"
  }
]

The output I want is

{
    "EFFDATE": [
        "2015-06-15",
        "2014-10-21",
        "2014-07-17",
        "2014-07-16",
        "2014-06-17",
        "2014-03-13"
    ]
}

Please advice.

2
  • no way... JSON needs to explicit the column names in each object Commented Jul 29, 2015 at 15:52
  • Does JSON needs to explicit the column or JSON.net needs to do that? The expected JSON is valid. So I think I got to custom-create my own JSON from the DataTable. Commented Jul 29, 2015 at 15:57

1 Answer 1

1
var json = JsonConvert.SerializeObject(
              new { EFFDATE = dt.AsEnumerable().Select(r => r[0]) }
           );
Sign up to request clarification or add additional context in comments.

2 Comments

@stackoverflowuser If you liked it you may want to accept it. meta.stackexchange.com/questions/5234/…
I was not allowed to. Had to wait for few minutes. Just did.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.