Skip to main content
Solved

extract json key value array


jdh
Contributor
Forum|alt.badge.img+28
  • Contributor

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.

Best answer by takashi

PythonCaller (the json module)

import json
def processFeature(feature):
    for m in json.loads(feature.getAttribute('_json')):
        feature.setAttribute(m['name'], m['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.

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

4 replies

Forum|alt.badge.img+2

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:


jdh
Contributor
Forum|alt.badge.img+28
  • Author
  • Contributor
  • August 29, 2018
hollyatsafe wrote:

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.

 

 


Forum|alt.badge.img+2
jdh wrote:
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

 

 


takashi
Influencer
  • Best Answer
  • August 29, 2018

PythonCaller (the json module)

import json
def processFeature(feature):
    for m in json.loads(feature.getAttribute('_json')):
        feature.setAttribute(m['name'], m['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


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