Merge and combine two JSON files

My dilemma involves two JSON files:

F1_data.json:

{
    "A": {
        "time": "2015-11-26T08:20:15.130Z",
    },
    "B": {
        "time": "2015-11-26T08:30:19.432Z",   
    {
    "C": {
        "time": "2015-11-26T08:20:15.130Z",  
    }
}

F2_data.json:

[{
        "oaci": "A",
        "latitude": "45.979722222222",
        "longitude": "5.3377777777778"
    },
    {
        "oaci": "B",
        "latitude": "46.123333333333",
        "longitude": "5.8047222222222"
    },
    {
        "oaci": "C",
        "latitude": "47.123333333333",
        "longitude": "8.8047222222222"
    },

    {
        "oaci": "D",
        "latitude": "46.205555555556",
        "longitude": "5.2916666666667"
    }
]

My objective is to cross-check the "oaci" values and append latitude and longitude from F2_data.json into F1_data.json accordingly.

The desired output would be:

F1_data.json

{
    "A": {  
        "time": "2015-11-26T08:20:15.130Z",
         "latitude": "45.979722222222",
        "longitude": "5.3377777777778"
    },
    "B": {  
        "time": "2015-11-26T08:30:19.432Z", 
        "latitude": "46.123333333333",
        "longitude": "5.8047222222222"  
    {
    "C": {  
            "time": "2015-11-26T08:20:15.130Z", 
            "latitude": "47.123333333333",
            "longitude": "8.8047222222222" 
        }
}

I have begun working on this in JSON but welcome any pointers or assistance.

Answer №1

Here is a sample code snippet in JavaScript:

var obj = {
    "A": {
        "time": "2015-11-26T08:20:15.130Z"
    },
    "B": {
        "time": "2015-11-26T08:30:19.432Z"   
    },
    "C": {
        "time": "2015-11-26T08:20:15.130Z" 
    }
}

var list = [
    {
        "oaci": "A",
        "latitude": "45.979722222222",
        "longitude": "5.3377777777778"
    },
    {
        "oaci": "B",
        "latitude": "46.123333333333",
        "longitude": "5.8047222222222"
    },
    {
        "oaci": "C",
        "latitude": "47.123333333333",
        "longitude": "8.8047222222222"
    },
    {
        "oaci": "D",
        "latitude": "46.205555555556",
        "longitude": "5.2916666666667"
    }
]

function getListItem(name){
    for(var i = 0; i < list.length; i++){
        if(list[i].oaci == name){
            return list[i];   
        }
    }
}

for(var prop in obj){
    var listItem = getListItem(prop);
    obj[prop].latitude = listItem.latitude;
    obj[prop].longitude = listItem.longitude;
}

console.log(JSON.stringify(obj, null, 2));

The final output from the above code:

{
  "A": {
    "time": "2015-11-26T08:20:15.130Z",
    "latitude": "45.979722222222",
    "longitude": "5.3377777777778"
  },
  "B": {
    "time": "2015-11-26T08:30:19.432Z",
    "latitude": "46.123333333333",
    "longitude": "5.8047222222222"
  },
  "C": {
    "time": "2015-11-26T08:20:15.130Z",
    "latitude": "47.123333333333",
    "longitude": "8.8047222222222"
  }
}

Answer №2

To implement the timestamps object as t and the coordinate list as c, you can use the following code:

for (let timestamp in t) {
    for (let coordinate in c) {
        if (c[coordinate]["oaci"] === timestamp) {
            t[timestamp]["latitude"] = c[coordinate]["latitude"];
            t[timestamp]["longitude"] = c[coordinate]["longitude"];
        }
    }
}

Answer №3

If you have objects labeled as F1 and F2, this code snippet can help achieve the desired outcome:

F2.forEach(function(_data){
    var currentData = F1[_data.oaci];
    if (currentData) for (var prop in _data) if (prop !== 'oaci'){
        currentData[prop] = _data[prop];
    }
});

The final result will be stored within the F1 object.


This solution is designed to adapt dynamically, allowing for seamless operation even when additional properties are added to the objects.


Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilize JSON Arrays within a JSON structure when programming in Java

Within my Array list are integer values ranging from 2 to 6, and I am checking if each number is odd or even using the code snippet below: JSONObject outerObject = new JSONObject(); JSONArray outerArray = new JSONArray(); JSONObject [] innerObject = new J ...

Using the `ssh2` module in Node.js without specifying a specific object

When you use require('ssh2') in Node.js without a specific object (e.g. require('ssh2').Client), what does it imply? I'm in the process of understanding some code and still new to learning Node.js. Here's how it is being used: ...

Using jQuery and Ajax to retrieve data from a PHP script's response

As a novice in jquery and Ajax functions, I am working on a website that displays data from a database using PHP. When the edit button is clicked, I want to show the information for each field. I have learned that using ajax is the best approach for this t ...

Tips for transferring the asp:Label ID from an asp:CheckBox

I have searched various online forums without success in finding a solution to my issue. I am looking for a way to retrieve an asp:Label's id and pass it to a JavaScript function using the "onclick" event from a nested asp:CheckBox. My goal is to pas ...

Looking for a jQuery plugin that creates a sticky top menu bar?

I am in search of a jQuery plugin (or code/guide) that can achieve this effect: Unfortunately, this particular one is not available for free Please take note that the navigation bar does not initially appear at the top but sticks once the viewport reache ...

The single-page anchor link is positioned just below a few pixels, connecting with the #link

Hey there! I'm currently working on a single-page website and running into an issue. When I click on the anchor link in the menu, it seems to go slightly below the intended area, just like in this image https://i.sstatic.net/3hAvO.jpg I want the beha ...

What methods and technologies are accessible for executing JavaScript through PHP?

Are there any frameworks or tools available to execute JavaScript from PHP? Is there a similar project like the Harmony project for PHP? I am interested in running JS unit tests (or even BDD) directly from PHP, as demonstrated in this post (for Ruby). Am ...

Looking to minimize the amount of HTML code in Angularjs by utilizing ng-repeat

Working with some HTML <tr class="matrix_row" ng-repeat="process in matrixCtrl.processes | filter : { park: parentIndex } track by $index"> <td class="properties" ng-click="dashboardCtrl.currParam=0; dashboardCtrl.currentProcess=process"> ...

Integrating CSS with Material-UI in a React project: A step-by-step guide

I am currently developing a project using React (along with TypeScript) and the Material-UI library. One of my requirements is to implement an animated submit button, while replacing the default one provided by the library. After some research, I came acr ...

Changing a string into a JavaScript date object

I am encountering an issue where I have a string retrieved from a JSON object and attempting to convert it to a JavaScript date variable. However, every time I try this, it returns an invalid date. Any insights into why this might be happening? jsonObj["d ...

Unable to start node.js server to upload file in node with express and multer

As a novice in the realm of node.js, I am attempting to create a website using Express, allowing me to upload various content. The server is currently set up locally on my Mac. Below is the code that I have written: server.js var express = require(&apos ...

Flask template fails to render following a redirect

I have embarked on creating a simple CARTO application using a combination of Vue JS and Flask. If you wish to explore the entire code, it can be accessed from this github repository. Here's how the application is designed to function: The user m ...

After the JavaScript calculation is completed, the following display may show a value of NaN

Check out my JavaScript calculation in action: Live Preview I've encountered an issue with the calculation script. After the initial calculation, it shows a NAN value on subsequent calculations without reloading the page. Is there a way to enable rep ...

The collaboration of TypeScript and Vue in implementing index signatures resulted in the TS7053 specification

Utilizing Vue 2.6 with vue class component along with typescript. Here is the current code snippet: private validateField(fieldType: string, e: string) { this[fieldType] = e.length > 0 } An error is being thrown: Error TS7053: Element implicitly h ...

Trouble initializing Google Maps in an Angular directive

I'm currently working on integrating the Google Maps API into an Angular website, but I'm encountering initialization issues. Within my HTML page, I'm utilizing bootstrap nav nav-tabs and switching between tabs using an Angular controller. ...

Providing optimal image dimensions based on the size of the viewing window

In the process of creating an image hosting website, I have taken on the challenge to familiarize myself with different programming languages such as PHP, MySQL, HTML, CSS, and JavaScript. Currently, the website loads full-size images and resizes them to ...

What are the steps to set up Redis Store in my production environment?

I am currently in the process of setting up Redis as a session store, but for some reason it's not functioning properly. I have integrated passport.js and express-flash, however when I attempt to run the current Redis setup, it fails to work: var ses ...

Retrieving specific properties from a JSON object and logging them

I am attempting to access JSON Object properties directly and log them using the following function : loadProcesses(filter?){ this._postService.getAllProcess(filter) .subscribe( res=> { this.processListe = res; // console.log(this.p ...

The error message "Property 'destroy' of undefined cannot be read in DataTables"

I need to develop a function that can create a new DataTable. If there is an existing table, I want the function to first destroy it and then create the new one. This is what I have so far: $.ajax().done(function(response){ Init_DT(response[& ...

Creating multiple countdowns in AngularJS using ng-repeat is a handy feature to have

Currently, I have a small AngularJS application that is designed to search for users and display their upcoming meetings. The data is retrieved from the server in JSON format, with time displayed in UTC for easier calculation to local times. While I have s ...