Skip to main content
Solved

Best FME practice to convert JSON (as HTTP _response_body) input for a table (Postgres)

  • August 24, 2023
  • 9 replies
  • 392 views

fbrws
BANNED
Forum|alt.badge.img+2

I need to convert:

 

{

"keys": [

<ordered list of field names>

],

"values": [

"<ordered list of field values, matching the keys field order>

]

}

 

What is the best way to do this in FME?

 

Input example: see attachment

 

 

Best answer by geomancer

Passing _response_body to a JSONFlattener does the trick for me.

image

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.

9 replies

geomancer
Evangelist
Forum|alt.badge.img+60
  • Evangelist
  • August 24, 2023

The ListKeyValuePairExtractor (from FME Hub) looks like a good choice here. You will have to expose the attributes afterwards.

image


fbrws
BANNED
Forum|alt.badge.img+2
  • Author
  • BANNED
  • August 24, 2023

Hi Geomancer,

Thats a fast and complete answer! Thanks a lot. I will try it and report back.

Cheers


fbrws
BANNED
Forum|alt.badge.img+2
  • Author
  • BANNED
  • August 25, 2023

The above example works like a charm. Thanks again!

 

But its getting a little more complicated. Instead of using a JSON file I am getting a JSON response from a RES API which I invoke with the HTTPCaller. After that I use a JSONValidator, JSONFormatter and JSONFragmenter to connect to the ListExploder, the rest is the same.

(The first part of this is from Exercise: Handling JSON - FME Web Services (imgscloudapps.com))

http_jsonConfiguration of the JSONFragmenter:

http_json3Configuration of the ListExploder

http_json4But I get an error with ListExploder:

http_json2What I am doing wrong?


fbrws
BANNED
Forum|alt.badge.img+2
  • Author
  • BANNED
  • August 25, 2023

I get it working with the HTTPCaller in connection with TempPathnameCreator and the FeatureReader

http_json_featurereaderBut it feels cleaner (best practice, less steps) to use the response_body, although I haven't got it working yet.

What do you think?


geomancer
Evangelist
Forum|alt.badge.img+60
  • Evangelist
  • Best Answer
  • August 25, 2023

Passing _response_body to a JSONFlattener does the trick for me.

image


fbrws
BANNED
Forum|alt.badge.img+2
  • Author
  • BANNED
  • August 25, 2023

Thanks again geomancer! It works perfectly. You are a genius :)


ebygomm
Influencer
Forum|alt.badge.img+46
  • Influencer
  • August 25, 2023

The above example works like a charm. Thanks again!

 

But its getting a little more complicated. Instead of using a JSON file I am getting a JSON response from a RES API which I invoke with the HTTPCaller. After that I use a JSONValidator, JSONFormatter and JSONFragmenter to connect to the ListExploder, the rest is the same.

(The first part of this is from Exercise: Handling JSON - FME Web Services (imgscloudapps.com))

http_jsonConfiguration of the JSONFragmenter:

http_json3Configuration of the ListExploder

http_json4But I get an error with ListExploder:

http_json2What I am doing wrong?

You are getting the error because the list you've exposed doesn't actually exist in the data if you have processed it using the JSONFragmenter setup pictured above.

Try a JSONFlattener instead, this should give you the lists you need.

Alternatively, you could save your http response to a file instead of an attribute, and then read that back in as JSON with a FeatureReader


geomancer
Evangelist
Forum|alt.badge.img+60
  • Evangelist
  • August 25, 2023

You're welcome, have a nice day! 🌞


fbrws
BANNED
Forum|alt.badge.img+2
  • Author
  • BANNED
  • August 25, 2023

You are getting the error because the list you've exposed doesn't actually exist in the data if you have processed it using the JSONFragmenter setup pictured above.

Try a JSONFlattener instead, this should give you the lists you need.

Alternatively, you could save your http response to a file instead of an attribute, and then read that back in as JSON with a FeatureReader

Thx eby! Really helpfull remarks. Your input is coming together below.