cancel
Showing results for 
Search instead for 
Did you mean: 

Web Service JSON Path parsing

MateuszBartczak
Level 4
Hello Everyone,

I'm struggling recently with an error generated by Web Service action that has following Json Path applied as output parameter: $..issues
  • .key

    The error thrown at the process layer is: ERROR: Internal : Unexpected error Path returned multiple tokens.

    The problem is that I indeed want to receive multiple tokens as an output, however, I have no idea how to put this in a collection or text variable...

    I will be grateful for any help.

    Thanks and Best Regards,
    Mateusz

    ------------------------------
    Mateusz Bartczak
    ------------------------------
  • 8 REPLIES 8

    Hi, Mateusz,

    I am afraid that you need to write your own function for that. Blue Prism Web API does not support (or has not supported yet) the multiple tokens. It is a pity and I raised an enhancement request some time ago (https://community.blueprism.com/innovate/ideas/viewidea?IdeationKey=fb08bdac-8f18-4ac8-95e8-0a2e668393c8) so if you want to help pushing it into the main product please vote for it. That's all what I can advice at this moment.

    Regards,

    Zdenek

    ------------------------------
    Zdeněk Kabátek
    Head of Professional Services
    NEOOPS
    http://www.neoops.com/
    Europe/Prague
    ------------------------------

    Hi Zdenek,

    Thank you for answer. Do you have any sample of working code for that? I was trying to use Newtosoft for that and tried something like:
    JObject o = JObject.Parse(Response_Content);
    JToken i = o.SelectToken("$..issues
  • .key");​
  •  
    and then split the token or use foreach to put it in a datatable but unfortunetely it's not working too (still the same error of multiple tokens)

    Thanks & best regards

    Mateusz

    ------------------------------
    Mateusz Bartczak
    ------------------------------

    Based on the example JSON you provided in the other thread here's some code that parses the array.

    JArray topArray = JArray.Parse(json);
    JArray issues = (JArray)array.SelectToken(query);
    
    foreach (JObject issue in issues)
      {
        string id = (string)issue["id"];
        string expand = (string)issue["expand"];
        string self = (string)issue["self"];
        string key = (string)issue["key"];
    
        // Do something with the values (ex. populate a DataTable and return to process as Collection.
    
    }
    


    Keep in mind, this is loading an array at the beginning. If you full JSON is actually an object you'd need to do a JObject.Parse() to load it and then use SelectToken() to pull out the issues array.

    Cheers,



    ------------------------------
    Eric Wilson
    Director, Partner Integrations for Digital Exchange
    Blue Prism
    ------------------------------

    Hi Mateusz,

    From your Json path I assume each object inside issues array has key which you are trying to retrieve, right? you can try out following where inside foreach you can write what you want to do or just put it in Datatable to return a collection.

    IEnumerable<JToken> i = o.SelectTokens("$..issues
  • .key"); foreach(JToken j in i) { Console.WriteLine(j.ToString()); }


  • ------------------------------
    Gopal Bhaire
    Analyst
    Accenture
    ------------------------------

    Hi Eric,

    Unfortunately, I tried this method with JSON object as my Response_Content is an Object but code below is throwing same error as simple JSON Path:

    Internal : Unexpected error Cannot execute action "Get Assigned Issues" due to an error when running custom code: Path returned multiple tokens.

    JObject o = JObject.Parse(Response_Content);
    JToken issues = o.SelectToken("$..issues
  • .key"); var dataTable = new DataTable(); dataTable.Columns.Add("id"); dataTable.Columns.Add("expand"); dataTable.Columns.Add("self"); dataTable.Columns.Add("key"); foreach (JObject issue in issues) { DataRow row = dataTable.NewRow(); row["id"] = (string)issue["id"]; row["expand"] = (string)issue["expand"]; row["self"] = (string)issue["self"]; row["key"] = (string)issue["key"]; dataTable.Rows.Add(row); } Issues = dataTable;


  • ------------------------------
    Mateusz Bartczak
    ------------------------------

    Change you JSON Path query to this:

    $..issues

    That should return the issues array. Then the foreach should work.

    Cheers,

    ------------------------------
    Eric Wilson
    Director, Partner Integrations for Digital Exchange
    Blue Prism
    ------------------------------

    Hi Gopal,

    Quite funny because this piece of code actually worked although there is no huge change in relation to Eric code...:

    JObject o = JObject.Parse(Response_Content);
    IEnumerable<JToken> i = o.SelectTokens("$..issues
  • .key"); var tempIssue = ""; foreach(JToken j in i) { tempIssue = tempIssue +","+(j.ToString()); } Issues = tempIssue;​
  • For the testing purposes I was able to return it to a string with separator 🙂

    Thanks!!!



    ------------------------------
    Mateusz Bartczak
    ------------------------------

    You are right, it worked too. Many thanks!

    ------------------------------
    Mateusz Bartczak
    ------------------------------