Skip to main content
Solved

Convert JSON string to table


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

Hello,

I have the JSON string format like this “[[{"name":"Column1","value":"Value1"},{"name":"Column2","value":"Value2"},{"name":"Column3","value":"Value3"}],
 [{"name":"Column1","value":"ValueA"},{"name":"Column2","value":"ValueB"},{"name":"Column3","value":"ValueC"}]]”

And I would like to convert it to table like:

------------------------------------------------

Column1 | Column2 | Column3

Value1    | Value2     | Value3

ValueA    | ValueB    | ValueC

 

Is there any transformer can help me to do so?

Best answer by geomancer

The JSONFragmenter creates a list (named ‘array’). You can see the list in the Feature Information window. There you can also see the array is not exposed.

It is neccessary to expose the array to proceed. Therefore you need to expose array{}.name and array{}.value in the JSONFragmenter.

After that, the easiest solution is to use the ListKeyValuePairExtractor from FME Hub to add attributes with name array{}.name and value array{}.value . The final step is to expose those attributes (Column1, Column2, Column3).

See the attached workspace.

View original
Did this help you find an answer to your question?

8 replies

andrewj74
Supporter
Forum|alt.badge.img+5
  • Supporter
  • May 30, 2024

try the json fragmenter followed by an attribute exposer. 


leroy
Contributor
Forum|alt.badge.img+1
  • Author
  • Contributor
  • May 30, 2024
andrewj74 wrote:

try the json fragmenter followed by an attribute exposer. 

With  json fragmenter followed by an attribute exposer. I can restructure it like:

 

name     |    value

---------------------------

Column1  | Value1

Column2  | Value2

Column3  | Value3

Column1  | ValueA

Column2  | ValueB

Column3  | ValueC

 

But I would like to restructure it like: 

Column1 | Column2 | Column3

----------------------------------------------

Value1    | Value2     | Value3

ValueA    | ValueB    | ValueC

 

I believe FME is capable to restructure it but I am too new on FME so I need some assistance. 

Appreciate to any advice. 

Thank you so much.


geomancer
Evangelist
Forum|alt.badge.img+47
  • Evangelist
  • Best Answer
  • May 31, 2024

The JSONFragmenter creates a list (named ‘array’). You can see the list in the Feature Information window. There you can also see the array is not exposed.

It is neccessary to expose the array to proceed. Therefore you need to expose array{}.name and array{}.value in the JSONFragmenter.

After that, the easiest solution is to use the ListKeyValuePairExtractor from FME Hub to add attributes with name array{}.name and value array{}.value . The final step is to expose those attributes (Column1, Column2, Column3).

See the attached workspace.


leroy
Contributor
Forum|alt.badge.img+1
  • Author
  • Contributor
  • June 2, 2024

Thank you very much for your assistance. That really solved my blocker.

I have one more question about the ListKeyValuePairExtractor. When I try to use it, a message pops up requesting the installation of the component. The FME script is intended to run on an FME Server with a scheduler. Are there any precautions I should take when using a component like this?


geomancer
Evangelist
Forum|alt.badge.img+47
  • Evangelist
  • June 3, 2024

You can read up on custom transformers and security here: 

You can find more information on FME Server (FME Flow) and custom transformers here: https://support.safe.com/hc/en-us/articles/25407468358029-Custom-Transformers-and-FME-Flow


leroy
Contributor
Forum|alt.badge.img+1
  • Author
  • Contributor
  • June 4, 2024
geomancer wrote:

You can read up on custom transformers and security here: 

You can find more information on FME Server (FME Flow) and custom transformers here: https://support.safe.com/hc/en-us/articles/25407468358029-Custom-Transformers-and-FME-Flow

Thank you for the information. Is there any possible way to use a Safe Software provided transformer to replace ListKeyValuePairExtractor? I find ListKeyValuePairExtractor very useful, but I'm concerned about any policies that might prohibit using custom transformers not provided by Safe Software.


geomancer
Evangelist
Forum|alt.badge.img+47
  • Evangelist
  • June 4, 2024

Certainly 😀

There is a neat FME trick involved here: naming an attribute after the value of another attribute.

Also, to expose the column names (Column1, Column2, Column3) you will have to know their names.

 


geomancer
Evangelist
Forum|alt.badge.img+47
  • Evangelist
  • June 4, 2024

When you open ListKeyValuePairExtractor in FME Form you can see that it is a combination of a few FME Transformers, including a PythonCaller which does most of the work.

You would have to study the Python code to ascertain the ListKeyValuePairExtractor is safe to use.

 


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