【问题标题】:jq: group objects by stringjq:按字符串分组对象
【发布时间】:2018-11-27 18:26:40
【问题描述】:

我有一些来自 Eurostat 的 json,如下所示:

{
  "version": "2.0",
  "label": "Principaux agrégats des administrations publiques, y compris recettes et dépenses",
  "href": "http://ec.europa.eu/eurostat/wdds/rest/data/v2.1/json/fr/gov_10a_main?unit=PC_GDP&na_item=TE&sector=S13&time=2008&time=2009&time=2010&time=2011&time=2012&time=2013&time=2014&time=2015&time=2016&time=2017&geo=DE&geo=AT&geo=BE&geo=BG&geo=CY&geo=HR&geo=FI",
  "source": "Eurostat",
  "updated": "2018-10-26",
  "status": {
    "57": "b"
  },
  "extension": {
    "datasetId": "gov_10a_main",
    "lang": "FR",
    "description": null,
    "subTitle": null,
    "status": {
      "label": {
        "b": "rupture de série"
      }
    }
  },
  "class": "dataset",
  "value": {
    "0": 49.9,
    "1": 54.1,
    "2": 52.8,
    "3": 50.9,
    "4": 51.2,
    "5": 51.6,
    "6": 52.4,
    "7": 51.1,
    "8": 50.3,
    "9": 49.2,
    "10": 50.3,
    "11": 54.2,
    "12": 53.3,
    "13": 54.5,
    "14": 55.9,
    "15": 55.8,
    "16": 55.3,
    "17": 53.7,
    "18": 53,
    "19": 52.2,
    "20": 37.1,
    "21": 39.4,
    "22": 36.2,
    "23": 33.8,
    "24": 34.5,
    "25": 37.7,
    "26": 43.1,
    "27": 40.5,
    "28": 35.1,
    "29": 35.1,
    "30": 38.4,
    "31": 42.1,
    "32": 42,
    "33": 42.3,
    "34": 41.9,
    "35": 41.9,
    "36": 48.8,
    "37": 40.6,
    "38": 38,
    "39": 37.5,
    "40": 43.6,
    "41": 47.6,
    "42": 47.3,
    "43": 44.7,
    "44": 44.3,
    "45": 44.7,
    "46": 44,
    "47": 43.7,
    "48": 43.9,
    "49": 43.9,
    "50": 48.3,
    "51": 54.8,
    "52": 54.8,
    "53": 54.4,
    "54": 56.2,
    "55": 57.5,
    "56": 58.1,
    "57": 57.1,
    "58": 55.9,
    "59": 54,
    "60": 45.3,
    "61": 48.3,
    "62": 48,
    "63": 48.5,
    "64": 47.8,
    "65": 47.6,
    "66": 48.1,
    "67": 48.3,
    "68": 46.9,
    "69": 45
  },
  "dimension": {
    "unit": {
      "label": "unit",
      "category": {
        "index": {
          "PC_GDP": 0
        },
        "label": {
          "PC_GDP": "Pourcentage du produit intérieur brut (PIB)"
        }
      }
    },
    "sector": {
      "label": "sector",
      "category": {
        "index": {
          "S13": 0
        },
        "label": {
          "S13": "Administrations publiques"
        }
      }
    },
    "na_item": {
      "label": "na_item",
      "category": {
        "index": {
          "TE": 0
        },
        "label": {
          "TE": "Total des dépenses des administrations publiques"
        }
      }
    },
    "geo": {
      "label": "geo",
      "category": {
        "index": {
          "AT": 0,
          "BE": 1,
          "BG": 2,
          "CY": 3,
          "DE": 4,
          "FI": 5,
          "HR": 6
        },
        "label": {
          "AT": "Autriche",
          "BE": "Belgique",
          "BG": "Bulgarie",
          "CY": "Chypre",
          "DE": "Allemagne (jusqu'en 1990, ancien territoire de la RFA)",
          "FI": "Finlande",
          "HR": "Croatie"
        }
      }
    },
    "time": {
      "label": "time",
      "category": {
        "index": {
          "2008": 0,
          "2009": 1,
          "2010": 2,
          "2011": 3,
          "2012": 4,
          "2013": 5,
          "2014": 6,
          "2015": 7,
          "2016": 8,
          "2017": 9
        },
        "label": {
          "2008": "2008",
          "2009": "2009",
          "2010": "2010",
          "2011": "2011",
          "2012": "2012",
          "2013": "2013",
          "2014": "2014",
          "2015": "2015",
          "2016": "2016",
          "2017": "2017"
        }
      }
    }
  },
  "id": [
    "unit",
    "sector",
    "na_item",
    "geo",
    "time"
  ],
  "size": [
    1,
    1,
    1,
    7,
    10
  ]
}

我想生成一个 csv 文件。

首先,我需要通过字符串将 .status 与 .value 连接起来(对不起,我的 json 知识很差) --> "status":{"57": "b"} 与 "value":{"57": 57.1}.

其次,我需要生成与原始表相同的表(从 Eurostat 下载)。

我尝试了很多 jq 命令,例如:

.status,.value | to_entries

我还没有找到解决办法。

有什么帮助吗?我认为需要 map 或 map_values/group_by 命令,但我不太了解这些功能。

编辑:

我从eurostat下载数据。

我使用他们的网络服务here,在那里我可以下载 json 格式的数据。

我想用 jq 在 shell 中重现与原始表相同的表。在我的例子中,它应该看起来像:

GEO/TIME,2010,2011,2012,2013,2014,2015,2016,2017 比利时,"53,3","54,5","55,9","55,8","55,3","53,7","53,0","52,2" 宝格丽,"36,2","33,8","34,5","37,7","43,1","40,5","35,1","35,1" "Allemagne (jusqu'en 1990, ancien territoire de la RFA)","47,3","44,7","44,3","44,7","44,0","43,7 ","43,9","43,9" 克罗地亚语,"48,0","48,5","47,8","47,6","48,1","48,3","46,9","45,0" 西普里,"42,0","42,3","41,9","41,9","48,8","40,6","38,0","37,5" 芬兰,"54,8","54,4","56,2","57,5","58,1","57,1","55,9","54,0"

但是json包含元数据,芬兰德必须有57,1b的值。

我希望这次编辑更清楚。

非常感谢您的帮助。

【问题讨论】:

  • 对于第一部分,.value[.status|keys[]] 应该可以工作。目前尚不清楚您想要的 CSV 文件实际上是什么样的。
  • 谢谢,但它只产生 57.1

标签: json bash shell jq


【解决方案1】:

您的问题并没有非常准确地表明您想要什么输出,但希望您能够适应以下内容:

.value as $dict
| .status
| to_entries
| map( [.key, .value, $dict[.key]] )
| .[]
| @csv

根据您的输入,并使用 -r 选项调用 jq,这会产生:

"57","b",57.1

【讨论】:

  • 对不起,我会尽力解释得更好。我会编辑帖子。
猜你喜欢
  • 2018-09-08
  • 2021-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-20
  • 1970-01-01
  • 2022-06-29
相关资源
最近更新 更多