PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 23, 2022

[FIXED] What is the best way to transfer certain data to another json document?

 July 23, 2022     electron, javascript, json     No comments   

Issue

Explanation:

I want to make an Electron app [Javascript not jQuary] (or am in the process of doing so) and would like to add a function that puts one config into the "format" of another.

The big file from which I want to take the information I currently read in via "dialog.showOpenDialog" and can also access the json object.

Now to the problem:

The file I get via the dialog is 8000 lines long and contains individual information that I want to pack into a smaller document with about 3000 lines. Important: Individual information have a different name e.g. I want "ABCD: 23" from document 1 in the other as EFG: 23.

Now my two questions:

  1. how can I best provide the smaller file for editing?
  2. how can I convert the individual information without going through each line separately?

bigconfig.json:

{
    "EXAMPLE_CATEGORY": {
        "setting1": 0,
        "setting2": 1,
        "setting3": 115,
        "setting4": 0,
    },

Smallerconfig.json

{
    "EXAMPLE_CATEGORY": {
        "setting7": 115,
        "setting8": 0,
    },

Edit: What I want to achieve is that I can create (and save) a modified file with the information I packed from the big file into the small one.

In the smaller one should be all 3000 felt

Would really appreciate help... yesterday I did a lot of research and used the search engine for several hours.

Thanks in advance


Solution

The only way your smallerConfig object will know which new keys to use is if you define them beforehand. To do this, you must create an object that links the old key names to the new key names. These links would be best defined in one place. The code below holds these links in the conversionTable.

To build the smallerConfig object, you must loop (using for...in) through the bigConfig object one line at a time. Here you will check if the key in the bigConfig object matches a key in the conversionTable (using the in operator). If a matching key is found, then we will use the key’s value in the conversionTable as the new key in the smallerConfig object. Using the bigConfig value in the creation of the smallerConfig object is easy.

let bigConfig = {
    'EXAMPLE_CATEGORY': {
        'setting1': 0,
        'setting2': 1,
        'setting3': 115,
        'setting4': 0
    }
};

let smallerConfig = {
    'EXAMPLE_CATEGORY': {}
};

let conversionTable = {
    'setting3': 'setting7',
    'setting4': 'setting8'
};

// Iterate through the bigConfig object
for (let bigKey in bigConfig.EXAMPLE_CATEGORY) {
    // Check for a matching key in the conversionTable
    if (bigKey in conversionTable) {
        smallerConfig.EXAMPLE_CATEGORY[conversionTable[bigKey]] = bigConfig.EXAMPLE_CATEGORY[bigKey];
    }
}

console.log(smallerConfig);

Output will be:

{
    'EXAMPLE_CATEGORY': {
        'setting7': 115,
        'setting8': 0
    }
}

Finally:

  • Use JSON.parse() to convert the file contents from a string to a Javascript object.
  • Use JSON.stringify() to convert the Javascript object back to a string for writing to the new file.


Answered By - midnight-coding
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing