Skip to main content
Best Answer

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
  • 295 views

friesewoudloper
Participant
Forum|alt.badge.img+1

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?

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

5 replies

jdh
Contributor
Forum|alt.badge.img+40
  • 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
Participant
Forum|alt.badge.img+1

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
Participant
Forum|alt.badge.img+1

@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+40
  • Contributor
  • October 24, 2018
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
Participant
Forum|alt.badge.img+1
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!