Question

JSON Fragementation

  • 3 October 2019
  • 3 replies
  • 5 views

Badge

Hi! I am new to the JSON format and even newer on how to tackle it in FME!

I beleive my question is quite basic but i havnt figured it out with the tutorials etc I have found.

 

So, on to the problem!

I would like to fragment a json into its separate parts (see code and the desired output down below)

Here are two tests Ive made but havnt had much success.

 

 

My hope is to get something like this in excel as a proof of concept from the code provided down below.

Any help is appreciated!

/David

 

Source jsoncode:

{

 

"id": 182,

 

"structureId": "cb52a3db-1260-454a-b3d9-08693f27b2db",

 

"title": "FME TEST",

 

"description": "",

 

"coClassVersionId": 7,

 

"structureItems": [{

 

"itemId": 3187,

 

"classDetails": {

 

"id": 295727,

 

"categoryId": 152,

 

"title": "Bro",

 

"identifier": "CAA",

 

"categoryIdentifier": "BV",

 

"link": "/categories/152/categoryClasses/295727/details",

 

"isType": false

 

},

 

"items": [{

 

"itemId": 3188,

 

"classDetails": {

 

"id": 64040,

 

"categoryId": 138,

 

"title": "Broöverbyggnad",

 

"identifier": "C50",

 

"categoryIdentifier": "FS",

 

"link": "/types/64040",

 

"isType": true

 

},

 

"items": [{

 

"itemId": 3226,

 

"classDetails": {

 

"id": 296889,

 

"categoryId": 155,

 

"title": "Brounderbyggnad",

 

"identifier": "BL",

 

"categoryIdentifier": "KS",

 

"link": "/categories/155/categoryClasses/296889/details",

 

"isType": false

 

},

 

"items": [],

 

"activityClasses": [],

 

"properties": []

 

}],

 

"activityClasses": [],

 

"properties": []

 

}],

 

"activityClasses": [],

 

"properties": []

 

}, {

 

"itemId": 4003,

 

"classDetails": {

 

"id": 64524,

 

"categoryId": 138,

 

"title": "Vägbana",

 

"identifier": "A31",

 

"categoryIdentifier": "FS",

 

"link": "/types/64524",

 

"isType": true

 

},

 

"items": [{

 

"itemId": 4004,

 

"classDetails": {

 

"id": 296894,

 

"categoryId": 155,

 

"title": "Överbyggnad för väg och plan",

 

"identifier": "CB",

 

"categoryIdentifier": "KS",

 

"link": "/categories/155/categoryClasses/296894/details",

 

"isType": false

 

},

 

"items": [],

 

"activityClasses": [],

 

"properties": []

 

}],

 

"activityClasses": [],

 

"properties": []

 

}]

 

}

 

Httpcaller _responsebody for the same code

{"id":182,"structureId":"cb52a3db-1260-454a-b3d9-08693f27b2db","title":"FME TEST","description":"","coClassVersionId":7,"structureItems":[{"itemId":3187,"classDetails":{"id":295727,"categoryId":152,"title":"Bro","identifier":"CAA","categoryIdentifier":"BV","link":"/categories/152/categoryClasses/295727/details","isType":false},"items":[{"itemId":3188,"classDetails":{"id":64040,"categoryId":138,"title":"Broöverbyggnad","identifier":"C50","categoryIdentifier":"FS","link":"/types/64040","isType":true},"items":[{"itemId":3226,"classDetails":{"id":296889,"categoryId":155,"title":"Brounderbyggnad","identifier":"BL","categoryIdentifier":"KS","link":"/categories/155/categoryClasses/296889/details","isType":false},"items":[],"activityClasses":[],"properties":[]}],"activityClasses":[],"properties":[]}],"activityClasses":[],"properties":[]},{"itemId":4003,"classDetails":{"id":64524,"categoryId":138,"title":"Vägbana","identifier":"A31","categoryIdentifier":"FS","link":"/types/64524","isType":true},"items":[{"itemId":4004,"classDetails":{"id":296894,"categoryId":155,"title":"Överbyggnad för väg och plan","identifier":"CB","categoryIdentifier":"KS","link":"/categories/155/categoryClasses/296894/details","isType":false},"items":[],"activityClasses":[],"properties":[]}],"activityClasses":[],"properties":[]}]}

 


3 replies

Badge +2

Hi @davidwesstrom,

Your close, but there are actually a few more layers to your JSON. The information you would like to extract is all under classDetails, so this needs to be included in the query and then under Attributes to Expose you can specify title, identifier and categoryIdentifier.

On top of that whilst some of the information is under the structureItems array some of it is another layer deep in items, and there is even an items layer with items, so we will need to use a couple of JSONFragmenter transformers with different queries to access all of the information. Please find attached a workspace demonstrating how to set this up.

jsonfragmenter.fmw

When I am putting together JSON queries I find it helpful to start by simply flattening the top layer e.g. json["structureItems"][*] and Flatten Query Result Attributes = Yes, then if you view all of the Feature Information this will list every attribute and exactly where it sits in the JSON which can assist in creating the correct query to access it.

Once you have your attributes, you'll likely need to rename them so they can be shown in different columns and perhaps explore the Excel format attributes e.g. (xlsx_row_id) so they are in the correct positions in your Excel sheet.

Badge +22

That's a tough one. You have a recursive dataset, and I don't know if you can set it up with a single JSONFragmenter, and that's ignoring the fact that you want two features output per node.

 

 

This is possibly not the best approach, but should give you a starting point. recursivejson.fmw
Badge

Thank you both for the input!

By combining your answers it looks like I can move forward and make more complex querys.

Cheers,

David

Reply