Skip to content

Instantly share code, notes, and snippets.

@Abdelkrim
Last active May 16, 2017 12:54
Show Gist options
  • Save Abdelkrim/715eb222cc318219196c8be293c233bf to your computer and use it in GitHub Desktop.
Save Abdelkrim/715eb222cc318219196c8be293c233bf to your computer and use it in GitHub Desktop.
Combine 2 JSON files into 1 file using Python (i.e. longitude and latitude)
{
"tags": [{
"name": "LATITUDE_deg",
"results": [{
"groups": [{
"name": "type",
"type": "number"
}],
"values": [
[1123306773000, 46.9976859318, 3],
[1123306774000, 46.9976859319, 3]
],
"attributes": {
"customer": ["Acme"],
"host": ["server1"]
}
}],
"stats": {
"rawCount": 2
}
}]
}
{
"tags": [{
"name": "LATITUDE_deg",
"results": [{
"groups": [{
"name": "type",
"type": "number"
}],
"values": [
[1123306773000, 46.9976859318, 3],
[1123306774000, 46.9976859319, 3]
],
"attributes": {
"customer": ["Acme"],
"host": ["server1"]
}
}],
"stats": {
"rawCount": 200
}
}]
}
{
"tags": [{
"name": "LONGITUDE_deg",
"results": [{
"groups": [{
"name": "type",
"type": "number"
}],
"values": [
[1123306773000, 36.9976859318, 3],
[1123306774000, 36.9976859317, 3]
],
"attributes": {
"customer": ["Acme"],
"host": ["server1"]
}
}],
"stats": {
"rawCount": 2
}
}]
}
{
"tags": [{
"name": "LONGITUDE_deg",
"results": [{
"groups": [{
"name": "type",
"type": "number"
}],
"values": [
[1123306773000, 36.9976859318, 3],
[1123306774000, 36.9976859317, 3]
],
"attributes": {
"customer": ["Acme"],
"host": ["server1"]
}
}],
"stats": {
"rawCount": 200
}
}]
}
'''Copyright 2017 ALT-F1 SPRL (http://www.alt-f1.be), AMIA SYSTEMS S.A.(http://www.amia-systems.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''
# Read the use case on Stack Overflow: http://bit.ly/2rmm3Uu
import copy
import json
JSON_LAT_FILES = [r"data/lat1.json", r"data/lat2.json"]
JSON_LONG_FILES = [r"data/long1.json", r"data/long2.json"]
JSON_LAT_LONG_FILES = [r"data/lat_long1.json", r"data/lat_long2.json"]
def get_string_from_file(file_name):
with open(file_name, 'r') as my_file:
return my_file.read().replace('\n', '')
def merge_two_json_files(lat_filename, long_filename, out_filename):
lat_string = get_string_from_file(lat_filename)
lat_data = json.loads(lat_string)
long_string = get_string_from_file(long_filename)
long_data = json.loads(long_string)
lat_long_data = copy.deepcopy(lat_data)
lat_long_data['tags'][0]['name'] = (lat_data['tags'][0]['name'] + ' ' +
long_data['tags'][0]['name'])
lat_values = lat_data['tags'][0]['results'][0]['values']
long_values = long_data['tags'][0]['results'][0]['values']
lat_long_values = []
assert (len(lat_values) == len(long_values))
for idx in range(len(lat_values)):
lat_value = lat_values[idx]
long_value = long_values[idx]
lat_long_value = [lat_value[0], lat_value[1], long_value[1],
lat_value[2]]
lat_long_values.append(lat_long_value)
lat_long_data['tags'][0]['results'][0]['values'] = lat_long_values
out_file = open(out_filename, 'w')
out_file.write(json.dumps(lat_long_data, ensure_ascii=False, indent=True))
out_file.close()
def merge_json_files():
assert(len(JSON_LAT_FILES) == len(JSON_LONG_FILES) ==
len(JSON_LAT_LONG_FILES))
for idx in range(len(JSON_LAT_FILES)):
merge_two_json_files(JSON_LAT_FILES[idx], JSON_LONG_FILES[idx],
JSON_LAT_LONG_FILES[idx])
def main():
merge_json_files()
if __name__ == "__main__":
main()
@Abdelkrim
Copy link
Author

USE CASE:

I want to append the longitude to a latitude stored in 2 separated json files (long1.json, lat1.json) and (long2.json, lat2.json)

The result should be stored in a 3rd file (lat_long1.json and lat_long2.json)

See http://bit.ly/2rmm3Uu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment