Skip to main content
Solved

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

  • Aug 24, 2023 1:56 PM UTC
  • 9 replies
  • 550 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

View original
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+71
  • Evangelist
  • Aug 24, 2023 3:07 PM UTC

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
  • Aug 24, 2023 5:15 PM UTC

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
  • Aug 25, 2023 8:42 AM UTC

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
  • Aug 25, 2023 9:24 AM UTC

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+71
  • Evangelist
  • Best Answer
  • Aug 25, 2023 9:33 AM UTC

Passing _response_body to a JSONFlattener does the trick for me.

image


fbrws
BANNED
Forum|alt.badge.img+2
  • Author
  • BANNED
  • Aug 25, 2023 9:48 AM UTC

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


ebygomm
Evangelist
Forum|alt.badge.img+54
  • Evangelist
  • Aug 25, 2023 9:49 AM UTC

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+71
  • Evangelist
  • Aug 25, 2023 9:50 AM UTC

You're welcome, have a nice day! 🌞


fbrws
BANNED
Forum|alt.badge.img+2
  • Author
  • BANNED
  • Aug 25, 2023 9:56 AM UTC

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.