Convert JSON data to querystring in C# GET request

19,882

Solution 1

If the json object is flat as in your example, then

string json = @"{
    ""name"": ""charlie"",
    ""num"": 123
}";

var jObj = (JObject)JsonConvert.DeserializeObject(json);

var query = String.Join("&",
                jObj.Children().Cast<JProperty>()
                .Select(jp=>jp.Name + "=" + HttpUtility.UrlEncode(jp.Value.ToString())));

query would be name=charlie&num=123

Solution 2

I make this code to run in .Net Core:

public static string JsonToQuery(this string jsonQuery)
    {
        string str = "?";
        str += jsonQuery.Replace(":", "=").Replace("{","").
                    Replace("}", "").Replace(",","&").
                        Replace("\"", "");
        return str;
    }

Example:

var _baseURI = "http://www.example.com/";
var endPoint = "myendpoint";
ExampleObjectModel requestModel = new ExampleObjectModel();
var requestModelJson = JsonConvert.SerializeObject(requestModel);
var url = string.Format("{0}{1}{2}", _baseURI, endPoint, requestModelJson.JsonToQuery());
Share:
19,882
Mike Flynn
Author by

Mike Flynn

I am the founder and CEO of Exposure Events. Exposure Events is a tournament and league management system delivering online scheduling and conflict checker, live results, apps, free directory and more.

Updated on June 04, 2022

Comments

  • Mike Flynn
    Mike Flynn almost 2 years

    What is the best way to convert a JSON object into querystrings to append to a GET Url? The POST is straight forward and gets read by my Web API backend.

    {Name: 'Mike' } = ?Name=Mike

     private static string MakeRequest(HttpWebRequest req, string data)
                {
                    try
                    {
                        if (req.Method == Verbs.POST.ToString() || req.Method == Verbs.PUT.ToString() || req.Method == Verbs.DELETE.ToString())
                        {
                            var encodedData = Encoding.UTF8.GetBytes(data);
    
                            req.ContentLength = encodedData.Length;
                            req.ContentType = "application/json";
                            req.GetRequestStream().Write(encodedData, 0, encodedData.Length);
                        }
    
                        using (var response = req.GetResponse() as HttpWebResponse)
                        using (var reader = new StreamReader(response.GetResponseStream()))
                        {
                            return reader.ReadToEnd();
                        }
                    }
                    catch (WebException we)
                    {
                        if(we.Response == null)
                        {
                            return JsonConvert.SerializeObject(new { Errors = new List<ApiError> { new ApiError(11, "API is currently unavailable") }});
                        }
    
                        using (var response = we.Response as HttpWebResponse)
                        using (var reader = new StreamReader(response.GetResponseStream()))
                        {
                            return reader.ReadToEnd();
                        }
                    }
    
    
      }