In a Json.NET Merge, can individual changes be logged?












0















Is there any way to identify the changes that have been made in a Merge? For example, here are two JSON files, test1.json



{
f1: "String",
f2: true,
f3: 1000001,
f4: [1]
}


.and test2.json



{
f1: "String",
f2: 1,
f3: 1000002,
f4: [1,2]
}


In the first, f2 is a Boolean but in the second f2 is a Number. Similarly, f3's value changes and an extra item is added to f4.



Is there any way to record these changes? I am most interested in the change of data type rather than in the change of content.










share|improve this question


















  • 2





    Do you need to do it recursively, or just for the root object?

    – dbc
    Jan 2 at 6:28











  • @dbc Recursively. Some of the structures are many layers deep. The goal is to have some statistical measure of whether certain properties are always of a specific type or whether occasionally they change type. With that data we can cook up a schema which we will use for a subsequent data migration.

    – bugmagnet
    Jan 2 at 7:53
















0















Is there any way to identify the changes that have been made in a Merge? For example, here are two JSON files, test1.json



{
f1: "String",
f2: true,
f3: 1000001,
f4: [1]
}


.and test2.json



{
f1: "String",
f2: 1,
f3: 1000002,
f4: [1,2]
}


In the first, f2 is a Boolean but in the second f2 is a Number. Similarly, f3's value changes and an extra item is added to f4.



Is there any way to record these changes? I am most interested in the change of data type rather than in the change of content.










share|improve this question


















  • 2





    Do you need to do it recursively, or just for the root object?

    – dbc
    Jan 2 at 6:28











  • @dbc Recursively. Some of the structures are many layers deep. The goal is to have some statistical measure of whether certain properties are always of a specific type or whether occasionally they change type. With that data we can cook up a schema which we will use for a subsequent data migration.

    – bugmagnet
    Jan 2 at 7:53














0












0








0








Is there any way to identify the changes that have been made in a Merge? For example, here are two JSON files, test1.json



{
f1: "String",
f2: true,
f3: 1000001,
f4: [1]
}


.and test2.json



{
f1: "String",
f2: 1,
f3: 1000002,
f4: [1,2]
}


In the first, f2 is a Boolean but in the second f2 is a Number. Similarly, f3's value changes and an extra item is added to f4.



Is there any way to record these changes? I am most interested in the change of data type rather than in the change of content.










share|improve this question














Is there any way to identify the changes that have been made in a Merge? For example, here are two JSON files, test1.json



{
f1: "String",
f2: true,
f3: 1000001,
f4: [1]
}


.and test2.json



{
f1: "String",
f2: 1,
f3: 1000002,
f4: [1,2]
}


In the first, f2 is a Boolean but in the second f2 is a Number. Similarly, f3's value changes and an extra item is added to f4.



Is there any way to record these changes? I am most interested in the change of data type rather than in the change of content.







json merge json.net






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 4:55









bugmagnetbugmagnet

4,270646109




4,270646109








  • 2





    Do you need to do it recursively, or just for the root object?

    – dbc
    Jan 2 at 6:28











  • @dbc Recursively. Some of the structures are many layers deep. The goal is to have some statistical measure of whether certain properties are always of a specific type or whether occasionally they change type. With that data we can cook up a schema which we will use for a subsequent data migration.

    – bugmagnet
    Jan 2 at 7:53














  • 2





    Do you need to do it recursively, or just for the root object?

    – dbc
    Jan 2 at 6:28











  • @dbc Recursively. Some of the structures are many layers deep. The goal is to have some statistical measure of whether certain properties are always of a specific type or whether occasionally they change type. With that data we can cook up a schema which we will use for a subsequent data migration.

    – bugmagnet
    Jan 2 at 7:53








2




2





Do you need to do it recursively, or just for the root object?

– dbc
Jan 2 at 6:28





Do you need to do it recursively, or just for the root object?

– dbc
Jan 2 at 6:28













@dbc Recursively. Some of the structures are many layers deep. The goal is to have some statistical measure of whether certain properties are always of a specific type or whether occasionally they change type. With that data we can cook up a schema which we will use for a subsequent data migration.

– bugmagnet
Jan 2 at 7:53





@dbc Recursively. Some of the structures are many layers deep. The goal is to have some statistical measure of whether certain properties are always of a specific type or whether occasionally they change type. With that data we can cook up a schema which we will use for a subsequent data migration.

– bugmagnet
Jan 2 at 7:53












1 Answer
1






active

oldest

votes


















1














You could read in both JSON files, deserialize them both to Dictionary<string,object>, then compare them with String.Equals() and output the differences.



The below demo assumes the one level deep JSON structure as shown in the question. The same logic should apply for deeply nested JSON objects, but how you traverse the JSON objects and match keys will change. For deeper layered JSON objects with different depths, recursive traversal will need to be used.



Basic Demo:



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

namespace MergeJson {
public static class Program {
private static string FILE_ONE = "test1.json";
private static string FILE_TWO = "test2.json";

/// <summary>
/// Converts JSON file into Dictionary
/// </summary>
/// <param name="path">The path of the JSON file</param>
/// <returns>The converted Dictionary</returns>
private static Dictionary<string, object> GetJsonDict (string path) {

// Read json file into string
string json = File.ReadAllText (path);

// Deserialize JSON string into dictionary
var jsonDict = JsonConvert.DeserializeObject<Dictionary<string, object>> (json);

return jsonDict;
}

public static void Main (string args) {

// Get both Dictionaries
var jsonDictOne = GetJsonDict (FILE_ONE);
var jsonDictTwo = GetJsonDict (FILE_TWO);

// Go through each key in the first dictionary and compare with second dictionary
foreach (KeyValuePair<string, object> entry in jsonDictOne) {

// Get key and value
var value = entry.Value;
var key = entry.Key;

// Ensure second dictionary has key
if (jsonDictTwo.ContainsKey (key)) {
var otherValue = jsonDictTwo[key];

// Compare both values and output differences
if (!value.Equals (otherValue)) {
FormattableString difference = $"Difference in key {entry.Key}: {value} -> {otherValue}";
Console.WriteLine (difference);
}
}
}
}
}
}


Output:



Difference in key f2: True -> 1
Difference in key f3: 1000001 -> 1000002
Difference in key f4: [
1
] -> [
1,
2
]





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001391%2fin-a-json-net-merge-can-individual-changes-be-logged%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You could read in both JSON files, deserialize them both to Dictionary<string,object>, then compare them with String.Equals() and output the differences.



    The below demo assumes the one level deep JSON structure as shown in the question. The same logic should apply for deeply nested JSON objects, but how you traverse the JSON objects and match keys will change. For deeper layered JSON objects with different depths, recursive traversal will need to be used.



    Basic Demo:



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

    namespace MergeJson {
    public static class Program {
    private static string FILE_ONE = "test1.json";
    private static string FILE_TWO = "test2.json";

    /// <summary>
    /// Converts JSON file into Dictionary
    /// </summary>
    /// <param name="path">The path of the JSON file</param>
    /// <returns>The converted Dictionary</returns>
    private static Dictionary<string, object> GetJsonDict (string path) {

    // Read json file into string
    string json = File.ReadAllText (path);

    // Deserialize JSON string into dictionary
    var jsonDict = JsonConvert.DeserializeObject<Dictionary<string, object>> (json);

    return jsonDict;
    }

    public static void Main (string args) {

    // Get both Dictionaries
    var jsonDictOne = GetJsonDict (FILE_ONE);
    var jsonDictTwo = GetJsonDict (FILE_TWO);

    // Go through each key in the first dictionary and compare with second dictionary
    foreach (KeyValuePair<string, object> entry in jsonDictOne) {

    // Get key and value
    var value = entry.Value;
    var key = entry.Key;

    // Ensure second dictionary has key
    if (jsonDictTwo.ContainsKey (key)) {
    var otherValue = jsonDictTwo[key];

    // Compare both values and output differences
    if (!value.Equals (otherValue)) {
    FormattableString difference = $"Difference in key {entry.Key}: {value} -> {otherValue}";
    Console.WriteLine (difference);
    }
    }
    }
    }
    }
    }


    Output:



    Difference in key f2: True -> 1
    Difference in key f3: 1000001 -> 1000002
    Difference in key f4: [
    1
    ] -> [
    1,
    2
    ]





    share|improve this answer






























      1














      You could read in both JSON files, deserialize them both to Dictionary<string,object>, then compare them with String.Equals() and output the differences.



      The below demo assumes the one level deep JSON structure as shown in the question. The same logic should apply for deeply nested JSON objects, but how you traverse the JSON objects and match keys will change. For deeper layered JSON objects with different depths, recursive traversal will need to be used.



      Basic Demo:



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

      namespace MergeJson {
      public static class Program {
      private static string FILE_ONE = "test1.json";
      private static string FILE_TWO = "test2.json";

      /// <summary>
      /// Converts JSON file into Dictionary
      /// </summary>
      /// <param name="path">The path of the JSON file</param>
      /// <returns>The converted Dictionary</returns>
      private static Dictionary<string, object> GetJsonDict (string path) {

      // Read json file into string
      string json = File.ReadAllText (path);

      // Deserialize JSON string into dictionary
      var jsonDict = JsonConvert.DeserializeObject<Dictionary<string, object>> (json);

      return jsonDict;
      }

      public static void Main (string args) {

      // Get both Dictionaries
      var jsonDictOne = GetJsonDict (FILE_ONE);
      var jsonDictTwo = GetJsonDict (FILE_TWO);

      // Go through each key in the first dictionary and compare with second dictionary
      foreach (KeyValuePair<string, object> entry in jsonDictOne) {

      // Get key and value
      var value = entry.Value;
      var key = entry.Key;

      // Ensure second dictionary has key
      if (jsonDictTwo.ContainsKey (key)) {
      var otherValue = jsonDictTwo[key];

      // Compare both values and output differences
      if (!value.Equals (otherValue)) {
      FormattableString difference = $"Difference in key {entry.Key}: {value} -> {otherValue}";
      Console.WriteLine (difference);
      }
      }
      }
      }
      }
      }


      Output:



      Difference in key f2: True -> 1
      Difference in key f3: 1000001 -> 1000002
      Difference in key f4: [
      1
      ] -> [
      1,
      2
      ]





      share|improve this answer




























        1












        1








        1







        You could read in both JSON files, deserialize them both to Dictionary<string,object>, then compare them with String.Equals() and output the differences.



        The below demo assumes the one level deep JSON structure as shown in the question. The same logic should apply for deeply nested JSON objects, but how you traverse the JSON objects and match keys will change. For deeper layered JSON objects with different depths, recursive traversal will need to be used.



        Basic Demo:



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

        namespace MergeJson {
        public static class Program {
        private static string FILE_ONE = "test1.json";
        private static string FILE_TWO = "test2.json";

        /// <summary>
        /// Converts JSON file into Dictionary
        /// </summary>
        /// <param name="path">The path of the JSON file</param>
        /// <returns>The converted Dictionary</returns>
        private static Dictionary<string, object> GetJsonDict (string path) {

        // Read json file into string
        string json = File.ReadAllText (path);

        // Deserialize JSON string into dictionary
        var jsonDict = JsonConvert.DeserializeObject<Dictionary<string, object>> (json);

        return jsonDict;
        }

        public static void Main (string args) {

        // Get both Dictionaries
        var jsonDictOne = GetJsonDict (FILE_ONE);
        var jsonDictTwo = GetJsonDict (FILE_TWO);

        // Go through each key in the first dictionary and compare with second dictionary
        foreach (KeyValuePair<string, object> entry in jsonDictOne) {

        // Get key and value
        var value = entry.Value;
        var key = entry.Key;

        // Ensure second dictionary has key
        if (jsonDictTwo.ContainsKey (key)) {
        var otherValue = jsonDictTwo[key];

        // Compare both values and output differences
        if (!value.Equals (otherValue)) {
        FormattableString difference = $"Difference in key {entry.Key}: {value} -> {otherValue}";
        Console.WriteLine (difference);
        }
        }
        }
        }
        }
        }


        Output:



        Difference in key f2: True -> 1
        Difference in key f3: 1000001 -> 1000002
        Difference in key f4: [
        1
        ] -> [
        1,
        2
        ]





        share|improve this answer















        You could read in both JSON files, deserialize them both to Dictionary<string,object>, then compare them with String.Equals() and output the differences.



        The below demo assumes the one level deep JSON structure as shown in the question. The same logic should apply for deeply nested JSON objects, but how you traverse the JSON objects and match keys will change. For deeper layered JSON objects with different depths, recursive traversal will need to be used.



        Basic Demo:



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

        namespace MergeJson {
        public static class Program {
        private static string FILE_ONE = "test1.json";
        private static string FILE_TWO = "test2.json";

        /// <summary>
        /// Converts JSON file into Dictionary
        /// </summary>
        /// <param name="path">The path of the JSON file</param>
        /// <returns>The converted Dictionary</returns>
        private static Dictionary<string, object> GetJsonDict (string path) {

        // Read json file into string
        string json = File.ReadAllText (path);

        // Deserialize JSON string into dictionary
        var jsonDict = JsonConvert.DeserializeObject<Dictionary<string, object>> (json);

        return jsonDict;
        }

        public static void Main (string args) {

        // Get both Dictionaries
        var jsonDictOne = GetJsonDict (FILE_ONE);
        var jsonDictTwo = GetJsonDict (FILE_TWO);

        // Go through each key in the first dictionary and compare with second dictionary
        foreach (KeyValuePair<string, object> entry in jsonDictOne) {

        // Get key and value
        var value = entry.Value;
        var key = entry.Key;

        // Ensure second dictionary has key
        if (jsonDictTwo.ContainsKey (key)) {
        var otherValue = jsonDictTwo[key];

        // Compare both values and output differences
        if (!value.Equals (otherValue)) {
        FormattableString difference = $"Difference in key {entry.Key}: {value} -> {otherValue}";
        Console.WriteLine (difference);
        }
        }
        }
        }
        }
        }


        Output:



        Difference in key f2: True -> 1
        Difference in key f3: 1000001 -> 1000002
        Difference in key f4: [
        1
        ] -> [
        1,
        2
        ]






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 2 at 10:52

























        answered Jan 2 at 5:53









        RoadRunnerRoadRunner

        11.3k31441




        11.3k31441
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001391%2fin-a-json-net-merge-can-individual-changes-be-logged%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]