0

I am trying to loop through some JSON to create some variables using a dictionary.

Here are the definitions that I am using:

public class Customfield10002
{
      public string name { get; set; }
}

public class Fields
{
    public string summary { get; set; }
    public List<Customfield10002> customfield_10002 { get; set; }
}

public class Issue
{
    public string id { get; set; }
    public string key { get; set; }
    public Fields fields { get; set; }
}

public class Root
{
    public List<Issue> issues { get; set; }
}

The following code validates fine.

Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(response.Content);

foreach (var issue in myDeserializedClass.issues)
            {
                string id = issue.id.ToString();
                string key = issue.key.ToString();
                string summary = issue.fields.summary.ToString();
            {

But when I include the following string field, it fails validation

string creatoremail = issue.fields.customfield_10002.name.ToString();

The error is:

'List<ITSDWorklogs.Customfield10002' does not contain a definition for 'name' and no extension method 'name' accepting a first argument of type" 'List<ITSDWorklogs.Customfield10002' could be found (are you missing a using directive or an assembly reference?)

How can I access the value from this nested list?

Any help greatly appreciated.

UPDATED with JSON

        {
      "expand": "names,schema",
      "startAt": 0,
      "maxResults": 1,
      "total": 151,
      "issues": [
        {
          "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
          "id": "10210",
          "self": "https://xxxxxxxxxx.atlassian.net/rest/api/3/issue/10210",
          "key": "ITSD-202",
          "fields": {
            "statuscategorychangedate": "2021-01-25T20:12:11.922+0000",
            "issuetype": {
              "self": "https://xxxxxxxxxx.atlassian.net/rest/api/3/issuetype/10003",
              "id": "10003",
              "description": "For customer support issues. Created by Jira Service Desk.",
              "iconUrl": "https://xxxxxxxxxx.atlassian.net/secure/viewavatar?size=medium&avatarId=10308&avatarType=issuetype",
              "name": "Support",
              "subtask": false,
              "avatarId": 10308
            },
            "timespent": null,
            "customfield_10030": null,
            "customfield_10031": {
              "id": "1",
              "name": "Time to resolution",
              "_links": {
                "self": "https://xxxxxxxxxx.atlassian.net/rest/servicedeskapi/request/10210/sla/1"
              },
              "completedCycles": [],
              "ongoingCycle": {
                "startTime": {
                  "iso8601": "2021-01-25T20:12:11+0000",
                  "jira": "2021-01-25T20:12:11.592+0000",
                  "friendly": "Yesterday 08:12",
                  "epochMillis": 1611605531592
                },
                "breachTime": {
                  "iso8601": "2021-02-11T14:30:00+0000",
                  "jira": "2021-02-11T14:30:00.000+0000",
                  "friendly": "11/Feb/21 02:30",
                  "epochMillis": 1613053800000
                },
                "breached": false,
                "paused": false,
                "withinCalendarHours": false,
                "goalDuration": {
                  "millis": 432000000,
                  "friendly": "120h"
                },
                "elapsedTime": {
                  "millis": 0,
                  "friendly": "0m"
                },
                "remainingTime": {
                  "millis": 432000000,
                  "friendly": "120h"
                }
              }
            },
            "project": {
              "self": "https://xxxxxxxxxx.atlassian.net/rest/api/3/project/10000",
              "id": "10000",
              "key": "ITSD",
              "name": "Customer Service Desk",
              "projectTypeKey": "service_desk",
              "simplified": false,
              "avatarUrls": {
                "48x48": "https://xxxxxxxxxx.atlassian.net/secure/projectavatar?pid=10000&avatarId=10412",
                "24x24": "https://xxxxxxxxxx.atlassian.net/secure/projectavatar?size=small&s=small&pid=10000&avatarId=10412",
                "16x16": "https://xxxxxxxxxx.atlassian.net/secure/projectavatar?size=xsmall&s=xsmall&pid=10000&avatarId=10412",
                "32x32": "https://xxxxxxxxxx.atlassian.net/secure/projectavatar?size=medium&s=medium&pid=10000&avatarId=10412"
              }
            },
            "customfield_10032": {
              "id": "2",
              "name": "Time to first response",
              "_links": {
                "self": "https://xxxxxxxxxx.atlassian.net/rest/servicedeskapi/request/10210/sla/2"
              },
              "completedCycles": [],
              "ongoingCycle": {
                "startTime": {
                  "iso8601": "2021-01-25T20:12:11+0000",
                  "jira": "2021-01-25T20:12:11.592+0000",
                  "friendly": "Yesterday 08:12",
                  "epochMillis": 1611605531592
                },
                "breachTime": {
                  "iso8601": "2021-01-26T12:30:00+0000",
                  "jira": "2021-01-26T12:30:00.000+0000",
                  "friendly": "Today 12:30",
                  "epochMillis": 1611664200000
                },
                "breached": false,
                "paused": false,
                "withinCalendarHours": false,
                "goalDuration": {
                  "millis": 14400000,
                  "friendly": "4h"
                },
                "elapsedTime": {
                  "millis": 0,
                  "friendly": "0m"
                },
                "remainingTime": {
                  "millis": 14400000,
                  "friendly": "4h"
                }
              }
            },
            "fixVersions": [],
            "customfield_10034": {
              "languageCode": "en",
              "displayName": "English"
            },
            "resolution": null,
            "customfield_10037": null,
            "customfield_10027": null,
            "customfield_10028": null,
            "customfield_10029": null,
            "resolutiondate": null,
            "workratio": -1,
            "lastViewed": "2021-01-25T20:12:11.936+0000",
            "watches": {
              "self": "https://xxxxxxxxxx.atlassian.net/rest/api/3/issue/ITSD-202/watchers",
              "watchCount": 1,
              "isWatching": true
            },
            "created": "2021-01-25T20:12:11.592+0000",
            "customfield_10020": null,
            "customfield_10021": null,
            "customfield_10022": null,
            "customfield_10023": null,
            "priority": {
              "self": "https://xxxxxxxxxx.atlassian.net/rest/api/3/priority/3",
              "iconUrl": "https://xxxxxxxxxx.atlassian.net/images/icons/priorities/medium.svg",
              "name": "Medium",
              "id": "3"
            },
            "customfield_10024": null,
            "customfield_10025": null,
            "labels": [],
            "customfield_10026": [],
            "customfield_10016": null,
            "customfield_10017": null,
            "customfield_10018": {
              "hasEpicLinkFieldDependency": false,
              "showField": false,
              "nonEditableReason": {
                "reason": "PLUGIN_LICENSE_ERROR",
                "message": "The Parent Link is only available to Jira Premium users."
              }
            },
            "customfield_10019": "0|i0013b:",
            "timeestimate": null,
            "versions": [],
            "assignee": {
              "self": "https://xxxxxxxxxx.atlassian.net/rest/api/3/user?accountId=12345678912345678912345",
              "accountId": "12345678912345678912345",
              "emailAddress": "[email protected]",
              "avatarUrls": {
                "48x48": "https://secure.gravatar.com/avatar/2758e009bbe7f2348bfdf52d653b6a2b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBE-1.png",
                "24x24": "https://secure.gravatar.com/avatar/2758e009bbe7f2348bfdf52d653b6a2b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBE-1.png",
                "16x16": "https://secure.gravatar.com/avatar/2758e009bbe7f2348bfdf52d653b6a2b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBE-1.png",
                "32x32": "https://secure.gravatar.com/avatar/2758e009bbe7f2348bfdf52d653b6a2b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBE-1.png"
              },
              "displayName": "First Last",
              "active": true,
              "timeZone": "Europe/London",
              "accountType": "atlassian"
            },
            "updated": "2021-01-25T20:12:14.321+0000",
            "status": {
              "self": "https://xxxxxxxxxx.atlassian.net/rest/api/3/status/10001",
              "description": "This was auto-generated by Jira Service Desk during workflow import",
              "iconUrl": "https://xxxxxxxxxx.atlassian.net/images/icons/status_generic.gif",
              "name": "Waiting for support",
              "id": "10001",
              "statusCategory": {
                "self": "https://xxxxxxxxxx.atlassian.net/rest/api/3/statuscategory/4",
                "id": 4,
                "key": "indeterminate",
                "colorName": "yellow",
                "name": "In Progress"
              }
            },
            "components": [],
            "customfield_10050": null,
            "customfield_10051": null,
            "timeoriginalestimate": null,
            "customfield_10052": null,
            "customfield_10053": null,
            "description": null,
            "customfield_10010": null,
            "customfield_10014": null,
            "customfield_10015": null,
            "customfield_10005": null,
            "customfield_10049": null,
            "customfield_10006": null,
            "customfield_10007": null,
            "security": null,
            "customfield_10008": null,
            "customfield_10009": null,
            "summary": "Barry Test",
            "creator": {
              "self": "https://xxxxxxxxxx.atlassian.net/rest/api/3/user?accountId=12345678912345678912345",
              "accountId": "12345678912345678912345",
              "emailAddress": "[email protected]",
              "avatarUrls": {
                "48x48": "https://secure.gravatar.com/avatar/2758e009bbe7f2348bfdf52d653b6a2b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBE-1.png",
                "24x24": "https://secure.gravatar.com/avatar/2758e009bbe7f2348bfdf52d653b6a2b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBE-1.png",
                "16x16": "https://secure.gravatar.com/avatar/2758e009bbe7f2348bfdf52d653b6a2b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBE-1.png",
                "32x32": "https://secure.gravatar.com/avatar/2758e009bbe7f2348bfdf52d653b6a2b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBE-1.png"
              },
              "displayName": "First Last",
              "active": true,
              "timeZone": "Europe/London",
              "accountType": "atlassian"
            },
            "customfield_10040": null,
            "customfield_10041": null,
            "customfield_10042": null,
            "customfield_10043": null,
            "reporter": {
              "self": "https://xxxxxxxxxx.atlassian.net/rest/api/3/user?accountId=12345678912345678912345",
              "accountId": "12345678912345678912345",
              "emailAddress": "[email protected]",
              "avatarUrls": {
                "48x48": "https://secure.gravatar.com/avatar/2758e009bbe7f2348bfdf52d653b6a2b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBE-1.png",
                "24x24": "https://secure.gravatar.com/avatar/2758e009bbe7f2348bfdf52d653b6a2b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBE-1.png",
                "16x16": "https://secure.gravatar.com/avatar/2758e009bbe7f2348bfdf52d653b6a2b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBE-1.png",
                "32x32": "https://secure.gravatar.com/avatar/2758e009bbe7f2348bfdf52d653b6a2b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBE-1.png"
              },
              "displayName": "First Last",
              "active": true,
              "timeZone": "Europe/London",
              "accountType": "atlassian"
            },
            "customfield_10044": null,
            "customfield_10000": "{}",
            "customfield_10001": null,
            "customfield_10045": null,
            "customfield_10046": null,
            "customfield_10002": [
              {
                "id": "4",
                "name": "xxxxxxxxxx.co.uk",
                "_links": {
                  "self": "https://xxxxxxxxxx.atlassian.net/rest/servicedeskapi/organization/4"
                }
              }
            ],
            "customfield_10003": null,
            "customfield_10047": null,
            "customfield_10048": null,
            "customfield_10004": null,
            "customfield_10038": null,
            "customfield_10039": null,
            "environment": null,
            "duedate": null,
            "progress": {
              "progress": 0,
              "total": 0
            }
          }
        }
      ]
    }
4
  • 2
    issue.fields.customfield_10002 is a list. A list doesn't have a property called "name". Each element might though. Commented Jan 26, 2021 at 16:00
  • can you add the json to the question? Commented Jan 26, 2021 at 16:11
  • @Sajid Thanks for looking, JSON added Commented Jan 26, 2021 at 16:23
  • @user3580480 if you have always one element in array, you can use Linq : issue.fields.customfield_10002.FirstOrDefaul()?.name Commented Jan 26, 2021 at 16:29

2 Answers 2

1

you can use LINQ for enumerate over Customfield10002 list:

var createOrEmail = issue.fields.customfield_10002.Select(c=>c.name).DefaultIfEmpty("").Aggregate((s, s1) => $"{s}, {s1}");
Sign up to request clarification or add additional context in comments.

3 Comments

I'm getting "Sequence contains no elements"
just use DefaultIfEmpty(). I update my answer
it has value "xxxxxxxxxx.co.uk" (from json)
1

This should work as suggested by @gunr2171

string creatoremail = "";
foreach (var names in issue.fields.customfield_10002)
{
    creatoremail = names.name.ToString();
}

or by Sajid

string creatoremail = issue.fields.customfield_10002.FirstOrDefault().name.ToString();

Regards

3 Comments

i think, in this case, you can use FirstOrDefault instead loop.
It should be tagged as linq as well
gunr2171 code was wrong. Edited just now. Added Sajid's code as well

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.