Skip to main content

I have a json array in the structure

[{"name":"A","content":"X"},{"name":"B","content":"Y"},{"name":"C","content":"Z"}]

how do I extract it as the name/content (key/value) pair so that end up with a feature containing the following attributes:

 

A: X

 

B: Y

 

C: Z

 

 

In the actual data there are about 40 attributes, so an automatic flatenning of some sort would be prefered.

Hi @jdh

Try using the JSONFragmenter with the following set up:

You can either read in the JSON file directly or set the source attribute to that containing the json string. Set the flatten query result to Yes and then you will have to type out the attributes you would like to expose. This will give you the following result:


Hi @jdh

Try using the JSONFragmenter with the following set up:

You can either read in the JSON file directly or set the source attribute to that containing the json string. Set the flatten query result to Yes and then you will have to type out the attributes you would like to expose. This will give you the following result:

I would like a single feature with (in this example) three attributes A,B,C.

 

Not three features each with attributes name, content.

 

 


I would like a single feature with (in this example) three attributes A,B,C.

 

Not three features each with attributes name, content.

 

 

Hi @jdh, 

 

 

Ah I see, I think a couple more steps are required here. 

 

 

1. Use a StringConcatenator after the JSONFragmenter to create a new attribute combining name and content 

 

@Value(name):@Value(content)
2. Add an Aggregator with the mode set to Attributes Only. Set the _result from step 1 as the Attribute to Concatenate and the Seperator Character set to the Special Character Newline. If you have multiple attributes you should be able to use the Group By parameter to build separate lists for each attribute here.

 

 

This should give you the desired result: 

 

0684Q00000ArN24QAF.png

 

 


PythonCaller (the json module)

import json
def processFeature(feature):
    for m in json.loads(feature.getAttribute('_json')):
        feature.setAttribute(ma'name'], mt'content'])

XMLXQueyrExtractor (XML Input: None (File is specified in query))

for $m in jn:members(fme:get-json-attribute('_json'))
return fme:set-attribute($m('name'), $m('content')) 

The XQuery processor "Zorba" bundled with FME supports the JSONiq extension. See these links to learn more about JSONiq.


Reply