Skip to main content
Question

Convert txt to structured Json format


oumar
Contributor
Forum|alt.badge.img+1
  • Contributor

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.

 

5 replies

takashi
Influencer
  • 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
  • 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+1
  • Author
  • Contributor
  • May 18, 2020

Thanks Dave and Takashi for your expertise.

 


oumar
Contributor
Forum|alt.badge.img+1
  • Author
  • Contributor
  • May 18, 2020
daveatsafe wrote:

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
  • May 19, 2020
oumar wrote:

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.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings