Skip to main content
Solved

Feature writer reformat my json attribute

  • November 21, 2024
  • 2 replies
  • 66 views

hnahoulder
Contributor
Forum|alt.badge.img+2

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
 

[
{
"result_json" : "[{\"millesime\": [{\"2022\": {\"echelles\": {\"Y\": {\"max\": \"20.39\", \"min\": \"18.11\"}, \"X\": {\"max\": 461.0, \"min\": 0}}, \"resultats\": [{\"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\": [{\"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\"}]"
}
]

 

Best answer by david_r

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.

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

2 replies

david_r
Celebrity
  • Best Answer
  • November 21, 2024

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.


hnahoulder
Contributor
Forum|alt.badge.img+2
  • Author
  • Contributor
  • November 21, 2024

You are absolutely right, thank you