1

I'm working on a project where I want to build tokens from a JSON Array.

       //Data fed to the system       
      {"Fruits":[{"Number":"111", "Name":"Apple"}, {"Number":"112", "Name":"Orange"},{"Number":"113", "Name":"Peach"}]}

      //serializes the http content to a string
      string result = Request.Content.ReadAsStringAsync().Result;

      //deserializes result
      Dictionary<string, dynamic> data = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(result);

      //builds custom tokens
      var customTokens = new Dictionary<string, object>();
      foreach (var dataField in data)
      {
        if (dataField.Value is JArray)
            {
                string nameValue = "";
                foreach (JObject content in dataField.Value.Children<JObject>())
                {
                    foreach (JProperty prop in content.Properties())
                    {
                        nameValue += prop.Name.ToString() + " : " + prop.Value.ToString();
                    }

                }
                customTokens.Add($"{dataField.Key}", nameValue);
            }
        }

The above code managed to create token $Fruits. But i also want to achieve token $Number and $Name, where values of each token is from the concatenated values of same key. Example, If I use the "$Number", it will be replaced by 111, 112, 113 and If I use the $Name, it will be replaced by Apple, Orange, Peach.

Also, I'm not using any strongly type models as I don't know what data will be fed to the system.

Any help?

3
  • I don't understand your required output, can you clarify at all? Commented Jul 12, 2019 at 12:10
  • I want to concatenate the values of same key, so that when i used the "Number" i get the values such as 111, 112, 113 Commented Jul 12, 2019 at 12:17
  • But where does the key Fruits come in? You're just ignoring that at present Commented Jul 12, 2019 at 12:19

1 Answer 1

1

There are a few minor changes to your code to achieve this. First make your dictionary look like this:

var customTokens = new Dictionary<string, List<string>>();

Then, when you loop over all the properties in the array, check if the property has been added, and if not add it.

foreach (JProperty prop in content.Properties())
{
    if(customTokens.ContainsKey(prop.Name))
    {
        customTokens[prop.Name].Add(prop.Value.ToString());
    }
    else
    {
        customTokens.Add(prop.Name, new List<string> { prop.Value.ToString() });
    }
}

At the end you have a dictionary where the key is the property name and the value is a List<string> - this can be concatenated together:

foreach(var item in customTokens)
{
    Console.WriteLine(item.Key + ":" + String.Join(",", item.Value));
}

Or, if you really want it in a dictionary of concatenated strings just do this

var finalResult = customTokens.ToDictionary(k => k.Key, v => String.Format(",",v.Value));

Note you'll need to add using System.Linq to the top of your file to use ToDictionary

Final test code:

var result = "{ \"Fruits\":[{\"Number\":\"111\", \"Name\":\"Apple\"}, {\"Number\":\"112\", \"Name\":\"Orange\"},{\"Number\":\"113\", \"Name\":\"Peach\"}]}";
Dictionary<string, dynamic> data = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(result);
var customTokens = new Dictionary<string, List<string>>();
foreach (var dataField in data)
{
    if (dataField.Value is JArray)
    {         
        foreach (JObject content in dataField.Value.Children<JObject>())
        {
            foreach (JProperty prop in content.Properties())
            {
                if(customTokens.ContainsKey(prop.Name))
                {
                    customTokens[prop.Name].Add(prop.Value.ToString());
                }
                else
                {
                    customTokens.Add(prop.Name, new List<string> { prop.Value.ToString() });
                }
            }

        }

        foreach(var item in customTokens)
        {
            Console.WriteLine(item.Key + ":" + String.Join(",", item.Value));
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is what I want to achieve.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.