Json For Each

Hi!

How do i loop through the elements within a JObject which does not contain an array?
ex:

{
“Id”: “123456”,
“Number”: “111”,
],
“Something”: [
{
“Id”: “123456”,
“Number”: “111”
]
}

I don’t want the “Id” within “Something”, since it’s an array.
How do i do this?

//Regards Mike

1 Like

Can you post the exact JSON you have @mikamol?

{
“Id”: “123-345_456-6789”,
“DiaryNumber”: “”,
“DateSubmitted”: “2019-07-01T12:17:50.743”,
“Ombudsman”: null,
“SentInAs”: “Citizen”,
“RequiresMultipleSignatures”: false,
“Status”: “Inkommet”,
“ServiceId”: “abcde”,
“ServiceName”: “Something creative”,
“ServiceVersion”: 23,
“ServiceStatuses”: [
“Wops”,
“Oh Crap”,
“Shit”,
“Aw Maan”
],
“Citizens”: [
{
“HasSigned”: false,
“IsCoApplicant”: false,
“UserIdentity”: “199910104444”,
“FirstName”: “James”,
“LastName”: “Bond”,
“Id”: 666
}
],
“Signatures”: ,
“Payments”: ,
“QueueItems”: ,
“Fields”: [
{
“Question”: {
#cdata-section”: “First Name”
},
“Answer”: {
#cdata-section”: “James”
}
},
{
“Question”: {
#cdata-section”: “Last Name”
},
“Answer”: {
#cdata-section”: “Bond”
}
},
{
“Question”: {
#cdata-section”: “Adress”
},
“Answer”: {
#cdata-section”: “Postal Address”
}
},
{
“Question”: {
#cdata-section”: “Postal Number”
},
“Answer”: {
#cdata-section”: “111 11”
}
},
{
“Question”: {
#cdata-section”: “City”
},
“Answer”: {
#cdata-section”: “Big City”
}
},
{
“Question”: {
#cdata-section”: “Telephone”
},
“Answer”: {
#cdata-section”: “1231231231230”
}
},
{
“Question”: {
#cdata-section”: “Mobile”
},
“Answer”: {
#cdata-section”: “0702020202”
}
},
{
“Question”: {
#cdata-section”: “Eventuell portkod”
},
“Answer”: {
#cdata-section”: “”
}
},
{
“Question”: {
#cdata-section”: “E-mail”
},
“Answer”: {
#cdata-section”: “someone@something.com
}
}
],
“Attachments”: [
{
“Id”: 0,
“Type”: “Pdf”,
“FileName”: “file.pdf”,
“SystemFileName”: “”,
}
],
“GroupId”: null
}

Yeah, perfect

Can you let me know which value you want to get from this?

Usually, deserialize json and store the value in output variable, then use for each activity to loop through json elements. This is working fine for me

If i want to find the key “Id” for example, i dont want the loop to search for it inside… say… “Citizens”.
How do i create a loop to search for several keys which does not search inside arrays?

Yeah, to loop inside the arrays, you need to get the output as JToken, then check the required.

This will give you the ID inside citizens @mikamol

jObj.Root.Item(“Citizens”).First.Item(“Id”)

Thanks!

What does “.Root.” do in that line?

And how do i just search among the ones without an array:

“Id”: “123-345_456-6789”,
“DiaryNumber”: “”,
“DateSubmitted”: “2019-07-01T12:17:50.743”,
“Ombudsman”: null,
“SentInAs”: “Citizen”,
“RequiresMultipleSignatures”: false,
“Status”: “Inkommet”,
“ServiceId”: “abcde”,
“ServiceName”: “Something creative”,
“ServiceVersion”: 23,
“ServiceStatuses”: [
“Wops”,
“Oh Crap”,
“Shit”,
“Aw Maan”

It will do nothing, if you have more JSON child elements, then it will help, but in your scenario, it will work without root or with root

Also, how do you get the property from a Token? (The value)

Can you elaborate what is the question @mikamol?

Once we give the jobj.Children(""), it will return JToken, again to get the inside json objects, we are giving FIrst, which will give us the first object in JTOken format, again inside that, we need ID. so we are giving as item(“ID”)

Sorry, i must be conused what i ment was when i got the token:

“Id”: “123-345_456-6789”,

How do i get the value (123-345_456-6789)?

try giving .Value in the end of the statement you have

Seems to only work for JProperty type?

Can you confirm you want to get the value of the first ID ? and how are you trying to retrieve that value?

Here this will give you the value of ID

jObj.Item(“Id”)

Sorry, i got it working now - i was just confused :slight_smile: Thanks for the help!

Got any idea how to fetch info when it looks like this?:

“Fields”: [
{
“Question”: {
#cdata-section”: “First Name”
},
“Answer”: {
#cdata-section”: “James”
}
]

Can you explain which values you want to fetch from above in the entire JSON attached above?

Under “Fields” the values i want to fetch is the ones under “Answer”, which i need to find by the value under “Question” somehow…:

So, for example, by searching for “First Name” i want to find “James”.

“Fields”: [
{
“Question”: {
#cdata-section”: “First Name”
},
“Answer”: {
#cdata-section”: “James”
}
},
{
“Question”: {
#cdata-section”: “Last Name”
},
“Answer”: {
#cdata-section”: “Bond”
}
},
{
“Question”: {
#cdata-section”: “Adress”
},
“Answer”: {
#cdata-section”: “Postal Address”
}
},
{
“Question”: {
#cdata-section”: “Postal Number”
},
“Answer”: {
#cdata-section”: “111 11”
}
},
{
“Question”: {
#cdata-section”: “City”
},
“Answer”: {
#cdata-section”: “Big City”
}
},
{
“Question”: {
#cdata-section”: “Telephone”
},
“Answer”: {
#cdata-section”: “1231231231230”
}
},
{
“Question”: {
#cdata-section”: “Mobile”
},
“Answer”: {
#cdata-section”: “0702020202”
}
},
{
“Question”: {
#cdata-section”: “Eventuell portkod”
},
“Answer”: {
#cdata-section”: “”
}
},
{
“Question”: {
#cdata-section”: “E-mail”
},
“Answer”: {
#cdata-section”: “someone@something.com
}
}
],

And is it possible to use “Contain” to find a JToken based on just a part of the identification?

For that you need to loop through the values inside the fields array @mikamol

here is the workflow which will help you with that, let me know if you have doubts

JSONNEw.xaml (9.0 KB)