1

I trying to pass JSON as an argument to python script using Powershell but everything getting new issues in python script. I am new to python so not sure whats wrong with script.

import json, sys, traceback
import requests
from aws_requests_auth.aws_auth import AWSRequestsAuth

try:
   if(len(sys.argv)>1):
        print("*** json.dumps() ***")
        print(json.dumps(sys.argv[1]))
        print("**** json.loads() ****")
        print(json.loads(sys.argv[1]))
        print("**** json.dumps-loads() ****")
        print(json.loads(json.dumps(sys.argv[1])))
        jsonInput = json.loads(json.dumps(sys.argv[1]));

        print(jsonInput['endpoint']);
        print(jsonInput['headers']);
        print(jsonInput['method']);


except Exception as e:
    output ={}
    output['error_message'] = e
    output['status_code']='1050'
    print(output)

This is how I am running command in PowerShell:

python C:\Folder\server\agent\scripts\PythonLibrary\cloudAPIInvoker.py '"{""endpoint"":""https://XXXXXX.execute-api.us-west-2.vpce.amazonaws.com/dev/transit-connectivity/api/v1/user"",""method"":""post"",""headers"":{""x-apigw-api-id"":""xxxx""},""queryParam"":"""",""body"":{""site_id"":""XXXXXXX"",""account_id"":""XXXXX"",""change_request"":""RITM0021337"",""customer_id"":""0000000"",""provider"":""aws"",""customer"":""new"",""region"":""us-east-1"",""network_id"":""vpc-00000000"",""cidr"":""10.10.10.0/00"",""egress_filter"":[""""],""route_tables"":[],""transit_connected"":true,""high_availability"":true,""service_now_notify"":true,""public_subnets"":[""10.10.10.0/00"",""10.10.00.00/00""]}}"'

Here is the output: Anyone has seen a similar issue?

*** json.dumps() ***
"{\"endpoint\":\"https://XXXXXX.execute-api.us-west-2.vpce.amazonaws.com/dev/transit-connectivity/api/v1/user\",\"method\":\"post\",\"headers\":{\"x-apigw-api-id\":\"xxxx\"},\"queryParam\":\"\",\"body\":{\"site_id\":\"XXXXXXX\",\"account_id\":\"XXXXX\",\"cha
nge_request\":\"RITM0021337\",\"customer_id\":\"0000000\",\"provider\":\"aws\",\"customer\":\"new\",\"region\":\"us-east-1\",\"network_id\":\"vpc-00000000\",\"cidr\":\"10.10.10.0/00\",\"egress_filter\":[\"\"],\"route_tables\":[],\"transit_connected\":true,\"
high_availability\":true,\"service_now_notify\":true,\"public_subnets\":[\"10.10.10.0/00\",\"10.10.00.00/00\"]}}"
**** json.loads() ****
{'endpoint': 'https://XXXXXX.execute-api.us-west-2.vpce.amazonaws.com/dev/transit-connectivity/api/v1/user', 'method': 'post', 'headers': {'x-apigw-api-id': 'xxxx'}, 'queryParam': '', 'body': {'site_id': 'XXXXXXX', 'account_id': 'XXXXX', 'change_request': 'R
ITM0021337', 'customer_id': '0000000', 'provider': 'aws', 'customer': 'new', 'region': 'us-east-1', 'network_id': 'vpc-00000000', 'cidr': '10.10.10.0/00', 'egress_filter': [''], 'route_tables': [], 'transit_connected': True, 'high_availability': True, 'servi
ce_now_notify': True, 'public_subnets': ['10.10.10.0/00', '10.10.00.00/00']}}
**** json.dumps-loads() ****
{"endpoint":"https://XXXXXX.execute-api.us-west-2.vpce.amazonaws.com/dev/transit-connectivity/api/v1/user","method":"post","headers":{"x-apigw-api-id":"xxxx"},"queryParam":"","body":{"site_id":"XXXXXXX","account_id":"XXXXX","change_request":"RITM0021337","cu
stomer_id":"0000000","provider":"aws","customer":"new","region":"us-east-1","network_id":"vpc-00000000","cidr":"10.10.10.0/00","egress_filter":[""],"route_tables":[],"transit_connected":true,"high_availability":true,"service_now_notify":true,"public_subnets"
:["10.10.10.0/00","10.10.00.00/00"]}}
{'error_message': TypeError('string indices must be integers'), 'status_code': '1050'}
0

1 Answer 1

1

Change

# WRONG: json.dumps() converts the input string to a *JSON string value*,
#        which json.loads() then reconverts to a string.
#        The whole operations is equivalent to:
#           jsonInput = sys.argv[1]
jsonInput = json.loads(json.dumps(sys.argv[1]));

to:

# OK: parse the JSON text as-is.
jsonInput = json.loads(sys.argv[1]);

json.dumps() is for converting Python objects to JSON. In your case, that object was a string, and the result was a single JSON string value.

Applying json.loads() to the result effectively reconverts that JSON string value to the original string, and the only way to index into a string object is to use a numeric (character-position) index - hence the error message.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.