---
# System prepended metadata

title: '[ChatGPT] Reorder JSON String'

---

# [ChatGPT] Reorder JSON String 

:::info
There is a JSON string:

{
    "prompt": "Once upon a time",
    "max_tokens": 50,
    "temperature": 0.5,
    "n": 1,
    "stop": "."
}

Could you create a class or method in C# .NET that reorders a JSON string based on parameter names?
:::



Yes, here's a simple class and method in C# .NET that can reorder the JSON string by parameter names:

```
using System;
using Newtonsoft.Json.Linq;

public class JsonStringReorderer {
    public static string Reorder(string jsonString) {
        JObject jsonObject = JObject.Parse(jsonString);
        JProperty[] properties = jsonObject.Properties().ToArray();
        Array.Sort(properties, (p1, p2) => string.Compare(p1.Name, p2.Name));
        jsonObject.RemoveAll();
        foreach (JProperty property in properties) {
            jsonObject.Add(property);
        }
        return jsonObject.ToString();
    }
}

```

This class has a static Reorder method that takes in a JSON string as input and returns the reordered JSON string. It uses the Newtonsoft.Json library to parse the input string into a JObject, then extracts the properties of the object into an array, sorts the properties by their names, removes all the properties from the original object, and finally adds the properties back to the object in the sorted order. The reordered object is then converted back to a JSON string and returned.

To use this class and method, simply create an instance of the JsonStringReorderer class and call the Reorder method with a JSON string as input:

```
string inputJsonString = "{\"prompt\": \"Once upon a time\",\"max_tokens\": 50,\"temperature\": 0.5,\"n\": 1,\"stop\": \".\"}";
string outputJsonString = JsonStringReorderer.Reorder(inputJsonString);
Console.WriteLine(outputJsonString);

```

This will output the reordered JSON string:

```
{
  "max_tokens": 50,
  "n": 1,
  "prompt": "Once upon a time",
  "stop": ".",
  "temperature": 0.5
}

```