Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using jq to add aditional key-value pairs to json

I have this json.

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
          [
            [
              [
                4.991669,
                52.324436,
                0
              ]
            ]
          ]
        ]
      },
      "properties": {
        "neighbourhood": "Bijlmer-Oost",
        "neighbourhood_group": null
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
          [
            [
              [
                5.079162,
                52.388654,
                0
              ]
            ]
          ]
        ]
      },
      "properties": {
        "neighbourhood": null,
        "neighbourhood_group": null
      }
    }
  ]
}

What I want is to add to the properties part a new key-value pair.
Someone could help me to figure it out what is the best way to do this?

I tried with this:
jq --arg dd "$d" '.features[] |= . + {properties: download_date: $dd}' /tmp/file-$d.geojson

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But this is updates the whole properites like this:
"properties": {
"download_date": "2023-03-09"
}

Expected result should look like this:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
          [
            [
              [
                4.991669,
                52.324436,
                0
              ]
            ]
          ]
        ]
      },
      "properties": {
        "neighbourhood": "Bijlmer-Oost",
        "neighbourhood_group": null,
        "date_added": "2022-01-01"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
          [
            [
              [
                5.079162,
                52.388654,
                0
              ]
            ]
          ]
        ]
      },
      "properties": {
        "neighbourhood": null,
        "neighbourhood_group": null,
        "date_added": "2022-01-01"
      }
    }
  ]
}

>Solution :

You can use:

$ jq --arg dd "$d" '.features[].properties += {date_added: $dd}' file
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading