Skip to main content
Solved

Json extractor nested arrays


jdh
Contributor
Forum|alt.badge.img+28
  • Contributor

I have a json object that contains nested arrays, a simplified example is:

{
   "Attr1" : [{
      "Attr2" : [{
         "Attr3" : "A"
		 }
	        ]
	      }, {
      "Attr2" : [{
	 "Attr3" : "B"
		 }, {
	 "Attr3" : "C"
		 }
		]
	      }, {
      "Attr2" : [{
         "Attr3" : "D"
		 }, {
	 "Attr3" : "E"
		 }, {
	 "Attr3" : "F"
		 }
		]
	      }
	     ]
}

I would like to extract just attr3, either as a (complex) list, or a valid json ex. [{"Attr3":"A"},{"Attr3":"B"},{"Attr3":"C"},{"Attr3":"D"},{"Attr3":"E"},{"Attr3":"F"}]

 

 

If I use the JSONExtractor with json["Attr1"][*]["Attr2"][*]

I get {"Attr3":"A"}{"Attr3":"B"}{"Attr3":"C"}{"Attr3":"D"}{"Attr3":"E"}{"Attr3":"F"}'

without commas between elments.

 

 

The JSONFlattener with recursion does work, but the actual json has a couple of hundred additional elements, and I would like to avoid creating them unnecessarily if possible.

Best answer by takashi

Hi @jdh, I would use the StringReplacer to replace }{ with },{ after extracting the elements by the JSONExtractor.

[Addition] An example: json-to-list.fmw or json-to-list-2.fmw

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

5 replies

deanatsafe
Safer
Forum|alt.badge.img+6
  • Safer
  • March 24, 2016

Hi @jdh

I used JSONFragmenter with json["Attr1"][*]["Attr2"][*]["Attr3"]

This yields 6 records with _result = 'A', _result = 'B', etc

Then to make a list I used a ListBuilder which generated `_list{}._result' = {A,B,C,D,E,F}

See attached: json2list.fmw

Dean


takashi
Evangelist
  • Best Answer
  • March 24, 2016

Hi @jdh, I would use the StringReplacer to replace }{ with },{ after extracting the elements by the JSONExtractor.

[Addition] An example: json-to-list.fmw or json-to-list-2.fmw


jdh
Contributor
Forum|alt.badge.img+28
  • Author
  • Contributor
  • March 24, 2016
deanatsafe wrote:

Hi @jdh

I used JSONFragmenter with json["Attr1"][*]["Attr2"][*]["Attr3"]

This yields 6 records with _result = 'A', _result = 'B', etc

Then to make a list I used a ListBuilder which generated `_list{}._result' = {A,B,C,D,E,F}

See attached: json2list.fmw

Dean

I thought of that, but I really want to avoid using blocking transformers for performance reasons.


jdh
Contributor
Forum|alt.badge.img+28
  • Author
  • Contributor
  • March 24, 2016
takashi wrote:

Hi @jdh, I would use the StringReplacer to replace }{ with },{ after extracting the elements by the JSONExtractor.

[Addition] An example: json-to-list.fmw or json-to-list-2.fmw

I've never tried the @ReplaceString in the text editor. I'll have to remember that in the future.


deanatsafe
Safer
Forum|alt.badge.img+6
  • Safer
  • March 24, 2016

No need for it to be blocking if you have a feature id to group by in the ListBuilder. If not you can make one with a Counter


Reply


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