Creating and storing Json array with a for each loop to be able to post

Scenario - I have arrays that I would like to store in a json array to be able to post.

Array values can be seen in image attached. I have a set of companyids in an array, the number of companyids may not always be two. So with a for each activity based on the number of companyids this will determine the iteration count. The issue I’m facing is that with after every iteration the previous values are overridden. Is there a way to store the value so the final json array looks like the below image. I’ve attached images of array values, activities and the intended output for json array.

Any advice is welcome.

Img 1 Array & values

img 2/3 activities used


img 4 current output
image004

img 5 intended output

Hi @duaine.b ,

The reason JArray values getting replaced is you need to append Jarray values into a list before adding them into “InsuredCompanyInfo” key.

I tried a backtracking approach, refer attached. Here I created your expected post Json object quick and fast with respective to CompanyId array’s length using for each and vb.net.

CreateJsonFromArray.zip (4.2 KB)

If you find this as works for you, mark it as solution.
Thanks,
Happy Automating :smile:

2 Likes

Hi Priya this works perfectly the only issue i’m facing is trying to assign this to a jobect to create the below.

initial Jobject:
{
“GUID”: “e080fdc6-20e2-4661-bb45-df27fb58d619”,
“ClientInfo”: {
“DelphiScore”: “999”,
“DocumentKey”: “76149c73-45de-477c-bc27-0a8f2f6ad46b”,
“DocumentUrl”: “Test document via API.pdf”
}

the one your code help created:
{
“InsuredCompaniesInfo”: [
{
“InsuredCompanyId”: 138,
“CompanyInfo”: {
“DelphiScore”: “123”,
“DocumentKey”: “76149c73-45de-477c-bc27-0a8f2f6ad46b”,
“DocumentUrl”: “Test document via API.pdf”
}
},
{
“InsuredCompanyId”: 139,
“CompanyInfo”: {
“DelphiScore”: “456”,
“DocumentKey”: “76149c73-45de-477c-bc27-0a8f2f6ad46b”,
“DocumentUrl”: “Test document via API.pdf”
}
}
]
}

But i would like to add both of these together to create :

{
“GUID”: “e080fdc6-20e2-4661-bb45-df27fb58d619”,
“ClientInfo”: {
“DelphiScore”: “999”,
“DocumentKey”: “76149c73-45de-477c-bc27-0a8f2f6ad46b”,
“DocumentUrl”: “Test document via API.pdf”
},
“InsuredCompaniesInfo”: [
{
“InsuredCompanyId”: 138,
“CompanyInfo”: {
“DelphiScore”: “123”,
“DocumentKey”: “76149c73-45de-477c-bc27-0a8f2f6ad46b”,
“DocumentUrl”: “Test document via API.pdf”
}
},
{
“InsuredCompanyId”: 139,
“CompanyInfo”: {
“DelphiScore”: “456”,
“DocumentKey”: “76149c73-45de-477c-bc27-0a8f2f6ad46b”,
“DocumentUrl”: “Test document via API.pdf”
}
}
]
}

JObject.FromObject(
New With {
key .InsuredCompanyId =in_arrCompanyId(ForLoopIndex),
key .CompanyInfo = New With {
key .DelphoScore =in_arrScore(ForLoopIndex),
key .DocumentKey = in_arrDocumentKey(ForLoopIndex),
key .DocumentUrl = in_strDocUrl,
key.IsSuccess = in_arrIsSuccess(ForLoopIndex),
key.Comment = in_arrComment(ForLoopIndex)
}
}
)

JObject.FromObject(
New With {
key .InsuredCompanyInfo = JArray.FromObject(InsuredCompanyList)
}
)

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.