Skip to main content

Hello,

I have a Geojson file. I would like to create a new key-value pair in properties.

Current properties of an object looks like it.

{
  "properties": {
    "name": "Seattle",
    "type": "CITY",
    "template": "Prohibited",
    "id": 1,
    "TRANDESCRI": "MAJOR TRANSIT ROUTE",
    "SLOPE_PCT": 1,
    "PVMTCATEGO": "ART",
    "PARKBOULEV": "N",
    "SHAPE_Leng": 666.1146823345148,
    "_related_candidates": "0"
  }

And I would like to create new key in properties which is called 'description' and it should have the other key-value pairs in it. So it should look like this

{  "properties": {    "name": "Seattle",    "type": "CITY",    "template": "Prohibited",    "id": 1,    "TRANDESCRI": "MAJOR TRANSIT ROUTE",    "SLOPE_PCT": 1,    "PVMTCATEGO": "ART",    "PARKBOULEV": "N",    "SHAPE_Leng": 666.1146823345148,    "_related_candidates": "0",    "description": {      "name": "Seattle",      "type": "CITY",      "template": "Prohibited",      "id": 1    }  }}

or  (description is a list here)

{  "properties": {    "name": "Seattle",    "type": "CITY",    "template": "Prohibited",    "id": 1,    "TRANDESCRI": "MAJOR TRANSIT ROUTE",    "SLOPE_PCT": 1,    "PVMTCATEGO": "ART",    "PARKBOULEV": "N",    "SHAPE_Leng": 666.1146823345148,    "_related_candidates": "0",    "description": m{      "name": "Seattle",      "type": "CITY",      "template": "Prohibited",      "id": 1    }]  }}

Thanks in advance. 

Hi @jugoslaviaa, the text is similar to a JSON document. I think some JSON transformers could help you to extract required elements from this text.

What string should be finally created from this?


Hi @jugoslaviaa, the text is similar to a JSON document. I think some JSON transformers could help you to extract required elements from this text.

What string should be finally created from this?

My file in geojson format and properties looks like this 

{
  "properties": {
    "name": "Seattle",
    "type": "CITY",
    "template": "Prohibited",
    "id": 1,
    "TRANDESCRI": "MAJOR TRANSIT ROUTE",
    "SLOPE_PCT": 1,
    "PVMTCATEGO": "ART",
    "PARKBOULEV": "N",
    "SHAPE_Leng": 666.1146823345148,
    "_related_candidates": "0"
  }
}

And I would like to generate new key -description which is a list of other keys and values which are already in properties  

{
  "properties": {
    "name": "Seattle",
    "type": "CITY",
    "template": "Prohibited",
    "id": 1,
    "TRANDESCRI": "MAJOR TRANSIT ROUTE",
    "SLOPE_PCT": 1,
    "PVMTCATEGO": "ART",
    "PARKBOULEV": "N",
    "SHAPE_Leng": 666.1146823345148,
    "_related_candidates": "0",
    "description": {
      "name": "Seattle",
      "type": "CITY",
      "template": "Prohibited",
      "id": 1
    }
  }
}

Hi @jugoslaviaa, the text is similar to a JSON document. I think some JSON transformers could help you to extract required elements from this text.

What string should be finally created from this?

A basic way is to use the JSONFlattenr to extract all members as feature attributes and expose their names, then re-construct required JSON document with the JSONTemplater. The template expression looks like this.

{
    "properties": {
        "name": fme:get-attribute("properties.name"),
        "type": fme:get-attribute("properties.type"),
        "template": fme:get-attribute("properties.template"),
        "id": fme:get-attribute("properties.id"),
        "TRANDESCRI": fme:get-attribute("properties.TRANDESCRI"),
        "SLOPE_PCT": fme:get-attribute("properties.SLOPE_PCT"),
        "PVMTCATEGO": fme:get-attribute("properties.PVMTCATEGO"),
        "PARKBOULEV": fme:get-attribute("properties.PARKBOULEV"),
        "SHAPE_Leng": fme:get-attribute("properties.SHAPE_Leng"),
        "_related_candidates": fme:get-attribute("properties._related_candidates"),
        "description": {
            "name": fme:get-attribute("properties.name"),
            "type": fme:get-attribute("properties.type"),
            "template": fme:get-attribute("properties.template"),
            "id": fme:get-attribute("properties.id")
        }
    }
}

The JSONTemplater supports to execute XQuery expression with JSONiq extension. This advanced template expression does that with no flattening operation. Assuming that a feature attribute called "_json" stored the source JSON document.

{
    let $properties := fme:get-json-attribute("_json")("properties")
    return {
        "properties" : {|
            $properties,
            {
                "description" : {|
                    for $name in ("name", "type", "template", "id")
                    return {
                        $name : $properties($name)
                    }
                |}
            }
        |}
    }
}

Reply