Conditional to capture JSON information via Pyhton

Asked

Viewed 44 times

-1

If anyone can help me.. I have a Python script that reads a file. json that contains the web services I need to interact with. At first, my json only needed to contain a service and the script works smoothly, but now I need this json to receive more services. How could I get the . py file to run the first service and then the second, third.... until I finish the list?

Code . json

{
    "service":[
       {
          "portalURL":"URL PORTAL WEB",
          "fsURL":"URL serviço WEB", 
          "fsLayerNum":0,
          "serviceuser":"Minha conta",
          "servicepw":"Minha senha",
          "fieldstoreport":["*"],
          "viewerMapLevel":19
       },
       {
        "portalURL":"URL PORTAL WEB",
        "fsURL":"URL serviço WEB", 
        "fsLayerNum":0,
        "serviceuser":"Minha conta",
        "servicepw":"Minha senha",
        "fieldstoreport":["*"],
        "viewerMapLevel":19
       }
    ],
       "filenames":{
       "lasteditfile":"lastedit.json"
    }
 }

File step . py currently reading the first service contained in . json.

fsurl = cfg['service']['fsURL']
    if fsurl[-1] == '/':
        urlLyr = '{}{}'.format(fsurl, cfg['service']['fsLayerNum'])
    else:
        urlLyr = '{}/{}'.format(fsurl, cfg['service']['fsLayerNum'])

    portalURL = cfg['service']['portalURL']
    if portalURL[4] == ':':
        portalURL = portalURL.replace(':', 's:')

    viewerurl = '{}/home/webmap/viewer.html'.format(portalURL)
    sharingurl = '{}/sharing'.format(portalURL)

1 answer

0

By what I could understand you would need to go through the array of services to use each service individually. See more on Documentation of W3schools

for service in cfg['service']:
    fsurl = service['fsURL']
    # Aqui você continua o seu código, substituindo cfg['service'] por service

Browser other questions tagged

You are not signed in. Login or sign up in order to post.