Question

Hi everyone, I have a formatted JSON response body as an attribute. Within the response body there exists an array. Each feature within the array contains 4 attributes. How is it possible to list all the individual features within the array?

  • 23 December 2021
  • 3 replies
  • 46 views

Below is an example JSON response body for a single feature. What I want to do is extract all of the "points" elements (in bold) as a list.

  1. Which transformer should I use?
  2. How would I write the syntax to extract the detail in bold through a JSON query?
  3. Can it be parsed as an attribute? If so, how?

 

{

  "id" : "feature1",

  "name" : "Feature 1",

  "length" : "5,000 km",

  "points" : [

   {

     "id" : "loc1",

     "name" : "Location 1",

     "country" : "CountryA",

     "is_tbd" : null

   },

   {

     "id" : "loc2",

     "name" : "Location 2",

     "country" : "CountryB",

     "is_tbd" : null

   }

  ],

  "notes1" : "test",

  "notes2" : null

}


3 replies

Badge +15

If you want to extract features from a JSON array your transformer is the JSONFragmenter. You just need to point at the array location with a wildcard. In your case json["points"][*] would do the job:

JSONFragmenterYou can use the Flatten Query Result into Attributes parameter to extract the attributes and then expose them with the Attributes to Expose parameter or using a separate AttributeExposer, which might be easier to use because you can import the attribute names from the cached features

 

Thanks fgiron,

I can successfully execute the JSON Fragmenter using the advice above. The result is now I have several more records in my table which have effectively exploded the feature by the number of "points" it contains.

What I want to do now, is combine all of the "points" for each feature into a single string attribute, separated by a comma. So rather than having an exploded list, I have the original number of features, just with an attribute that lists all of the "points" it contains. How might I be able to do this?

Badge +15

Hi @bogus​ 

If you don't want to explode your "points" into different features it might actually be the JSONExtractor what you're looking for. Or just add an Aggregator after the JSONFragmenter to concatenate the attributes from the exploded features. Another alternative would be a ListBuilder, which lets you combine the attributes from the exploded feature into an attribute list in one feature.

Reply