Skip to main content
Solved

How to insert a new key value pair into a JSON file, where the value is based upon an attribute

  • October 24, 2018
  • 5 replies
  • 213 views

friesewoudloper
Forum|alt.badge.img

Hi,

I have a dataset with an attribute containing a JSON file for every feature. The dataset also has an attribute called frequency. I'd like to add the key frequency to the JSON file, with the value in the frequency attribute. I tried using the JSONUpdater transformer:

Update Type: Insert into Object

 

JSON path: json

 

Object/Array index: frequency

 

Value Type: Plain Text

 

Value: [the value of the frequency attribute]

The resulting JSON file has a key frequency with a string of length 0 as a value.

I also tried:

Update Type: Insert into Object

 

JSON path: json

 

Object/Array index: frequency

 

Value Type: JSON/XQuery

 

Value: fme:get-attribute("frequency")

This time the result was a key frequency with a value of null.

 

How do I resolve this?

Best answer by jdh

The value type should be plain text, and the value should just point to the attribute.

The following works fine for me.

Just to verify, you are attaching the feature to both the Document and Update ports?

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

5 replies

jdh
Contributor
Forum|alt.badge.img+28
  • Contributor
  • Best Answer
  • October 24, 2018

The value type should be plain text, and the value should just point to the attribute.

The following works fine for me.

Just to verify, you are attaching the feature to both the Document and Update ports?


friesewoudloper
Forum|alt.badge.img
jdh wrote:

The value type should be plain text, and the value should just point to the attribute.

The following works fine for me.

Just to verify, you are attaching the feature to both the Document and Update ports?

I hadn't connected the feature to the Update port. Connecting it however doesn't solve my problem. The processing time increases very much. But that isn't the biggest problem. For most of the features, the frequency key in the JSON file doesn't have the right value.

 

 

I have 217 features in my dataset. I think the frequency key in the JSON file is now updated 217 times *for every feature*. After running the workspace, all JSON files seem to have the same value for frequency, namely that of the frequency attribute in the last feature that was processed.

 

 


friesewoudloper
Forum|alt.badge.img

@jdh got me thinking: maybe I should use the 'Group Update Features By' parameter? I'll give it a try.


jdh
Contributor
Forum|alt.badge.img+28
  • Contributor
  • October 24, 2018
friesewoudloper wrote:
I hadn't connected the feature to the Update port. Connecting it however doesn't solve my problem. The processing time increases very much. But that isn't the biggest problem. For most of the features, the frequency key in the JSON file doesn't have the right value.

 

 

I have 217 features in my dataset. I think the frequency key in the JSON file is now updated 217 times *for every feature*. After running the workspace, all JSON files seem to have the same value for frequency, namely that of the frequency attribute in the last feature that was processed.

 

 

If you have multiple features and you want each feature to be treated individually, you need to set the group by, on a unique attribute for each feature.

friesewoudloper
Forum|alt.badge.img
jdh wrote:
If you have multiple features and you want each feature to be treated individually, you need to set the group by, on a unique attribute for each feature.
You are right! It works fine now. Thank you!

 

 


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