4. RPA Challenge - Json Parsing

json

#1

Given the attached json file write an automation that outputs (Write Line) the name, age and mobile for the persons that are over 30 years and do have a mobile number.

inputJson.json (1.3 KB)

[
{
     "firstName": "John",
     "lastName": "Smith",
     "age": 31,
     "address":
     {
         "streetAddress": "21 2nd Street",
         "city": "New York",
         "state": "NY",
         "postalCode": "10021"
     },
     "phoneNumber":
     [
         {
           "type": "home",
           "number": "212 555-1234"
         }
     ]
 },
 {
     "firstName": "John",
     "lastName": "Doe",
     "age": 27,
     "address":
     {
         "streetAddress": "21 2nd Street",
         "city": "New Delhi",
         "state": "NY",
         "postalCode": "10021"
     },
     "phoneNumber":
     [
         {
           "type": "home",
           "number": "212 555-1234"
         },
		 		 {
           "type": "mobile",
           "number": "212 555-1234"
         }
     ]
 },
 {
     "firstName": "Maria",
     "lastName": "Smith",
     "age": 34,
     "address":
     {
         "streetAddress": "21 2nd Street",
         "city": "Michigan",
         "state": "MI",
         "postalCode": "99971"
     },
     "phoneNumber":
     [
         {
           "type": "home",
           "number": "212 555-1234"
         },
		 {
           "type": "mobile",
           "number": "212 555-1234"
         }
     ]
 }
 ]

Extraction of Data from a JSON
How to read array from .json file?
Automation Challenge Winners
How to read/write xml /json in uipath
#2

Hello!

I think that should have a error on the JSON file because I’m trying to do the specified filters and It’s not working.

Anyway, I have an example of JSON filtering If you guys need It.

What It does:

1) Filter the books by Price (> $12.00)
2) Filter the book by name (The Lord of the Rings)
3) Returns the ISBN (International Standard Book Number) from the filtered book

Follow the artifact on attach.

RPA_Challenge_JSONfilter_Lucas.Pimenta.zip (2.4 KB)

Hope it helps!

Regards,


#3

The JSON file from @badita has no errors, so it has to be something in your code. Note that the phoneNumber node is an array which could be throwing your code off.

Your example should point people into correct solution though, albeit it’s made in a way to return a single value (the topic that this spurred from was about filtering a collection).

Regards,
Andrzej


#4

Maybe you’re right but I has used the same way to filter another JSON Array file and have successfully filtered the information. But this one, I doesn’t.

Could you please show to us how to do with @badita JSON File? :wink:

Regards,


#5

it is throwing error on the first line of the json file.


#6

Like I said, there’s no error :wink:
It’s just that the whole file is not a strict JSON, but a JSON array in itself - if you use Deserialize JSON Array activity instead, it’ll work.

Solution attached - Linq and SelectToken variants.

Sidenote - for better view if it’s correct change the phone numbers in the file so they’re not all identical.

Main.xaml (9.5 KB)


Extraction of Data from a JSON
Formula to read value from json
#7

Yes, this was the catch :))

It is throwing error because the json itself is an array.
In this case Deserialize Json will give error. However, Deserialize Json Array does the job.


#8

You’re right.

Congratulations :smile:

Regards,


#9

I was light years away from achieving it LOOOL

I was trying to reading the file directly in the deserialize activity (to be honest I was trying with deserialize array then I was like how dummy lol)

many many thanks, these RPA challenges are getting us up to speed with all the possibilities in robot automation.

Thanks @badita @Lucas.Pimenta @andrzej.kniola


#10

I was so close to achieve it but I had some problems with the filters using the JSON array :frowning2:

I was trying to do:.SelectTokens("$[?(@.age > 30 && @.phoneNumber.type == ‘mobile’)]")

Thanks to @andrzej.kniola that solved the problem.

Regards,


#11

In this case here’s an “old school” version that avoids using LINQ and SelectTokens.

Json.xaml (12.4 KB)


#12

For any newbies this requires the UiPath.Web.Activities to avoid the red message of death… :slight_smile:


#13

Main.xaml (8.9 KB)

Hi

I have attached a solution here. Do you guys still give feedback on solutions?


#14

Hi @badita . I have taken your Json.xaml solution and want to output to a data table.

To do this, I have used this example from @vvaidya, but get the error “: Unexpected JSON token when reading DataTable. Expected StartArray, got String. Path ‘’, line 1, position 13.”

The file I have developed is attached.

Your help would be greatly appreciated.Json.xaml (11.3 KB)


#15

If I want to read json Objects then how can we iterate?

My Json Format :-
{
“Claims Processing”: {
“Index”: 2,
“IsMainMenu”: true
},
“Hospital Claims Processing”: {
“Index”: 4,
“IsMainMenu”: false
}
}

I have got the JSONObject but not able to read the values


#16

hi,

Please follow below video it is well explained,

thanks,
Karthik,


#17

How can I get all the key items that this token includes. For example from the above code I want to have “type” and “number”?


#18

How can I get all the key items that this token includes. For example from the above code I want to have “type” and “number”?,
Do you know any way to do it?


#19

#20