How to parse JSON to flat csv dlimited text file

Hi,

I need to convert a JSON string to a CSV flat file

I have a JSON string result set that looks like this:

{
	"instrument" : "EUR_USD",
	"granularity" : "M15",
	"candles" : [
		{
			"time" : "2005-09-01T04:00:00.000000Z",
			"openBid" : 1.23343,
			"openAsk" : 1.23358,
			"highBid" : 1.23348,
			"highAsk" : 1.23363,
			"lowBid" : 1.23318,
			"lowAsk" : 1.23333,
			"closeBid" : 1.23328,
			"closeAsk" : 1.23343,
			"volume" : 53,
			"complete" : true
		},
       ]
}

Open in new window


There are several "candle" data elements to follow, but this is an abbreviated snippet... So part of the solution would need to iterate through the remaining JSON string to get all the candle sets...

I need the output to look like this (comma separated values):

EUR_USD,M15
2005-09-01T04:00:00.000000Z,1.23343,1.23358,1.23348,1.23363,1.23318,1.23333,1.23328,1.23343,53,true
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Personally, I would perform JSON deserialization on this data. I would then take the resulting object and dump it to the CSV. For the deserialization, you can use JSON.NET (available in NuGet). You can take your JSON structure and paste it into the site http://json2csharp.com to get a quick-and-dirty set of classes that represent the JSON structure:

public class Candle
{
    public string time { get; set; }
    public double openBid { get; set; }
    public double openAsk { get; set; }
    public double highBid { get; set; }
    public double highAsk { get; set; }
    public double lowBid { get; set; }
    public double lowAsk { get; set; }
    public double closeBid { get; set; }
    public double closeAsk { get; set; }
    public int volume { get; set; }
    public bool complete { get; set; }
}

public class RootObject
{
    public string instrument { get; set; }
    public string granularity { get; set; }
    public List<Candle> candles { get; set; }
}

Open in new window


Then, it's simply a matter of deserializing the JSON via JSON.NET and dumping the parsed object to a file:

using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;

namespace _28783579
{
    class Program
    {
        static void Main(string[] args)
        {
            string json = @"
{
    ""instrument"" : ""EUR_USD"",
    ""granularity"" : ""M15"",
    ""candles"" : [
        {
            ""time"" : ""2005-09-01T04:00:00.000000Z"",
            ""openBid"" : 1.23343,
            ""openAsk"" : 1.23358,
            ""highBid"" : 1.23348,
            ""highAsk"" : 1.23363,
            ""lowBid"" : 1.23318,
            ""lowAsk"" : 1.23333,
            ""closeBid"" : 1.23328,
            ""closeAsk"" : 1.23343,
            ""volume"" : 53,
            ""complete"" : true
        },
       ]
}";

            RootObject instance = JsonConvert.DeserializeObject<RootObject>(json);

            using (StreamWriter writer = new StreamWriter("output.csv"))
            {
                foreach (Candle c in instance.candles)
                {
                    writer.WriteLine($"{c.time},{c.openBid},{c.openAsk},{c.highBid},{c.highAsk},{c.lowBid},{c.lowAsk},{c.closeBid},{c.closeAsk},{c.volume},{c.complete}");
                }
            }
        }
    }


    public class Candle
    {
        public string time { get; set; }
        public double openBid { get; set; }
        public double openAsk { get; set; }
        public double highBid { get; set; }
        public double highAsk { get; set; }
        public double lowBid { get; set; }
        public double lowAsk { get; set; }
        public double closeBid { get; set; }
        public double closeAsk { get; set; }
        public int volume { get; set; }
        public bool complete { get; set; }
    }

    public class RootObject
    {
        public string instrument { get; set; }
        public string granularity { get; set; }
        public List<Candle> candles { get; set; }
    }
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RobertFromSecretWeaponsAuthor Commented:
kaufmed, you're the man...  Worked great...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.