1

I get from another app an HTTP request with a JSON payload like this:

{
    "reportName": "myfile",
    "type_1": "pdf",
    "paramm": [
        { "REF": "value1"},
        { "whatevervalue2": "whatevervalue2" }
    ]
}

I receive the data and try to process it. I created a class to get the data from the JSON:

public class ReportId
{
    public string reportName { get; set; }
    public string type_1 { get; set; }
    public object[] paramm { get; set; }
}

And here is the method where I process all the data on the API controller:

[HttpPost]
public IActionResult generate([FromBody] ReportId jsonResult)
{
    try
    {
        var reportName = jsonResult.reportName;
        var type = jsonResult.type_1;
        var recParam = jsonResult.paramm;
//....

        List<ReportParameter> parameters = new List<ReportParameter>();
        foreach (var t in recParam)
        {
            string[] paramName;
            paramName = t.ToString().Split(":");
            parameters.Add(new ReportParameter() 
            { 
                Name = paramName[0], 
                Labels = new List<string>() { paramName[1] }, 
                Values = new List<string>() { paramName[1] } 
            });
        }
        reportWriter.SetParameters(parameters);
//....
    }
    catch
    {
        return null;
    }
}

I got no problems with reportName and type_1 but on paramm it gets kinda wonky. I need to get the values of that array from the JSON. I do manage to get them, but with "all the format". For example, after the split on the first one I would get:

"{\r\n  \"REF\""
" \"value1\"\r\n}"

Is there any more elegant way to get the values from that paramm array without having to replace the characters that are not part of the "real" string?

Thanks in advance.

[EDIT for clarification]
On the JSON I'll get multiple parameters:

    "paramm": [
        { "parameter1": "value1" },
        { "parameter2": "value2" },
        { "parameter3": "value3" }
    ]

The parameter name could be any word and the value could be any as well. Those parameters are being sent to an RDL to filter some queries, so I need to get the name (parameter1, 2, 3...) and its value and add it as a parameter on the iteration of each.

3
  • @itsme86 they are... Model binding indeed uses either Json.Net or .Net's own JSON parser... For some reasons they don't want to declare what looks like dictionary as dictionary so... Commented Jul 16, 2020 at 16:31
  • vvic - please clarify what exactly you expect and want to achieve from that JSON. To outside observer it looks like just regular dictionary... Is the problem that you have duplicated keys (also even for that there are plenty of questions already)? Commented Jul 16, 2020 at 16:33
  • edited for clarification. Thanks Commented Jul 16, 2020 at 17:34

2 Answers 2

3

Yes, there is an easier way.

In your ReportId class, change the paramm member to be a List<Dictionary<string, string>> instead of object[].

public class ReportId
{
    ...
    public List<Dictionary<string, string>> paramm { get; set; }
}

Then, in your generate method you can just get the key and value from each dictionary instead of needing to do string manipulation:

List<ReportParameter> parameters = new List<ReportParameter>();
foreach (var dict in recParam)
{
    var kvp = dict.First();
    parameters.Add(new ReportParameter() 
    { 
        Name = kvp.Key, 
        Labels = new List<string>() { kvp.Value }, 
        Values = new List<string>() { kvp.Value } 
    });
}
Sign up to request clarification or add additional context in comments.

2 Comments

Working like a charm actually. Thanks a lot.
No problem; glad I could help.
3

I guess, Your ReportId class should be like this. I hope this will work.

public partial class ReportId
{
    [JsonProperty("reportName")]
    public string ReportName { get; set; }

    [JsonProperty("type_1")]
    public string Type1 { get; set; }

    [JsonProperty("paramm")]
    public Paramm[] Paramm { get; set; }
}

public partial class Paramm
{
    [JsonProperty("REF", NullValueHandling = NullValueHandling.Ignore)]
    public string Ref { get; set; }

    [JsonProperty("whatevervalue2", NullValueHandling = NullValueHandling.Ignore)]
    public string Whatevervalue2 { get; set; }
}

2 Comments

Close to what I need I think. Paramm are just a variable number of parameters i need to receive (could be 2, 3 or even none). They will consist on a name ("REF" in the example but it could be any) and a value. Then I need to iterate on those parameters and assign them to parameters.Add
var ri = new ReportId(); ri.Paramms = new Paramm[10]; ri.Paramms[0]= new Paramm(){Ref="asd",Whatevervalue2 = "asd"};your answer works as well, but you need to define the size of the array, so a List<T> is a bit nicer and handy when using

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.