Forum Discussion

Yuriy_Babay's avatar
Yuriy_Babay
New Contributor
6 years ago
Solved

How to get all the ID's one by one

Hi all,

I have next task and understand that I need a groovy script for this.

I have next response with a few countries: 

  {
"id" : "ABW",
"iso2Code" : "AW",
"name" : "Aruba",
"region" : {
"id" : "LCN",
"iso2code" : "ZJ",
"value" : "Latin America & Caribbean "
},
"adminregion" : {
"id" : "",
"iso2code" : "",
"value" : ""
},
"incomeLevel" : {
"id" : "HIC",
"iso2code" : "XD",
"value" : "High income"
},
"lendingType" : {
"id" : "LNX",
"iso2code" : "XX",
"value" : "Not classified"
},
"capitalCity" : "Oranjestad",
"longitude" : "-70.0167",
"latitude" : "12.5167"
},
{
"id" : "ARG",
"iso2Code" : "AR",
"name" : "Argentina",
"region" : {
"id" : "LCN",
"iso2code" : "ZJ",
"value" : "Latin America & Caribbean "
},

 

And I need to put int the veriable all the ID's one by one: Example "id" : "ABW",

I'm using Property transfer for this and I'm able to select only one ID: $[1][0]['id'] - X[ath to the first one.

 

How can I select all of them?

 

Thank you in advance

 

  • Hi,

     

    Are you saying you have a variable number of 'id' attributes in your response or are they a fixed number?

     

    You don't necessarily need groovyscript for this - unless you haven't got the Pro version??  Which version are you using?

     

    Also - it's difficult to provide the relevant jsonpath as you .json is actually malformed.  

     

    Can you provide the wellformed .json?   It looks like you just copied in the objects corresponding to a single record and added in a comma and copied in the objects to a second record but this returns malformed .json so it'll never work anyway.

     

    Once we've got the wellformed json - it'll be a lot easier to give you the jsonpath you need I suspect - although I always struggle with jsonpath relative to xpath.

     

    thanks,

     

    richie

3 Replies

  • richie's avatar
    richie
    Community Hero

    Hi,

     

    Are you saying you have a variable number of 'id' attributes in your response or are they a fixed number?

     

    You don't necessarily need groovyscript for this - unless you haven't got the Pro version??  Which version are you using?

     

    Also - it's difficult to provide the relevant jsonpath as you .json is actually malformed.  

     

    Can you provide the wellformed .json?   It looks like you just copied in the objects corresponding to a single record and added in a comma and copied in the objects to a second record but this returns malformed .json so it'll never work anyway.

     

    Once we've got the wellformed json - it'll be a lot easier to give you the jsonpath you need I suspect - although I always struggle with jsonpath relative to xpath.

     

    thanks,

     

    richie

  • nmrao's avatar
    nmrao
    Champion Level 3
    Please provide valid json. What's desired output?
  • Yuriy_Babay's avatar
    Yuriy_Babay
    New Contributor

    Thank you all,

    found a solution. Used data source for this.

     

    Thanks,

    Yuriy