Question

Complex list as a sub-template using JSONTemplater - CHRISTMAS PUZZLE?

  • 22 December 2016
  • 1 reply
  • 2 views

Badge

Hi,

I'll try to explain this as best as possible. I am using a JSONTemplater with XQuery to insert attribute values from CSV files. 

The structure of my JSON is:

 

{
    "snips": {
        "population": 115900,
        "liveBirths": 1305,
        "deaths": 1234,
        "age0-17": 23400,
        "bmePercent": 9.7,
        "households": 50900,
        "noCarOrVanPercent": 21.5,
        "noFormalQualificationsPercent": 25,
        "aged65PlusPercent": 20
    },
    "totalPopulation": {
        "year": "2015 MYE",
        "sourceLink": "http://www.ons.gov.uk",
        "columns": [
            "All People",
            "Total Male population",
            "Total Female population"
        ],
        "thisArea": {
            "name": "Gedling",
            "data": [115900, 56400, 59500]
        },
        "comparators": [
            {
                "name": "Nottinghamshire",
                "data": [805800, 396700, 409200]
            },
            {
                "name": "Nottingham and Nottinghamshire",
                "data": [1124700, 557800, 567000]
            },
            {
                "name": "England",
                "data": [5478630, 27029300, 27757000]
            }
        ]
    }
}
 My JSONTemplater root expression is:

{
    "snips":
        fme:process-features("SNIPS"),
    "totalPopulation":
        fme:process-features("TOTALPOP")
}

My SNIPS sub template expression is:

{
"population":fme:get-attribute("population")
,"liveBirths":fme:get-attribute("liveBirths")
,"deaths":fme:get-attribute("deaths")
,"age0-17":fme:get-attribute("age0-17")
,"bmePercent":fme:get-attribute("bmePercent")
,"households":fme:get-attribute("households")
,"noCarOrVanPercent":fme:get-attribute("noCarOrVanPercent")
,"noFormalQualificationsPercent":fme:get-attribute("noFormalQualificationsPercent")
,"aged65PlusPercent":fme:get-attribute("aged65PlusPercent")
}

My TOTALPOP sub template expression is:

{
"year":fme:get-attribute("year"),
"sourceLink": fme:get-attribute("sourceLink"),
"columns": [fme:get-list-attribute("columns{}.col0")],
"thisArea": fme:process-features("THISAREA")
}

My THISAREA sub template is:

{
"name":fme:get-attribute("name"),
"data": [fme:get-list-attribute("columns{}.data")]
}

So far with all the above I've managed to complete most of the JSONTemplate but now I have to build the comparators section which needs to go within the TOTALPOP sub template. This is where I am getting stuck on...  

The CSV data that holds the comparator data looks like this:

GedlingNottinghamshireNottingham and NottinghamshireEnglandAll People (2015)115900805800112470054786300Total Male Population (2015)5640039670055780027029300Total Female Population5950040920056700027757000

As you can see, I almost need two attribute values, one called name which comes as 3 records, Nottinghamshire, Nottingham and Nottinghamshire, England. The other attribute would be called data and would store the values as a list. On top of that, I need to exclude the first column (Gedling) as this is the area in focus and has already been defined in "thisArea" sub template. 

The solution needs to be dynamic so if I pull in a reader that has many more areas, such as ward level, it needs to create a JSON like the one above but then list all the wards and the data values in a list in the comparators section.

I guess this could be an advanced FME Users' Christmas puzzle??


1 reply

Userlevel 1
Badge +21

If I've understood correctly, an attributeexploder followed by an aggregator will get you a lot of the way there, using the method described here transpose-a-table-using-fme

Reply