Skip to main content
Question

Create Attribute whose value is dictionaty

  • March 7, 2019
  • 3 replies
  • 23 views

jugoslaviaa
Enthusiast
Forum|alt.badge.img+6

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": [{      "name": "Seattle",      "type": "CITY",      "template": "Prohibited",      "id": 1    }]  }}

Thanks in advance. 

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

3 replies

takashi
Celebrity
  • March 7, 2019

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?


jugoslaviaa
Enthusiast
Forum|alt.badge.img+6
  • Author
  • Enthusiast
  • March 7, 2019

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
    }
  }
}

takashi
Celebrity
  • March 7, 2019

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)
                    }
                |}
            }
        |}
    }
}