Python vs Jython - MuleSoft
Question: I have a Python script that converts JSON to CSV successfully when run in
PyCharm. When I move that Python script into a Python Transformer in MuleSoft,
the script fails with the error:

> TypeError: unicode indices must be integers in at line number 10
> (javax.script.ScriptException). Message payload is of type: String
> (org.mule.api.transformer.TransformerMessagingException). Message payload is
> of type: String

What is the difference between Python and Jython in this context? I don't get
it!

Here is the Python:

    
    
    import csv
    import io
    
    data = message.getInvocationProperty("my_JSON")
    
    output = io.BytesIO()
    writer = csv.writer(output)
    
    for item in data:
        writer.writerow(([item['observationid'], item['fkey_observation'],  item['value'], item['participantid'], item['uom'], item['finishtime'], item['starttime'], item['observedproperty'], item['measuretime'], item['measurementid'], item['longitude'], item['identifier'], item['latitude']]))
    
    result = output.getvalue()
    

`"my_JSON"` is a variable containing the JSON.


Answer: You seem to have forgotten to parse the JSON, like so: `data =
json.loads(data)`.

Without that, `data` is a `str`, `item` is a `str` of length 1, and
`item['observationid']` raises `TypeError`.

