0

I have a JSON dump that I am trying to post to a another application. In the payload there is a "pageGroups": "['TEST_TCIS:Oncall Schedule']", and the double quotes need to be removed to represent this output to pass the data: "pageGroups": ['TEST_TCIS:Oncall Schedule'],

I have tried to use replace but it fails. If I manually remove the double quotes out of the JSON payload it works.

Here is the code:

api_url_base = "some url"
headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer some token"
     }
data = {"abendCode": "",
        "action": "CREATE",
        "assignGroup": "TCIS-APP-SUPPORT",
        "component": "tcis",
        "description": "Test incident creation",
        "entered": "", "logicalName": "tcis",
        "pageGroups": "['TEST_TCIS:Oncall Schedule']",
        "priority": "",
        "racfId": "",
        "sendPage": "",
        "sysProdName": "tcis",
        "transactionId": "",
        "type": "SYSMAN"}

data = json.dumps(data)
data = data.replace("\"[", "[").replace("]\"", "]")
print(data)


response = requests.post("https://apistaging.csx.com/tcis-events/v1/events",
                         json=data, headers=headers)
response_url = json.loads(response.content.decode('utf-8'))
print(response_url)
print("response " + str(response))

Here is the output I am getting:

{"abendCode": "",
 "action": "CREATE",
 "assignGroup": "TCIS-APP-SUPPORT",
 "component": "tcis",
 "description": "Test incident creation",
 "entered": "",
 "logicalName": "tcis",
 "pageGroups": ['TEST_TCIS:Oncall Schedule'],
 "priority": "",
 "racfId": "",
 "sendPage": "",
 "sysProdName": "tcis",
 "transactionId": "",
 "type": "SYSMAN"}

{'traceId': 'd9dcd6ee-a0f3-170c-83e5-a52f2133cd31', 'error': {'code': 'APG400', 'message': 'The server could not process the request.'}}
response <Response [400]>
[Finished in 0.789s]
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
JWB
  • 65
  • 4

1 Answers1

1

When you remove the double quotes from the already JSON-formatted data you get ['TEST_TCIS:Oncall Schedule'], but this isn't valid JSON anymore. If this is supposed to be a list containing one string, the string needs to be quoted with double quotes (") in JSON.

To do this properly, remove the quotes from the value first, and then convert it to a JSON string.

In fact, "removing the quotes" from "['TEST_TCIS:Oncall Schedule']" means to parse a string representation of a list into a list, for which there is ast.literal_eval.

import ast
import json

data = {"abendCode": "",
        "action": "CREATE",
        "assignGroup": "TCIS-APP-SUPPORT",
        "component": "tcis",
        "description": "Test incident creation",
        "entered": "", "logicalName": "tcis",
        "pageGroups": "['TEST_TCIS:Oncall Schedule']",
        "priority": "",
        "racfId": "",
        "sendPage": "",
        "sysProdName": "tcis",
        "transactionId": "",
        "type": "SYSMAN"}

data['pageGroups'] = ast.literal_eval(data['pageGroups'])
data = json.dumps(data)
print(data)

#response = requests.posts(..., json=data, ...)

Output (line breaks added manually):

{"abendCode": "",
 "action": "CREATE",
 "assignGroup": "TCIS-APP-SUPPORT",
 "component": "tcis",
 "description": "Test incident creation",
 "entered": "",
 "logicalName": "tcis",
 "pageGroups": ["TEST_TCIS:Oncall Schedule"],
 "priority": "",
 "racfId": "",
 "sendPage": "",
 "sysProdName": "tcis",
 "transactionId": "",
 "type": "SYSMAN"}
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 1
    It worked, but I had to maintain the json.dumps in the code and use (..., data=data, ...) for it to pass a 200, otherwise I was getting a 400. – JWB Mar 09 '21 at 13:15