Skip to main content
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?

  • December 23, 2021
  • 3 replies
  • 233 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

fgiron
Supporter
Forum|alt.badge.img+20
  • Supporter
  • December 23, 2021

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

 


  • Author
  • December 23, 2021

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?


fgiron
Supporter
Forum|alt.badge.img+20
  • Supporter
  • December 24, 2021

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.


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