0

What I want:

To add a property in JSON file in a specific place (for this case I want to add {"name2":"Tom} after {"name":"Andrew"} ) without changing its format (empty new lines)

Current JSON

    {
    "Profiles": [
        {
            "Properties": {

                "color": "blue",
                "shape": "circle",
                "fruit": "apple",
            
                "name": "Andrew",

                "username": "ad11",
                "Password": "pass",

                "country": "France",
                "region": "Europe"
            }
        }
    ]
}

Current code

        var json = File.ReadAllText(path);
        var Jobect = JObject.Parse(json);

        Jobect["Profiles"][0]["Properties"]["name2"] = "Tom";

        File.WriteAllText(path, Jobect.ToString());

Resulted JSON

 {
   "Profiles": [
     {
       "Properties": {
         "color": "blue",
         "shape": "circle",
         "fruit": "apple",
         "name": "Andrew",
         "username": "ad11",
         "Password": "pass",
         "country": "France",
         "region": "Europe",
         "name2": "Tom"
       }
     }
   ]
 }

Desired JSON

{
    "Profiles": [
        {
            "Properties": {

                "color": "blue",
                "shape": "circle",
                "fruit": "apple",
            
                "name": "Andrew",
                "name2": "Tom,

                "username": "ad11",
                "Password": "pass",

                "country": "France",
                "region": "Europe"
            }
        }
    ]
}

In conclusion. How can I add a property in JSON file in a specific place and without change its format?

  • 4
    That's probably going to be difficult, since JSON is not supposed to care about whitespace, as such I don't think deserializers preserve knowledge about where it found whitespace and what kind/how much it found. May I ask why preserving the whitespace is important? – Lasse V. Karlsen Nov 02 '20 at 12:03
  • @LasseV.Karlsen I do not want to change the existing format of the JSON. This example is simple and easy to compare the changes, but if we had a JSON with 1000 lines and with some empty lines for human readability, then this edit would guide to disaster. – Christos Stefanopoulos Nov 02 '20 at 12:11
  • However, is there a way to add the value in the specific place? Let's say, we want name2 after name and not at the end of the file. – Christos Stefanopoulos Nov 02 '20 at 12:12
  • 2
    Why a JSON need to be human readable? – Alejandro Nov 02 '20 at 12:15
  • 3
    You could achieve this behaviour by replacing all empty lines by a placeholder value first like ```"placeholer":"emptyline"```. After that execute your insertion with json.net and in the end replace the previously included placeholder values with empty lines again. A bit hacky but does what you need. – Erik T. Nov 02 '20 at 12:22
  • @Alejandro If the JSON includes valuable things that you want to review. I do not desire the resulted JSON to lose its initial format. – Christos Stefanopoulos Nov 02 '20 at 12:24
  • Search for "name", search for the next newline and insert the desired line after that. If you deserialize/serialize, you'll always get a format that doesn't keep your empty lines. – Fildor Nov 02 '20 at 12:24
  • @ErikT. Thanks for trying to find an answer. I really appreciate it. I guess that this will probably work. However, the optimal would be something more automated. – Christos Stefanopoulos Nov 02 '20 at 12:29
  • There is no support that I know of that will do what you want, so either you hack it, like @ErikT. mentioned, or you do it manually without any help from a JSON library. – Lasse V. Karlsen Nov 02 '20 at 12:29
  • @Fildor Is this feasible? What should I use to achieve this? – Christos Stefanopoulos Nov 02 '20 at 12:33
  • String indexof, regex, etc. – Lasse V. Karlsen Nov 02 '20 at 12:37
  • 1
    I don't know your actual data, but from what I can take out of the question, I'd give it a try. You can use quite any text editing tool of your choice - C#, sed, awk, ... just work on the _text_ not on json objects. BUT: That would need `name` to be unique in each file! – Fildor Nov 02 '20 at 12:37
  • 1
    " but if we had a JSON with 1000 lines and with some empty lines for human readability" A 1000 line JSON shouldn't be considered as human-readable. – Zohar Peled Nov 02 '20 at 12:40
  • @Fildor Thanks, Yes, I think it is possible to do with an awk.. However, what I meant is how to do it in C# (i.e in the above code). And yes, let's assume that name is unique – Christos Stefanopoulos Nov 02 '20 at 12:43
  • @ZoharPeled No offense, but I think you miss the point, thanks though – Christos Stefanopoulos Nov 02 '20 at 12:45
  • 1
    Does this answer your question? [Add a new line at a specific position in a text file.](https://stackoverflow.com/questions/16212127/add-a-new-line-at-a-specific-position-in-a-text-file) – Fildor Nov 02 '20 at 12:46
  • @ChristosStefanopoulos none taken, however I can say the same thing back to you. JSON Doesn't care if `Name` is before or after `Name2` - both are still the same no matter what the order of the properties inside. JSON is nothing more than a Data serialization protocol and as such the order of the properties and the existence of empty lines is irrelevant. `{"x"=1, "y"=2}` is exactly the same as `{"y"=2, "x"=1}`... – Zohar Peled Nov 02 '20 at 12:50
  • @ZoharPeled Yes indeed. But this is not what I want to achieve. – Christos Stefanopoulos Nov 02 '20 at 12:56
  • 1
    @Fildor I will try that, this may work and solve one part of the issue. Really appreciate your time and effort to find a solution in the given problem! – Christos Stefanopoulos Nov 02 '20 at 12:57

0 Answers0