为美国各州合并两个JSON文件

为美国各州合并两个JSON文件,json,database,datatables,jq,Json,Database,Datatables,Jq,我有一个json文件,由美国郡过滤,在“财产”部分有收入中值。所以这个json文件包含按县划分的收入中值 { "type": "Topology", "transform": { "scale": [ 0.035896170617061705, 0.005347309530953095 ], "translate": [ -179.14734, 17.884813 ] }, "objects": {

我有一个json文件,由美国郡过滤,在“财产”部分有收入中值。所以这个json文件包含按县划分的收入中值

{
  "type": "Topology",
  "transform": {
    "scale": [
      0.035896170617061705,
      0.005347309530953095
    ],
    "translate": [
      -179.14734,
      17.884813
    ]
  },
  "objects": {
    "us_counties_20m": {
      "type": "GeometryCollection",
      "geometries": [
        {
          "type": "Polygon",
          "arcs": [
            [
              0,
              1,
              2,
              3,
              4
            ]
          ],
          "id": "0500000US01001",
          "properties": {
            "PRICE": 48863
          }
        },
        {
          "type": "Polygon",
          "arcs": [
            [
              5,
              6,
              7,
              8,
              9,
              10
            ]
          ],
          "id": "0500000US01009",
          "properties": {
            "PRICE": 41940
          }
        },
        {
          "type": "Polygon",
          "arcs": [
            [
              11,
              12,
              13,
              14,
              15
            ]
          ],
          "id": "0500000US01017",
          "properties": {
            "PRICE": 33500
          }
        },
        {
          "type": "Polygon",
          "arcs": [
            [
              16,
              17,
              -3,
              18,
              19,
              20,
              21
            ]
          ],
          "id": "0500000US01021",
          "properties": {
            "PRICE": 38833
          }
        },
我想在“房地产”部分增加另一个价格,即每个县的房价中值。因此,我有第二个json文件,其中包含如下数据:

[
 {
   "Full County Number": 56045,
   "Price-RangeQ42019": "$150,000-$350,000",
   "Geography": "Weston County,  Wyoming",
   "Latitude (generated)": 43.8403,
   "Longitude (generated)": -104.5684,
   "Q42019 Price": "$178,218"
 },
 {
   "Full County Number": 56043,
   "Price-RangeQ42019": "$150,000-$350,000",
   "Geography": "Washakie County,  Wyoming",
   "Latitude (generated)": 43.8356,
   "Longitude (generated)": -107.6602,
   "Q42019 Price": "$170,665"
 },
,我希望第二个json的所有类别都作为单独的类别附加到“属性”部分

所需输出(“属性”部分包含更多信息):

第一个和第二个json文件中的“id”和“完整县编号”完全匹配。然而,“完整县编号”在每个县之前缺少“0500000US”前缀。如何合并这两个json文件以获得第三个json和附加属性


提前非常感谢。

以下内容应该可以提供解决方案。首先,构造一个字典($dict),然后使用该字典更新第一个文件

调用:

jq -n -f program.jq secondfile.json firstfile.json
其中program.jq包含:

def lpad:
  tostring | if length < 5 then ("00000" + .) | .[-5:] else . end;


(input
 | map( with_entries(if .key == "Full County Number" 
                     then .key = "id" | .value |= "0500000US" + lpad 
                     else .
                     end ) )
   | INDEX(.[]; .id) ) as $dict
| inputs
| .objects.us_counties_20m.geometries |= 
      map( .id as $id 
           | (.properties += $dict[$id]) )
def lpad: tostring |如果长度小于5,则为(“00000”+)|。[-5:]否则。终止 (输入 |地图(带有_条目(if.key==“完整县编号”) 然后.key=“id”|.value |=“0500000US”+lpad 其他的 完) |索引(.[];.id))为$dict |投入 |.objects.us_countries_20m.geometrics |= 映射(.id为$id) |(.properties+=$dict[$id]))
提供的两个JSON都不完整。提供完整的JSON和适当的预期输出。我更新了预期输出。JSON文件实际上只是在它们的模式中继续(只是第一个json文件的开头略有不同。@Inian这里有一个类似的示例,程序太长,无法在命令行上方便地输入。只需使用任何文本编辑器将程序复制到一个文件中即可。非常感谢!我解决了我的问题。再次非常感谢!我需要去掉引号和美元在“2019年第四季度价格”条目周围签名。我可以怎么做?我需要170665而不是“$170665”,这看起来是一个完全独立且重点明确的问题,因此我建议相应地提问。但是,请注意,
170665
不是JSON,因此如果您想要JSON输出,您必须在JSON编号和JSON字符串之间进行选择。
def lpad:
  tostring | if length < 5 then ("00000" + .) | .[-5:] else . end;


(input
 | map( with_entries(if .key == "Full County Number" 
                     then .key = "id" | .value |= "0500000US" + lpad 
                     else .
                     end ) )
   | INDEX(.[]; .id) ) as $dict
| inputs
| .objects.us_counties_20m.geometries |= 
      map( .id as $id 
           | (.properties += $dict[$id]) )