Dan
Dan

Reputation: 9847

What Are Some Options To Convert Url-Encoded Form Data to JSON in .Net

I have a web request that is sending the server data that is in the format application/x-www-form-urlencoded. I would like to convert it to application/json.

Example:

URL-encoded form data:

Property1=A&Property2=B&Property3%5B0%5D%5BSubProperty1%5D=a&Property3%5B0%5D%5BSubProperty2%5D=b&Property3%5B1%5D%5BSubProperty1%5D=c&Property3%5B1%5D%5BSubProperty2%5D=d

Pretty version:

Property1=A
Property2=B
Property3[0][SubProperty1]=a
Property3[0][SubProperty2]=b
Property3[1][SubProperty1]=c
Property3[1][SubProperty2]=d

The above data needs to be converted to the following JSON data:

{
    Property1: "A",
    Property2: "B",
    Property3: [
        { SubProperty1: "a", SubProperty2: "b" },
        { SubProperty1: "c", SubProperty2: "d" }]
}

Question:

Are there any free tools that are capable of doing this? I have not been able to find any myself and if they exist, I'd rather consume them than writing one myself, but if it comes to that, I will.

A C#/.Net solution is preferred.

Upvotes: 21

Views: 16204

Answers (3)

anavarro9731
anavarro9731

Reputation: 61

If you are using are ASP.NET Core 1.0+ and JSON.NET you could do:

var reader = new Microsoft.AspNetCore.WebUtilities.FormReader(httpRequest.Body, Encoding.UTF8);
Dictionary<string, StringValues> keyValuePairs = await reader.ReadFormAsync();
var json = JsonConvert.SerializeObject(keyValuePairs);

Upvotes: 0

MUG4N
MUG4N

Reputation: 19717

The .NET Framework 4.5 includes everything you need to convert url-encoded form data to JSON. In order to do this you have to add a reference to the namespace System.Web.Extension in your C# project. After that you can use the JavaScriptSerializer class which provides you with everything you need to do the conversion.

The Code

using System.Web;
using System.Web.Script.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var dict = HttpUtility.ParseQueryString("Property1=A&Property2=B&Property3%5B0%5D%5BSubProperty1%5D=a&Property3%5B0%5D%5BSubProperty2%5D=b&Property3%5B1%5D%5BSubProperty1%5D=c&Property3%5B1%5D%5BSubProperty2%5D=d");
            var json = new JavaScriptSerializer().Serialize(
                                                     dict.Keys.Cast<string>()
                                                         .ToDictionary(k => k, k => dict[k]));

            Console.WriteLine(json);
            Console.ReadLine();
        }
    }
}

The Output

{
    "Property1":"A",
    "Property2":"B",
    "Property3[0][SubProperty1]":"a",
    "Property3[0][SubProperty2]":"b",
    "Property3[1][SubProperty1]":"c",
    "Property3[1][SubProperty2]":"d"
}

Notice: The output does not contain linebreaks or any formatting

Source: How do I convert a querystring to a json string?

Upvotes: 12

Peter O.
Peter O.

Reputation: 32898

I've written a utility class for parsing query strings and form data. It's available at:

https://gist.github.com/peteroupc/5619864

Example:

// Example query string from the question
String test="Property1=A&Property2=B&Property3%5B0%5D%5BSubProperty1%5D=a&Property3%5B0%5D%5BSubProperty2%5D=b&Property3%5B1%5D%5BSubProperty1%5D=c&Property3%5B1%5D%5BSubProperty2%5D=d";
// Convert the query string to a JSON-friendly dictionary
var o=QueryStringHelper.QueryStringToDict(test);
// Convert the dictionary to a JSON string using the JSON.NET
// library <http://json.codeplex.com/>
var json=JsonConvert.SerializeObject(o);
// Output the JSON string to the console
Console.WriteLine(json);

Let me know if it works for you.

Upvotes: 28

Related Questions