0

i have two json. i am comparing both json and finding the output but i need output in some different format. so i am sharing my code and demo jasons over here please help me on this. how can i get that desired output ?

demo json 1

  {
    "id": "0001",
    "type": "don",
    "name": "Cake2",
    "ppu": 0.56,
    "ppu1": 0.56,
    "ppu2": 0.56,
    "batters": {
        "batter": [{
                "fields": [{
                        "name": "fname",
                        "value": "ftest"
                    },
                    {
                        "name": "lname",
                        "value": "ltest"
                    }
                ]
            },
            {
                "fields": [{
                        "name": "fname",
                        "value": "ftest1"
                    },
                    {
                        "name": "lname",
                        "value": "ltest1"
                    }
                ]
            }

        ]
    },
    "batters2": {
        "batter2": [{
                "fields2": [{
                        "name": "fname",
                        "value": "ftest"
                    },
                    {
                        "name": "lname",
                        "value": "ltest"
                    }
                ]
            },
            {
                "fields2": [{
                        "name": "fname",
                        "value": "ftest1"
                    },
                    {
                        "name": "lname",
                        "value": "ltest1"
                    }
                ]
            }

        ]
    }

}

demo json 2

{
"id": "0001",
"type": "don",
"name": "Cake2",
"ppu": 0.56,
"ppu1": 0.56,
"ppu2": 0.56,
"batters": {
    "batter": [{
            "fields": [{
                    "name": "fname",
                    "value": "ftest"
                },
                {
                    "name": "lname",
                    "value": "ltest"
                }
            ]
        },
        {
            "fields": [{
                    "name": "fname",
                    "value": "ftest1"
                },
                {
                    "name": "lname",
                    "value": "ltest1"
                }
            ]
        }

    ]
},
"batters2": {
    "batter2": [{
            "fields2": [{
                    "name": "fname",
                    "value": "_ftest"
                },
                {
                    "name": "lname",
                    "value": "_ltest"
                }
            ]
        },
        {
            "fields2": [{
                    "name": "fname",
                    "value": "_ftest1"
                },
                {
                    "name": "lname",
                    "value": "ltest1"
                }
            ]
        }

    ]
}

}

compare logic

/// <summary>
        /// Deep compare two NewtonSoft JObjects. If they don't match, returns text diffs
        /// </summary>
        /// <param name="source">The expected results</param>
        /// <param name="target">The actual results</param>
        /// <returns>Text string</returns>

        public static StringBuilder CompareObjects(JObject source, JObject target)
        {
            StringBuilder returnString = new StringBuilder();
            foreach (KeyValuePair<string, JToken> sourcePair in source)
            {
                if (sourcePair.Value.Type == JTokenType.Object)
                {
                    if (target.GetValue(sourcePair.Key) == null)
                    {
                        returnString.Append("Key " + sourcePair.Key
                                            + " not found" + Environment.NewLine);
                    }
                    else if (target.GetValue(sourcePair.Key).Type != JTokenType.Object)
                    {
                        returnString.Append("Key " + sourcePair.Key
                                            + " is not an object in target" + Environment.NewLine);
                    }
                    else
                    {
                        returnString.Append(CompareObjects(sourcePair.Value.ToObject<JObject>(),
                            target.GetValue(sourcePair.Key).ToObject<JObject>()));
                    }
                }
                else if (sourcePair.Value.Type == JTokenType.Array)
                {
                    if (target.GetValue(sourcePair.Key) == null)
                    {
                        returnString.Append("Key " + sourcePair.Key
                                            + " not found" + Environment.NewLine);
                    }
                    else
                    {
                        returnString.Append(CompareArrays(sourcePair.Value.ToObject<JArray>(),
                            target.GetValue(sourcePair.Key).ToObject<JArray>(), sourcePair.Key));
                    }
                }
                else
                {
                    JToken expected = sourcePair.Value;
                    var actual = target.SelectToken(sourcePair.Key);
                    if (actual == null)
                    {
                        returnString.Append("Key " + sourcePair.Key
                                            + " not found" + Environment.NewLine);
                    }
                    else
                    {
                        if (!JToken.DeepEquals(expected, actual))
                        {
                            try
                            {
                                returnString.Append("Key "+ sourcePair.Key + ": "
                                                                       + sourcePair.Value + " !=  "
                                                                       + target.Property(sourcePair.Key).Value
                                                                       + Environment.NewLine);
                            }
                            catch (Exception)
                            {

                               
                            }
                        }
                    }
                }
            }
            return returnString;
        }

        /// <summary>
        /// Deep compare two NewtonSoft JArrays. If they don't match, returns text diffs
        /// </summary>
        /// <param name="source">The expected results</param>
        /// <param name="target">The actual results</param>
        /// <param name="arrayName">The name of the array to use in the text diff</param>
        /// <returns>Text string</returns>

        private static StringBuilder CompareArrays(JArray source, JArray target, string arrayName = "")
        {
            var returnString = new StringBuilder();
            for (var index = 0; index < source.Count; index++)
            {

                var expected = source[index];
                if (expected.Type == JTokenType.Object)
                {
                    var actual = (index >= target.Count) ? new JObject() : target[index];
                    returnString.Append(CompareObjects(expected.ToObject<JObject>(),
                        actual.ToObject<JObject>()));
                }
                else
                {

                    var actual = (index >= target.Count) ? "" : target[index];
                    if (!JToken.DeepEquals(expected, actual))
                    {
                        if (String.IsNullOrEmpty(arrayName))
                        {
                            returnString.Append("Index " + index + ": " + expected
                                                + " != " + actual + Environment.NewLine);
                        }
                        else
                        {
                            returnString.Append("Key " + arrayName
                                                + "[" + index + "]: " + expected
                                                + " != " + actual + Environment.NewLine);
                        }
                    }
                }
            }
            return returnString;
        }

after compare wanted output like this (get json hierarchy)

batters->batter->fields[0]->fname ->ftest != _ftest
batters->batter->fields[0]->lname ->ltest != _ltest
batters2->batter2->fields2[0]->fname ->ftest1 != _ftest1
haldo
  • 14,512
  • 5
  • 46
  • 52
  • You have the code, what is your problem with your code exactly? if you need help framing the question, refer [how to ask](https://stackoverflow.com/help/how-to-ask) – Mat J Feb 09 '22 at 16:58
  • Do any of the answers from [Find and return JSON differences using newtonsoft in C#?](https://stackoverflow.com/q/24876082/3744182) meet your needs? If not, can you clarify exactly how those answers do not meet your needs? – dbc Feb 09 '22 at 17:35
  • ... Actually, it seems you copied your code from [this answer](https://stackoverflow.com/a/39623754/3744182) by [Walter](https://stackoverflow.com/users/126654/walter) to [Find and return JSON differences using newtonsoft in C#?](https://stackoverflow.com/q/24876082/3744182). When you use code from a stack overflow post you **must** give credit as per https://stackoverflow.com/help/referencing. – dbc Feb 09 '22 at 17:35
  • Sorry will edit and give reference of that post.the solution was only giving compartion of two properties and returns the output but I want json hierarchy as well with result. – user3347906 Feb 10 '22 at 02:23

0 Answers0