'parse description fields from JSON response returned by JIRA rest API
I'm trying to parse the JIRA issue fields returned by JIRA REST API v3 but I'm not able to parse the description fields as it is returned in ADF format and the structure or level of child objects are unknown. i have tried to use recursion but it didn't worked.
JSON response
{
"version": 1,
"type": "doc",
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "As a Community User, I want to be able to see the Timestamp to show when a feature article is published in minutes (date and Time), so that the the published articles can be organized from the article most recently published.",
"marks": [
{
"type": "em"
},
{
"type": "textColor",
"attrs": {
"color": "#ff5630"
}
}
]
},
{
"type": "text",
"text": " ",
"marks": [
{
"type": "textColor",
"attrs": {
"color": "#ff5630"
}
}
]
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "Acceptance Criteria",
"marks": [
{
"type": "strong"
},
{
"type": "underline"
}
]
}
]
},
{
"type": "bulletList",
"content": [
{
"type": "listItem",
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": " As a Community User, when I publish new "
},
{
"type": "text",
"text": "articles",
"marks": [
{
"type": "strong"
}
]
},
{
"type": "text",
"text": ", I can see my new article published at the top of all recently published articles from the News Tab."
}
]
}
]
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": " "
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "Solution",
"marks": [
{
"type": "strong"
},
{
"type": "underline"
}
]
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "How are we going to solve their problem in Salesforce?"
}
]
},
{
"type": "bulletList",
"content": [
{
"type": "listItem",
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": " Change Timestamp to show date/time and confirm that the published articles list is showing the most recently published articles at the top. "
}
]
}
]
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "Test Script",
"marks": [
{
"type": "strong"
},
{
"type": "underline"
}
]
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "Once the solution is completed, fill out the steps for testing.",
"marks": [
{
"type": "em"
}
]
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": " "
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "Deliverables",
"marks": [
{
"type": "strong"
},
{
"type": "underline"
}
]
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "What needs to be built and documented?",
"marks": [
{
"type": "em"
}
]
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": " "
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "Components",
"marks": [
{
"type": "strong"
},
{
"type": "underline"
}
]
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "List of components that were created or updated for the user story and will need to be deployed to any target environment.",
"marks": [
{
"type": "em"
}
]
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": " "
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "Deployment Steps",
"marks": [
{
"type": "em"
},
{
"type": "strong"
},
{
"type": "underline"
}
]
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "How will this solution be deployed to the target environment?",
"marks": [
{
"type": "em"
}
]
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "Client Notes",
"marks": [
{
"type": "strong"
},
{
"type": "textColor",
"attrs": {
"color": "#6554c0"
}
},
{
"type": "underline"
}
]
}
]
},
{
"type": "bulletList",
"content": [
{
"type": "listItem",
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "New item – When I published a Featured article and it appears on the News tab with all other published articles, the time stamp is very confusing. I expected the timestamp to show “X minutes,” but instead it shows “in a day.” The expected result is that the article’s timestamp as a published article would be based on the published date and time regardless of whether it was for featured or recent news. The timestamp of “in a day” also caused it to appear below several other articles in the list even though it should have appeared as the first article since it was the one that was most recently published. I have attached screen shots above.",
"marks": [
{
"type": "textColor",
"attrs": {
"color": "#6554c0"
}
}
]
}
]
}
]
}
]
}
]
}
I need the list of all the content objects in sequential order starting from the first to the last content object, with property values type and text. any help will be much appreciated.
Solution 1:[1]
Regarding your JSON, you need the following Model structure to deserialize your string. Once you get your data, you can add it to a List and then sort it as required:
public class Attrs
{
public string color { get; set; }
}
public class Mark
{
public string type { get; set; }
public Attrs attrs { get; set; }
}
public class Content
{
public string type { get; set; }
public string text { get; set; }
public List<Mark> marks { get; set; }
public List<Content> content { get; set; }
}
public class Root
{
public int version { get; set; }
public string type { get; set; }
public List<Content> content { get; set; }
}
You can refer to the below code snippet to achieve your functionality:
using System;
using Newtonsoft.Json;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
string jsonString = "{'version':1,'type':'doc','content':[{'type':'paragraph','content':[{'type':'text','text':'As a Community User, I want to be able to see the Timestamp to show when a feature article is published in minutes (date and Time), so that the the published articles can be organized from the article most recently published.','marks':[{'type':'em'},{'type':'textColor','attrs':{'color':'#ff5630'}}]},{'type':'text','text':' ','marks':[{'type':'textColor','attrs':{'color':'#ff5630'}}]}]},{'type':'paragraph','content':[{'type':'text','text':'Acceptance Criteria','marks':[{'type':'strong'},{'type':'underline'}]}]},{'type':'bulletList','content':[{'type':'listItem','content':[{'type':'paragraph','content':[{'type':'text','text':' As a Community User, when I publish new '},{'type':'text','text':'articles','marks':[{'type':'strong'}]},{'type':'text','text':', I can see my new article published at the top of all recently published articles from the News Tab.'}]}]}]},{'type':'paragraph','content':[{'type':'text','text':' '}]},{'type':'paragraph','content':[{'type':'text','text':'Solution','marks':[{'type':'strong'},{'type':'underline'}]}]},{'type':'paragraph','content':[{'type':'text','text':'How are we going to solve their problem in Salesforce?'}]},{'type':'bulletList','content':[{'type':'listItem','content':[{'type':'paragraph','content':[{'type':'text','text':' Change Timestamp to show date/time and confirm that the published articles list is showing the most recently published articles at the top. '}]}]}]},{'type':'paragraph','content':[{'type':'text','text':'Test Script','marks':[{'type':'strong'},{'type':'underline'}]}]},{'type':'paragraph','content':[{'type':'text','text':'Once the solution is completed, fill out the steps for testing.','marks':[{'type':'em'}]}]},{'type':'paragraph','content':[{'type':'text','text':' '}]},{'type':'paragraph','content':[{'type':'text','text':'Deliverables','marks':[{'type':'strong'},{'type':'underline'}]}]},{'type':'paragraph','content':[{'type':'text','text':'What needs to be built and documented?','marks':[{'type':'em'}]}]},{'type':'paragraph','content':[{'type':'text','text':' '}]},{'type':'paragraph','content':[{'type':'text','text':'Components','marks':[{'type':'strong'},{'type':'underline'}]}]},{'type':'paragraph','content':[{'type':'text','text':'List of components that were created or updated for the user story and will need to be deployed to any target environment.','marks':[{'type':'em'}]}]},{'type':'paragraph','content':[{'type':'text','text':' '}]},{'type':'paragraph','content':[{'type':'text','text':'Deployment Steps','marks':[{'type':'em'},{'type':'strong'},{'type':'underline'}]}]},{'type':'paragraph','content':[{'type':'text','text':'How will this solution be deployed to the target environment?','marks':[{'type':'em'}]}]},{'type':'paragraph','content':[{'type':'text','text':'Client Notes','marks':[{'type':'strong'},{'type':'textColor','attrs':{'color':'#6554c0'}},{'type':'underline'}]}]},{'type':'bulletList','content':[{'type':'listItem','content':[{'type':'paragraph','content':[{'type':'text','text':'New item – When I published a Featured article and it appears on the News tab with all other published articles, the time stamp is very confusing. I expected the timestamp to show “X minutes,” but instead it shows “in a day.” The expected result is that the article’s timestamp as a published article would be based on the published date and time regardless of whether it was for featured or recent news. The timestamp of “in a day” also caused it to appear below several other articles in the list even though it should have appeared as the first article since it was the one that was most recently published. I have attached screen shots above.','marks':[{'type':'textColor','attrs':{'color':'#6554c0'}}]}]}]}]}]}";
var result =JsonConvert.DeserializeObject<Root>(jsonString);
Console.WriteLine(result.type);
Console.WriteLine(result.version);
foreach(var item in result.content)
{
foreach(var item1 in item.content)
{
Console.WriteLine(item1.marks);
Console.WriteLine(item1.text);
Console.WriteLine(item1.type);
}
}
}
}
public class Attrs
{
public string color { get; set; }
}
public class Mark
{
public string type { get; set; }
public Attrs attrs { get; set; }
}
public class Content
{
public string type { get; set; }
public string text { get; set; }
public List<Mark> marks { get; set; }
public List<Content> content { get; set; }
}
public class Root
{
public int version { get; set; }
public string type { get; set; }
public List<Content> content { get; set; }
}
Output:
doc
1
System.Collections.Generic.List`1[Mark]
As a Community User, I want to be able to see the Timestamp to show when a feature article is published in minutes (date and Time), so that the the published articles can be organized from the article most recently published.
text
System.Collections.Generic.List`1[Mark]
text
System.Collections.Generic.List`1[Mark]
Acceptance Criteria
text
listItem
text
System.Collections.Generic.List`1[Mark]
Solution
text
How are we going to solve their problem in Salesforce?
text
listItem
System.Collections.Generic.List`1[Mark]
Test Script
text
System.Collections.Generic.List`1[Mark]
Once the solution is completed, fill out the steps for testing.
text
text
System.Collections.Generic.List`1[Mark]
Deliverables
text
System.Collections.Generic.List`1[Mark]
What needs to be built and documented?
text
text
System.Collections.Generic.List`1[Mark]
Components
text
System.Collections.Generic.List`1[Mark]
List of components that were created or updated for the user story and will need to be deployed to any target environment.
text
text
System.Collections.Generic.List`1[Mark]
Deployment Steps
text
System.Collections.Generic.List`1[Mark]
How will this solution be deployed to the target environment?
text
System.Collections.Generic.List`1[Mark]
Client Notes
text
listItem
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
