JSON Looping

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
khansjak
Newbie
Posts: 3
Joined: Sun Mar 04, 2018 6:52 pm

JSON Looping

Post by khansjak » Tue Mar 06, 2018 7:12 am

HI,

I have one scenario where i need to go through a loop in JSON

The data looks like this.

Code: Select all

{
    "meta": {
        "code": 200,
        "msg": "This is test"
    },
    "data": {
        "id": "69",
        "name":"newName"
        "roles": [
            {
                "vendorID": "15",
                "isLocal": false,
                "dName": "vendor2",
                "permissions": [
                    {
                        "roleID": "13",
                        "isDefaultRole": true,
                        "rPermission": [
                            {
                                "permission": 1,
                                "rName": "Dashboard_User"
                            }
                        ]
                    }
                ]
            },
            {
                "vendorID": "5",
                "isLocal": true,
                "dName": "vendor1",
                "permissions": [
                    {
                        "roleID": "13",
                        "isDefaultRole": true,
                        "rPermission": [
                            {
                                "permission": 1,
                                "rName": "Dashboard_User"
                            }
                        ]
                    }
                ]
            }
        ],
        
    }
}

Assume i have got all of this in a variable HTMLResult and then i start picking up the results

The code which i wrote looks like this

Code: Select all

JSONParse>HTMLResult,data,result
JSONParse>HTMLResult,data.roles[0],result


What i am trying to do here is Check
data>roles>
and then check if "isLocal" is true
then pick roleId for that node.

In order to do so , i will need to loop through this JSON on node : roles.
JSONPrase function cab be used if i can get count of the elements.

Is there a way ?

Go easy as this is first post and this can be pretty lame post for me in beginning.Appreciated effort whoever took time to read through.
Thanks
Javed

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: JSON Looping

Post by Marcus Tettmar » Tue Mar 06, 2018 12:40 pm

Hi,

First you have some syntax errors in your JSON. There's a comma missing after the name node under the data node. (After "name":"newName" you need a comma). There's also an erroneous comma right at the end. Unless you fix these it won't parse. My code below has the fixed up version in a label to you can test.

Now, once you've got that sorted, what I would do is loop through until an empy string is returned. In my example I've put the JSON in a label and used LabelToVar to get into a variable. In your case you'd be returning it via HTTP. But if those syntax errors are there you're going to have to deal with that at the source. For now, run my example below with the static copy:

Code: Select all

LabelToVar>theJSON,JSONstr

Let>k=0
While>isLocal<>{""}
  JSONParse>JSONstr,data.roles[%k%].isLocal,isLocal
  if>isLocal=true
    JSONParse>JSONstr,data.roles[%k%].permissions[0].roleID,roleID
    MDL>roleID
  Endif
  Let>k=k+1
EndWhile

/*
theJSON:
{
    "meta": {
        "code": 200,
        "msg": "This is test"
    },
    "data": {
        "id": "69",
        "name":"newName",
        "roles": [
            {
                "vendorID": "15",
                "isLocal": false,
                "dName": "vendor2",
                "permissions": [
                    {
                        "roleID": "13",
                        "isDefaultRole": true,
                        "rPermission": [
                            {
                                "permission": 1,
                                "rName": "Dashboard_User"
                            }
                        ]
                    }
                ]
            },
            {
                "vendorID": "5",
                "isLocal": true,
                "dName": "vendor1",
                "permissions": [
                    {
                        "roleID": "13",
                        "isDefaultRole": true,
                        "rPermission": [
                            {
                                "permission": 1,
                                "rName": "Dashboard_User"
                            }
                        ]
                    }
                ]
            }
        ]
        
    }
}
*/
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

khansjak
Newbie
Posts: 3
Joined: Sun Mar 04, 2018 6:52 pm

Re: JSON Looping

Post by khansjak » Wed Mar 07, 2018 5:26 am

Thanks ,
Let me check this. I realised there is some issue with JSON string inspite of that your effort to guide me though solution is much appreciated.

Javed

khansjak
Newbie
Posts: 3
Joined: Sun Mar 04, 2018 6:52 pm

Re: JSON Looping

Post by khansjak » Wed Mar 07, 2018 6:19 am

Thank you , It worked !.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts