Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8645507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:34:08+00:00 2026-06-12T12:34:08+00:00

I have multiple JSON files that I would like to parse, edit, and merge

  • 0

I have multiple JSON files that I would like to parse, edit, and merge into one object, which will ultimately get re-encoded and output as a single JSON.

I currently have a working example of when I parse and edit one object, but I can’t figure out how to do two and get a single result.

Working example:

$source = 'http://www.jakedup.com/_/source1';

$data = json_decode(file_get_contents($source));

// THIS WILL BE THE FINAL OUTPUT OBJECT
$output = new stdClass();

// GET THE ARTICLE DATES AS AN ARRAY
$tracks = get_object_vars($data->tracks);

// LOOPS OVER ARTICLE PUBDATES TO GET THE OUTER OBJECTS
foreach ($tracks as $key=>$val) {
  // LOOPS OVER THE INNER OBJECTS IN EACH ARTICLE PUBDATE
  foreach ($data->tracks->$key as $object) {
    // ADD IT ONTO THE OUTPUT OBJECT WITH "ID" AS PARENT NAME
    $output->{$object->id} = $object;
  }
}

echo json_encode($output);

Here are my JSON examples. I am providing two examples, but I am looking for a way to merge an infinite number of JSON files dynamically.

JSON Source1:

{
    "foo":"bar",
    "tracks":{
        "1349496000":[
            {
                "id":"25328",
                "is_promo":null,
                "is_feature":null,
                "listens":"1312",
                "downloads":"777"
            },
            {
                "id":"25327",
                "is_promo":null,
                "is_feature":null,
                "listens":"1255",
                "downloads":"578"
            },
            {
                "id":"25329",
                "is_promo":null,
                "is_feature":null,
                "listens":"341",
                "downloads":"139"
            }
        ],
        "1349323200":[
            {
                "id":"25310",
                "is_promo":null,
                "is_feature":null,
                "listens":"10793",
                "downloads":"4953"
            }
        ]
    }
}

JSON Source 2:

{
    "foo":"bar",
    "tracks":{
        "1349323200":[
            {
                "id":"25303",
                "is_promo":null,
                "is_feature":null,
                "listens":"2347",
                "downloads":"1499"
            }
        ],
        "1349236800":[
            {
                "id":"25266",
                "is_promo":null,
                "is_feature":null,
                "listens":"24485",
                "downloads":"19366"
            }
        ]
    }
}

I have been toying around with the idea of grabbing sources in a loop, but I am stuck when it gets to merging the data. All my different efforts have not produced the right data structure.

Current test code:

$source = 'http://www.jakedup.com/_/source';
$pages = 2;

    for ($i = 1; $i <= $pages; $i++) {
        $sources[$i] = $source.$i;
        ${'source'.$i} = json_decode(file_get_contents($source.$i));
    }

I now have two objects — $source1 & $source2 — but I’m not sure what to do next.

This is the output I am expecting:

{
    "25328": {
        "id": "25328",
        "is_promo": null,
        "is_feature": null,
        "listens": "1312",
        "downloads": "777",
        "timestamp": "1349496000"
    },
    "25327": {
        "id": "25327",
        "is_promo": null,
        "is_feature": null,
        "listens": "1255",
        "downloads": "578",
        "timestamp": "1349496000"
    },
    "25329": {
        "id": "25329",
        "is_promo": null,
        "is_feature": null,
        "listens": "341",
        "downloads": "139",
        "timestamp": "1349496000"
    },
    "25310": {
        "id": "25310",
        "is_promo": null,
        "is_feature": null,
        "listens": "10793",
        "downloads": "4953",
        "timestamp": "1349323200"
    },
    "25303": {
        "id": "25303",
        "is_promo": null,
        "is_feature": null,
        "listens": "2347",
        "downloads": "1499",
        "timestamp": "1349323200"
    },
    "25266": {
        "id": "25266",
        "is_promo": null,
        "is_feature": null,
        "listens": "24485",
        "downloads": "19366",
        "timestamp": "1349236800"
    }
}

Thanks in advance for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:34:10+00:00Added an answer on June 12, 2026 at 12:34 pm

    This is a little more simply done with a PHP array rather than trying to build the stdClass object with numeric properties. json_encode() will automatically produce an object {} as opposed to an array [] if the PHP array has non-consecutive, non-zero based keys. Likewise json_decode() has an optional second parameter to produce an associative array rather than an object. So I would recommend decoding the files as an array and looping over them, appending each id onto an output array, which you will ultimately json_encode() again.

    Your code implies that you have JSON source files like ‘http://www.jakedup.com/_/source1’. You were on the right track, but instead of populating new variable variables, retrieve and decode each file in a loop, and append it directly to the output array.

    // 5 source numbers, don't have to be consecutive
    $sources = array(1,2,3,4,5);
    // Output array to be encoded as JSON
    $output = array();
    
    // Loop over all the source files, retrieve them and add tracks onto the output    
    foreach ($sources as $sourcenum) {
      $source = "http://www.jakedup.com/_/source$sourcenum";
      $source_json = file_get_contents($source);
    
      // Decode it as an associative array, passing TRUE as second param
      $source_array = json_decode($source_json, TRUE);
    
      // Now loop over the tracks (which are an array) and append each to the output
      // the original key looks like a timestamp
      foreach ($source_array['tracks'] as $timestamp => $ts) {
        foreach ($ts as $track) {
          // Add the track id to $output as a key
          // This doesn't check if the id already exists...
          // if it does, it will just be overwritten with this one
          $output[$track['id']] = $track;
          // Optionally, save the original timestamp key into the track array
          // Maybe you don't care about this
          $output[$track['id']]['timestamp'] = $timestamp;
        }
      }
    }
    
    // Look over your array
    var_dump($output);
    
    // Finally, re-encode the whole thing back to JSON
    // using JSON_FORCE_OBJECT (even though it shouldn't be necessary)
    $output_json = json_encode($output, JSON_FORCE_OBJECT);
    

    JSON output:

    {
        "25328": {
            "id": "25328",
            "is_promo": null,
            "is_feature": null,
            "listens": "1312",
            "downloads": "777",
            "timestamp": 1349496000
        },
        "25327": {
            "id": "25327",
            "is_promo": null,
            "is_feature": null,
            "listens": "1255",
            "downloads": "578",
            "timestamp": 1349496000
        },
        "25329": {
            "id": "25329",
            "is_promo": null,
            "is_feature": null,
            "listens": "341",
            "downloads": "139",
            "timestamp": 1349496000
        },
        "25310": {
            "id": "25310",
            "is_promo": null,
            "is_feature": null,
            "listens": "10793",
            "downloads": "4953",
            "timestamp": 1349323200
        },
        "25303": {
            "id": "25303",
            "is_promo": null,
            "is_feature": null,
            "listens": "2347",
            "downloads": "1499",
            "timestamp": 1349323200
        },
        "25266": {
            "id": "25266",
            "is_promo": null,
            "is_feature": null,
            "listens": "24485",
            "downloads": "19366",
            "timestamp": 1349236800
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I will have 3 <select multiple=multiple> and I would like to fill them using
I need to run a query that looks would look like INSERT INTO Appointments
On my site, I have a PHP script which takes multiple source files (HTML,CSS,
I have thousands of text files containing multiple JSON objects, but unfortunately there is
Pulling my hair out here. Jquery, Multiple JSON files & Ajax I have a
I have multiple projects which are to be hosted together in a Tomcat container,
I have multiple UpdatePanels on a webpage, one inside UserControl (Purple ColorBox as show
I'm currently having an issue where I have a javascript object that is trying
I have some ajax calls in multiple JavaScript functions. Each one does a post
How can I pass multiple variables between PHP files using jquery with json? Ex:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.