none
How to append other json output to one json root element using c# RRS feed

  • Question

  • I have 3 api calls which gives me 3 separate json output

    api 1 output

    {
        "demo": {
            "results": [
                {
                    "__metadata": {
                        "id": "",
                        "uri": "",
                        "type": "ABC.MasterData"
                    },
                    "part1": {
                        "__metadata": {
                            "type": "ABC.MasterData_demo"
                        },
                        "location": "df393"
                    },
                    "Identifier": "987",
                    "Name": "dex"               
                }
            ]
        }
    }

    • Edited by ashuthinks32 Saturday, September 14, 2019 9:56 AM
    Saturday, September 14, 2019 8:19 AM

All replies

  • You need to create one big class that has the Api1Class, Api2Class, and Api3Class in it and then serialize out just the one big class.



    Sam Hobbs
    SimpleSamples.Info

    Saturday, September 14, 2019 8:31 AM
  • I tried that but not gettign expected output as api1 is also giving me array but if you see my expected output string i do not have array for `MasterData` please paste some code will be helpful to me :) thanks


    SE

    Saturday, September 14, 2019 8:33 AM
  • Well if you can tell us what you tried and why it did not work then someone would help you with it. For myself I don't believe in doing all of someone else's work; I will let someone else have the points.


    Sam Hobbs
    SimpleSamples.Info

    Saturday, September 14, 2019 9:33 AM
  • Hi ashuthinks32, 

    Thank you for posting here.

    For your question, you want to append other json output to one json root element.

    I make a test on my side, and you can refer to the following code:

            static void Main(string[] args)
            {
                JObject o1 = JObject.Parse(@"
                {
                    'demo': {
                    'results': [
                                {
                                    '__metadata': {
                                        'id': '',
                                        'uri': '',
                                        'type': 'ABC.MasterData'
                                    },
                                    'part1': {
                                        '__metadata': {
                                            'type': 'ABC.MasterData_demo'
                                        },
                                        'location': 'df393'
                                    },
                                    'Identifier': '987',
                                    'Name': 'dex'               
                                }
                                ]
                            }
                }");
                JObject o2 = JObject.Parse(@"
                {
                    'demo': {
                    'results': [
                                {
                                    '__metadata2': {
                                        'id': '',
                                        'uri': '',
                                        'type': 'ABC.MasterData'
                                    },
                                    'part2': {
                                        '__metadata2': {
                                            'type': 'ABC.MasterData_demo'
                                        },
                                        'location': 'df394'
                                    },
                                    'Identifier': '988',
                                    'Name': 'dex'               
                                }
                                ]
                            }
                }");
                JObject o3 = JObject.Parse(@"
                {
                    'demo': {
                    'results': [
                                {
                                    '__metadata3': {
                                        'id': '',
                                        'uri': '',
                                        'type': 'ABC.MasterData'
                                    },
                                    'part3': {
                                        '__metadata3': {
                                            'type': 'ABC.MasterData_demo'
                                        },
                                        'location': 'df395'
                                    },
                                    'Identifier': '989',
                                    'Name': 'dex'               
                                }
                                ]
                            }
                }");
                o1.Merge(o2, new JsonMergeSettings
                {
                    MergeArrayHandling = MergeArrayHandling.Union
                });
                o1.Merge(o3, new JsonMergeSettings
                {
                    MergeArrayHandling = MergeArrayHandling.Union
                });
                string json = o1.ToString();
    
                Console.WriteLine(json);
                Console.ReadLine();
            }

    Hope it can help you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 16, 2019 8:04 AM
    Moderator
  • thank you but it merge all output outside `Mastedata` i want it inside masterdata 1st element and ignore array of masterdata. other elements should be inside root element `masterdata`

    i'm able to do that my modifying json but finding if we have better option to do it.

               var output = o1.Substring(0, o1.Length - 4) + "," + o2.Substring(1, o2.Length - 2) + "," +  + o3.Substring(1) + "}";


    SE



    Monday, September 16, 2019 8:16 AM
  • Hi ashuthinks32, 

    Thanks for your feedback.

    You can also create a new element as the root element of the json string.

    Here’s the code:

          string str = "{\"root\":[" + o1 + "," + o2 + "," + o3 + "]}";

    Result:

    {"root":[{
      "demo": {
        ...
      }
    },{
      "demo": {
        ...
      }
    },{
      "demo": {
        ...
      }
    }]}
    

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, September 20, 2019 8:36 AM
    Moderator