Skip to main content
Question

Convert txt to structured Json format

  • May 14, 2020
  • 5 replies
  • 359 views

oumar
Contributor
Forum|alt.badge.img+2
  • Contributor
  • 2 replies

Hi,

I am in new in Json file formatting and I have a flat file that contained data to be used in HTTPCaller to an API that need structured Json formatting file. I am struggling with converting my flat file into structured Json format. I tried many options and can't get it to work.

 

I have attached my input and desired output files

Thanks for your assistance.

 

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.

5 replies

takashi
Celebrity
  • 7843 replies
  • May 14, 2020

Hi @oumar, you can use the JSONTemplater to convert the CSV record to a JSON text structure. This is a template expression example that convert a single row of the CSV to a JSON text.

{
    "logging" : {
        "sessionId" : fme:get-attribute("logging.sessionId"),
        "quoteNumber" : fme:get-attribute("logging.quoteNumber")
    },
    "quote" : {
        "numberOfUnits" : fme:get-attribute("quote.numberOfUnits"),
        "numberOfResidents" : fme:get-attribute("quote.numberOfResidents"),
        "numberOfDogs" : fme:get-attribute("quote.numberOfDogs"),
        "numberOfGarages" : fme:get-attribute("quote.numberOfGarages")
    },
    "filter" : {
        "formCode" : fme:get-attribute("filter.formCode"),
        "uwCompany" : fme:get-attribute("filter.uwCompany"),
        "uwTier" : fme:get-attribute("filter.uwTier"),
        "wildfireScore": fme:get-attribute("filter.wildfireScore")
    }
}

See also the Help to learn more.

https://docs.safe.com/fme/html/FME_Desktop_Documentation/FME_Transformers/Transformers/jsontemplater.htm

Hope this helps.


daveatsafe
Safer
Forum|alt.badge.img+19
  • Safer
  • 1637 replies
  • May 14, 2020

Hi @oumar,

You can use the JSONTemplater to create the output JSON that can be used in the JSON writer. I am attaching a workspace to illustrate.

csv2json.fmw


oumar
Contributor
Forum|alt.badge.img+2
  • Author
  • Contributor
  • 2 replies
  • May 18, 2020

Thanks Dave and Takashi for your expertise.

 


oumar
Contributor
Forum|alt.badge.img+2
  • Author
  • Contributor
  • 2 replies
  • May 18, 2020

Hi @oumar,

You can use the JSONTemplater to create the output JSON that can be used in the JSON writer. I am attaching a workspace to illustrate.

csv2json.fmw

Hi Dave,

on a follow up question, how do I feed the result into a HTTPCaller to call an API. I am able to do so just by having only 1 record in a file using Upload data = file and pointing that file with single record.CL_Underwriter_JsonExtracter_Grid_Input_json.fmwt


daveatsafe
Safer
Forum|alt.badge.img+19
  • Safer
  • 1637 replies
  • May 19, 2020

Hi Dave,

on a follow up question, how do I feed the result into a HTTPCaller to call an API. I am able to do so just by having only 1 record in a file using Upload data = file and pointing that file with single record.CL_Underwriter_JsonExtracter_Grid_Input_json.fmwt

Hi @oumar,

If you use Takashi's template in a single JSONTemplater, you will get one attribute containing the complete JSON string, that can be used as the upload string in the HTTPCaller.