Json : filtering for special characters

Hi,

I’m building a json file from a list of resources.
However, sometimes, my customer insert special characters such as a double quote "
I tried to use output filter like this : [[+pagetitle:htmlent]] or [[+pagetitle:stripString=myspecialchar]] (the back quotes are not showing on the forum) and it’s working : the " are deleted but as result, my placeholder is always empty on the json result and I don’t understand why…

My json template looks like :

        {
          "type": "Feature",
          "geometry": { "type": "Point", "coordinates": [ [[+longitude]], [[+latitude]] ] },
          "properties": {
            "category": {
               "color": "#c85537",
               "icon": "sync_alt"
            },
            "type": "[[+tv.type]]",
            "name": "[[+pagetitle]]",
          }
        }

Create a snippet called something like tojson and use that as an output modifier on your variables. for example (very basic!)

$output = json_encode($input);
return $output;

and in your json template,

        {
          "type": "Feature",
          "geometry": { "type": "Point", "coordinates": [ [[+longitude]], [[+latitude]] ] },
          "properties": {
            "category": {
               "color": "#c85537",
               "icon": "sync_alt"
            },
            "type": [[!#[[+tv.type:tojson]],
            "name": [[+pagetitle:tojson]],
          }
        }

noting that there aren’t any quotes around the placeholder that your converting to json, since they’ll be added by the snippet.

You may wish to look at the json_encode options to make sure it encodes correctly for your input strings.

Not sure this has anything to do with your pagetitle output filter, but doesn’t this line

"type": "[[!#[[+tv.type]]",

need two additional closing brackets ]] ?

Sorry it was an uncleaned example code :wink:

I just tried your idea but as result, all my pagetitle are empty on my final json !

I wonder if the issue is not coming from an add on I’m using for the job : GeoTV
But unfortunately, if it’s this, I won’t solved my issue because the developer don’t maintain this very nice extension to make TVs as geolocation system… :frowning:

If [[+pagetitle]] returns something in your chunk/tpl, and [[+pagetitle:tojson]] doesn’t, then either the snippet doesn’t exist or it’s throwing some kind of error. You could check the MODX error logs to see if it’s complaining about anything in the snippet.

(good spot harry…)

So where exactly do you use this json template? As the “Point template” of your GeoTV? In that case I don’t think you have access to the placeholder [[+pagetitle]] of the resource.

Can you provide more information about the snippet calls and chunks you use to output your json file?

I think @halftrainedharry is right, there is no ‘pagetitle’ placeholder available in those templates. More detail about how that JSON chunk fits into your overall snippet call or template would help.

@wallacio : I have no error on MODX log when using the output filter tojson, such when I’m using the :htmlent output filter

@halftrainedharry : effectively, I’m using this json template at the “point template” of GeoTV addon.
But the add on works well with [[+pagetitle]] and all others regular TVs. I have access to them with a beautiful populated Json file

Here my call for build the json :

{
    "type": "FeatureCollection",
    "features": [
      [[getResources? 
         &parents=`21` 
         &tpl=`@INLINE [[+tv.Pro__Geolocalisation]]` 
         &includeTVs=`1` 
         &processTVs=`1` 
         &processTVList=`Pro__Geolocalisation` 
         &tvFilters=`Pro__Geolocalisation!=`
         &limit=`0` 
         &debug=`0` 
         &outputSeparator=`,` 
      ]]
   ]
}

Here my geopoints_point_tpl template :


        {
          "type": "Feature",
          "geometry": { "type": "Point", "coordinates": [ [[+longitude]], [[+latitude]] ] },
          "properties": {
            "category": {
               "categorie": "[[+tv.Pro__Categorie]]",
               "color": "#[[!getResourceField? &id=`[[+tv.Pro__Categorie]]` &field=`Cat_Color` &processTV=`1`]]",
               "icon": "[[!getResourceField? &id=`[[+tv.Pro__Categorie]]` &field=`Cat_Icon` &processTV=`1`]]"
            },
            "id": "[[+id]]",
            "name": "[[+pagetitle]]",
            "description": "[[+tv.Pro__Description]]",
            "adresse": "<i class='material-icons'>place</i> [[+tv.Pro__Adresse]]",
            "cp": "[[+tv.Pro__CP]]",
            "ville": "[[+tv.Pro__Ville]]<br />",
            "telfixe": "[[!If? subject=`[[+tv.Pro__Tel-fixe]]` &operator=`notempty` &then=`<i class='material-icons'>phone</i> [[+tv.Pro__Tel-fixe]]<br />`]]",
            "telmobile": "[[!If? subject=`[[+tv.Pro__Tel-mobile]]` &operator=`notempty` &then=`<i class='material-icons'>stay_current_portrait</i> [[+tv.Pro__Tel-mobile]]<br />`]]",
            "email": "[[!If? subject=`[[+tv.Pro__Mail]]` &operator=`notempty` &then=`<i class='material-icons'>mail_outline</i> <a href='mailto:[[+tv.Pro__Mail]]'>[[+tv.Pro__Mail]]</a><br />`]]",
            "web": "[[!If? subject=`[[+tv.Pro__Web]]` &operator=`notempty` &then=`<i class='material-icons'>language</i> <a href='[[+tv.Pro__Web]]'>[[+tv.Pro__Web]]</a>`]]"
          }
        }

This code working well, except when my customer’s using special characters in pagetitle or other TVs used on the json file

I’m sure now the issue is coming from GeoTV.
I have another json file on the same website, with same resources.
And on the pdoResources output template, if I’m using the output filter :

[[+pagetitle:stripString=`"`]]

… the double quote are effectively striped on the generated json :frowning:

I would try replacing this @INLINE code

&tpl=`@INLINE [[+tv.Pro__Geolocalisation]]` 

with a chunk &tpl=`myNewChunk` that basically has the content/structure of your current geopoints_point_tpl template. Then replace

"geometry": { "type": "Point", "coordinates": [ [[+longitude]], [[+latitude]] ] },

with

"geometry": { "type": "Point", "coordinates": [ [[+tv.Pro__Geolocalisation]] ] },

and change geopoints_point_tpl to

[[+longitude]], [[+latitude]]
1 Like

Oh oh oh Revolution !
I was misunderstood the way the GeoTV template works !
Thanks a lot @halftrainedharry ! The output filter naturally works well now :slight_smile: