Error with json, "No viable alterantive at input"

I am trying to run an API all against Plex using an API from their Developer Portal. PLEX Developer Portal
The json for the Request Body is giving me an error in the Script console "no viable alternative at input" where I define the body of the request.
This is the script

plexusUserNo = 12345
url = "https://connect.plex.com/platform/custom-fields/v1/field-values/save"
header = {"content-type":"application/json", "X-Plex-Connect-Api-Key": "APIKey"}
payload =  [0:{"standardObjectName":"Employee", "recordIdentifiers":{"Plexus_User_No": plexusUserNo}, "customFields":{"RFID":5501}}]
client = system.net.httpClient()
response = client.post(url=url, headers=header, data= payload)
ds = system.util.jsonDecode(response.text)
print response
print ds

I get the error on the payload at the '0:'

SyntaxError: no viable alternative at input ':' (< input>, line 4)

The request body sample from the Dev Portal is

[
  0:{
    standardObjectName:"GLJournalDist",
    recordIdentifiers:{
      Journal_Link:7,
      Line_Item_No:30113464
    },
    customFields:{
      MyBooleanField:true,
      MyCustomObjectRef:{},
      MyDateField:"2023-10-29",
      MyDateTimeField:"2023-10-24T15:50:00Z",
      MyDecimalField:50.75,
      MyIntegerField:20,
      MyPartStandardObjectRef:{},
      MyTextField:"Twenty Two"
    }
  }
]

I have successfully used other API calls from the Developer Portal as well as Plex Web Services. but none of those start with the square brackets [..]. I'm not sure what I am missing here and how to format this and get it to work.

Does anybody have any ideas?

There is no syntax in Python to define a collection using square bracket, key, colon, value, square bracket.

The 0: in the link you posted is just an attempt to make json array indices clear. You don't actually have to specify them.

That is, it's just saying "the first element in the array you supply should be this", which is as simple to accomplish in Python as:
payload = [{"standardObjectName":"Employee", "recordIdentifiers":{"Plexus_User_No": plexusUserNo}, "customFields":{"RFID":5501}}]

2 Likes

When you prepare your payload, you are creating jython objects (lists and dicts), not actual JSON. The httpClient instance does the jython => json conversion for you.

Jython doesn't allow you to specify list subscripts within lists, as it doesn't support sparse lists. Just leave out the 0: in your payload.

Edit: sniped by Paul. Sigh.

1 Like

Thanks. That worked. I was positive I had already tried that, but sometimes you try so many different things, you forget what you have and have not tried.

Thanks for the help.

1 Like