Question

I need to extract values from the following JSON objects, ratings and score. I have tried using the JSON Extractor but do not know what the JSOn query should be.

  • 18 November 2019
  • 3 replies
  • 63 views

I need to extract the last two values for "rating" and "score" objects from the following JSON string. So I have an attribute 'rating' with value of 'Unclassified' and attribute 'score' with the value of '8.91'.

[{"assessmentReference":"BREEAM-0052-9883","assetScores":{"assetScore":[{"categoryScores":{"categoryScore":[{"categoryCode":"Man","categoryName":"Management","creditsAvailable":"21.00","creditsAwarded":"5.00","score":"2.85","weighting":"12.00"},{"categoryCode":"Hea","categoryName":"Health and Wellbeing","creditsAvailable":"11.00","creditsAwarded":"0.00","score":"0.00","weighting":"15.00"},{"categoryCode":"Inn","categoryName":"Innovation","creditsAvailable":"10.00","creditsAwarded":"1.00","score":"1.00","weighting":"10.00"}]},"rating":"Unclassified","score":"8.91"}]

I have tried using the JSON Extractor but do not know what the JSON query should be.


3 replies

Userlevel 4
Badge +30

Hi @sbarn

Could you share us your fmwt ( Workespace Template ) or your source JSON ?

 

Thanks,

Danilo

Badge +2

Hi @sbarn,

When I am working with JSON I find it useful to first use a JSONFlattener transformer, as this will allow me to view all the attributes in the Feature Information window of Visual Preview/Data Inspector and it will show you the structure to access each of the attributes which should be used to form your JSON Query.

 

 

So once you know how the attributes are stored there are a couple of options as to how you can extract them from the JSON string:

Option 1:

Expose the attributes directly in the JSONFlattener, using the 'Attributes to Expose' parameter. You'd need to use the full attribute name listed in the Feature Information window e.g.

array{0}.assetScores.assetScore{0}.rating

array{0}.assetScores.assetScore{0}.score

and then use a BulkAttributeRenamer to remove the array{0}.assetScores.assetScore{0} prefix.

 

Option 2:

Expose the attributes using a JSONFragmenter, this allows you to first perform a JSON Query on the string so that you can access the assetScore element and expose the attributes using the names rating and score meaning you don't have to rename them afterwards.

 

Option 3:

Use the JSONExtractor to perform a JSON Query for each attribute you wish to expose.

 

The workspace attached demonstrates all three options jsonextractattributes.fmw. Which option you choose usually depends on the complexity of your JSON and what attributes you want to extract from the JSON. Please note, if this is not the full JSON string the query will likely be more complex, but hopefully this demonstrates how you can find the right query and should get you started.

Take a look at this article for more info on working with JSON.

Thank-you @hollyatsafe I picked Option 1 and it worked a treat.

Reply