Skip to content
Advertisement

Is there any way to merge 2 json files programmatically?

There is the US json for the current situation in US, yet the json only shows the States while I’d also need the coordinates to place them in a map.

us data

On here we have the long and lat for each State, not sure how could I merge the 2 to combine them into a single file adding the coordinates for each State.

US data json is:

[
  {
    "date": 20200421,
    "state": "AK",
    ...
  },
  {
    "date": 20200421,
    "state": "AL",
    "positive": 5231,
    "negative": 43295,...

For each state I am looking at adding:

[
  {
    "date": 20200421,
    "state": "AK",
    "lat": "63.588753",
    "long": "-154.493062",

Then be able to download it. Or is there any other way I could programmatically get the coords instead of adding them each time? Not sure how to approach it.

UPDATE

Added php tag too as this could be done via backend.

Advertisement

Answer

If you compose a dictionary of information about latitude and longitude in format:

{'AK': {lat:"63.588753", long: "-154.493062"},
 'AL': {lat:"32.318231", long: "-86.902298"}}

To get the info into dictionary you can use scraper script on site you’ve provided (run in browser console):

JSON.stringify(Array.from(document.querySelector('.devsite-table-wrapper').children[0].children[0].children).reduce((acc,item) => ({...acc, [item.children[0].textContent]: {lat: item.children[1].textContent, long : item.children[2].textContent} })))

It iterates through all table rows and build a dictionary

Results are:

{
   "AK":{
      "lat":"63.588753",
      "long":"-154.493062"
   },
   "AL":{
      "lat":"32.318231",
      "long":"-86.902298"
   },
   "AR":{
      "lat":"35.20105",
      "long":"-91.831833"
   },
   "AZ":{
      "lat":"34.048928",
      "long":"-111.093731"
   },
   "CA":{
      "lat":"36.778261",
      "long":"-119.417932"
   },
   "CO":{
      "lat":"39.550051",
      "long":"-105.782067"
   },
   "CT":{
      "lat":"41.603221",
      "long":"-73.087749"
   },
   "DC":{
      "lat":"38.905985",
      "long":"-77.033418"
   },
   "DE":{
      "lat":"38.910832",
      "long":"-75.52767"
   },
   "FL":{
      "lat":"27.664827",
      "long":"-81.515754"
   },
   "GA":{
      "lat":"32.157435",
      "long":"-82.907123"
   },
   "HI":{
      "lat":"19.898682",
      "long":"-155.665857"
   },
   "IA":{
      "lat":"41.878003",
      "long":"-93.097702"
   },
   "ID":{
      "lat":"44.068202",
      "long":"-114.742041"
   },
   "IL":{
      "lat":"40.633125",
      "long":"-89.398528"
   },
   "IN":{
      "lat":"40.551217",
      "long":"-85.602364"
   },
   "KS":{
      "lat":"39.011902",
      "long":"-98.484246"
   },
   "KY":{
      "lat":"37.839333",
      "long":"-84.270018"
   },
   "LA":{
      "lat":"31.244823",
      "long":"-92.145024"
   },
   "MA":{
      "lat":"42.407211",
      "long":"-71.382437"
   },
   "MD":{
      "lat":"39.045755",
      "long":"-76.641271"
   },
   "ME":{
      "lat":"45.253783",
      "long":"-69.445469"
   },
   "MI":{
      "lat":"44.314844",
      "long":"-85.602364"
   },
   "MN":{
      "lat":"46.729553",
      "long":"-94.6859"
   },
   "MO":{
      "lat":"37.964253",
      "long":"-91.831833"
   },
   "MS":{
      "lat":"32.354668",
      "long":"-89.398528"
   },
   "MT":{
      "lat":"46.879682",
      "long":"-110.362566"
   },
   "NC":{
      "lat":"35.759573",
      "long":"-79.0193"
   },
   "ND":{
      "lat":"47.551493",
      "long":"-101.002012"
   },
   "NE":{
      "lat":"41.492537",
      "long":"-99.901813"
   },
   "NH":{
      "lat":"43.193852",
      "long":"-71.572395"
   },
   "NJ":{
      "lat":"40.058324",
      "long":"-74.405661"
   },
   "NM":{
      "lat":"34.97273",
      "long":"-105.032363"
   },
   "NV":{
      "lat":"38.80261",
      "long":"-116.419389"
   },
   "NY":{
      "lat":"43.299428",
      "long":"-74.217933"
   },
   "OH":{
      "lat":"40.417287",
      "long":"-82.907123"
   },
   "OK":{
      "lat":"35.007752",
      "long":"-97.092877"
   },
   "OR":{
      "lat":"43.804133",
      "long":"-120.554201"
   },
   "PA":{
      "lat":"41.203322",
      "long":"-77.194525"
   },
   "PR":{
      "lat":"18.220833",
      "long":"-66.590149"
   },
   "RI":{
      "lat":"41.580095",
      "long":"-71.477429"
   },
   "SC":{
      "lat":"33.836081",
      "long":"-81.163725"
   },
   "SD":{
      "lat":"43.969515",
      "long":"-99.901813"
   },
   "TN":{
      "lat":"35.517491",
      "long":"-86.580447"
   },
   "TX":{
      "lat":"31.968599",
      "long":"-99.901813"
   },
   "UT":{
      "lat":"39.32098",
      "long":"-111.093731"
   },
   "VA":{
      "lat":"37.431573",
      "long":"-78.656894"
   },
   "VT":{
      "lat":"44.558803",
      "long":"-72.577841"
   },
   "WA":{
      "lat":"47.751074",
      "long":"-120.740139"
   },
   "WI":{
      "lat":"43.78444",
      "long":"-88.787868"
   },
   "WV":{
      "lat":"38.597626",
      "long":"-80.454903"
   },
   "WY":{
      "lat":"43.075968",
      "long":"-107.290284"
   }
}

Then you can compose data using map function and spread operator and add coordinates to every item of array, e.g.

    const data = [{
        "date": 20200421,
        "state": "AK"
      },
      {
        "date": 20200421,
        "state": "AL",
        "positive": 5231,
        "negative": 43295
       }
       ]

       const dictionary = {'AK': {lat:"63.588753", long: "-154.493062"},
       'AL': {lat:"32.318231", long: "-86.902298"}}

       const result = data.map(item => ({...item, lat: dictionary[item.state].lat, long: dictionary[item.state].long }))
       
       console.log(result)
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement