none
How to retrieve C# key dictionary nested keys? RRS feed

  • Question

  • Hello all,

    Here is my JASON file:-

    [
        {
            "name": "1040A|TURBOTAX | 2016",
            "contentType": "application/json",
            "type": "CLIENT",
            "metadata": {
                "flagable": false,
                "source": "MBC",
                "attachmentId": "",
                "processed": "false",
                "followup": "false",
                "fileType": "pdf",
                "extracted": "true",
            }
        },

    How to retrieve keys & value under "metadata" key?

    E.g. how to retrieve source key under "metadata" key and it's value as "MBC"?

    Any help will be appreciated

    Thanks

    Regards,

    gk03


    Please do let us know your feedback. Thank You - KG, MCTS

    Wednesday, March 18, 2020 1:31 AM

Answers

  • Hi gk1393,

    Thank you for posting here.

    You can try the following way to get the nested value.

             static void Main(string[] args)
            {
                string json = "{'name':'1040A | TURBOTAX | 2016','contentType':'application / json','type':'CLIENT','metadata':{'flagable':'false','source':'MBC','attachmentId':'','processed':'false','followup':'false','fileType':'pdf','extracted':'true'}}";
                JObject dynObj = JObject.Parse(json);
                var value = dynObj.SelectToken("metadata.source");
                Console.WriteLine(value);
                Console.ReadLine();
            }

    Result:

    Hope this could be helpful.

    Best Regards,

    Timon


    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.

    • Marked as answer by gk1393 Sunday, March 22, 2020 8:29 PM
    Wednesday, March 18, 2020 5:31 AM

All replies

  • What are you using to deserialize your json object? If you're always expecting this particular type, can you create class with these public properties and then just use something like

    https://www.newtonsoft.com/json/help/html/Overload_Newtonsoft_Json_JsonConvert_DeserializeObject.htm


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    Wednesday, March 18, 2020 1:58 AM
    Moderator
  • Hi gk1393,

    Thank you for posting here.

    You can try the following way to get the nested value.

             static void Main(string[] args)
            {
                string json = "{'name':'1040A | TURBOTAX | 2016','contentType':'application / json','type':'CLIENT','metadata':{'flagable':'false','source':'MBC','attachmentId':'','processed':'false','followup':'false','fileType':'pdf','extracted':'true'}}";
                JObject dynObj = JObject.Parse(json);
                var value = dynObj.SelectToken("metadata.source");
                Console.WriteLine(value);
                Console.ReadLine();
            }

    Result:

    Hope this could be helpful.

    Best Regards,

    Timon


    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.

    • Marked as answer by gk1393 Sunday, March 22, 2020 8:29 PM
    Wednesday, March 18, 2020 5:31 AM