Skip to main content

For my process, I use pythonCaller and I have a JSON-type attribute. However, since the setAttribute function doesn’t recognize the JSON type, I have to use json.dumps() to convert the JSON into a string. I then connect a featureWriter to the output of pythonCaller to write the result in JSON format. However, I don’t understand why the featureWriter reformats the JSON on input. How can I have a correct json format
Here is an example of the file from featureWriter
 

s
{
"result_json" : ""{\"millesime\": e{\"2022\": {\"echelles\": {\"Y\": {\"max\": \"20.39\", \"min\": \"18.11\"}, \"X\": {\"max\": 461.0, \"min\": 0}}, \"resultats\": s{\"distance\": 0, \"pixel_value\": \"20.39\"}, {\"distance\": 425.0, \"pixel_value\": \"19.76\"}, {\"distance\": 265.0, \"pixel_value\": \"19.4\"}, {\"distance\": 461.0, \"pixel_value\": \"18.11\"}]}}, {\"2023\": {\"echelles\": {\"Y\": {\"max\": \"2039\", \"min\": \"1811\"}, \"X\": {\"max\": 461.0, \"min\": 0}}, \"resultats\": s{\"distance\": 0, \"pixel_value\": \"2039\"}, {\"distance\": 425.0, \"pixel_value\": \"1976\"}, {\"distance\": 265.0, \"pixel_value\": \"1940\"}, {\"distance\": 461.0, \"pixel_value\": \"1811\"}]}}], \"project_name\": \"Fecamp\"}]"
}
]

 

JSON documents are represented as dicts internally in Python, which is why you have to use the .dumps() method to cast it (“dump”) it as a string.

Since your JSON document is now a string, you shouldn’t use the JSON writer for the output, but a regular text file writer.

If you use the JSON writer, FME will think that your JSON document is the content rather than the document, and escape all conflicting characters accordingly. Which I think explains what you’re observing.


You are absolutely right, thank you


Reply