1

Below is the object

{
  'File_12345.ZLM': {    
    MeterID_12345: {     
      BASIC_INFO: [Object]

    }
  }
}
{
  'File_678910.ZLM': {
    MeterID_678910: {
      BASIC_INFO: [Object],
    }
  }
}

===============================================================================================
I want File_12345.ZLM and File_678910.ZLM replaced with key name as "FileName" and MeterID_12345 and MeterID_678910 replaced with "MeterId"

So Expected Output would be as below

{
  'FileName': {    
    MeterId: {     
      BASIC_INFO: [Object]

    }
  }
}
{
  'FileName': {
    MeterId: {
      BASIC_INFO: [Object],
    }
  }
}
4
  • Post the expected Object structure in the question itself. Commented Sep 6, 2022 at 10:42
  • is the input and output are arrays? Commented Sep 6, 2022 at 10:45
  • No in output keys are changed, i have shown how the expected output should be Commented Sep 6, 2022 at 10:47
  • 1
    Is Stringify -> RegEx -> Parse an option? Commented Sep 6, 2022 at 11:01

4 Answers 4

2

As @efkah pointed out, the RegEx solution here:

const files = [{'File_12345.ZLM':{MeterID_12345:{BASIC_INFO:[]}}},{'File_678910.ZLM':{MeterID_678910:{BASIC_INFO:[]}}}];

const renamed = JSON.stringify(files)
  .replaceAll(/File_\d+\.ZLM/g, 'FileName')
  .replaceAll(/MeterID_\d+/g, 'MeterId');
  
const result = JSON.parse(renamed);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0 }

Sign up to request clarification or add additional context in comments.

Comments

1

The idea is to grab the keys of the objects, since there is only one. With that we get to know the key we want to replace. Do it for both keys and voilà!

I've added a second way to do it, to flatten the objects a bit to make them easier to use, and to also not loose the filename and meterid info.

const files = [{
  'File_12345.ZLM': {    
    MeterID_12345: {     
      BASIC_INFO: []

    }
  }
},
{
  'File_678910.ZLM': {
    MeterID_678910: {
      BASIC_INFO: [],
    }
  }
}];

console.log(files.map(f=>{
  const filename = Object.keys(f)[0];
  f.FileName = f[filename]; delete f[filename];
  const MeterId = Object.keys(f.FileName)[0];
  f.FileName.MeterId = f.FileName[MeterId]; delete f.FileName[MeterId];
  return f;
}));

const files2 = [{
  'File_12345.ZLM': {    
    MeterID_12345: {     
      BASIC_INFO: []

    }
  }
},
{
  'File_678910.ZLM': {
    MeterID_678910: {
      BASIC_INFO: [],
    }
  }
}];
console.log(files2.map(f=>{
  const filename = Object.keys(f)[0];
  const MeterId = Object.keys(f[filename])[0];
  return {FileName:filename,MeterId,BASIC_INFO:f[filename][MeterId].BASIC_INFO};
}));

Comments

0
    const data = [{
    'File_12345.ZLM':{
        MeterID_12345: {
            BASIC_INFO: []
            } 
        }
    },{ 'File_678910.ZLM': { MeterID_678910: { BASIC_INFO: [], } } }]
    
const obj = data.map(item => {
    const Meterkeys = Object.keys(item)
    const Meterkey = Meterkeys.find(k => k.includes('File'))
    if (Meterkey) {
        const Filekeys = Object.keys(item[Meterkey])
        const Filekey = Filekeys.find(k => k.includes('Meter'))
        const res = {
            ...item,
            FileName: {
                ...item[Meterkey],
                MeterId: (item[Meterkey])[Filekey]
            }
        }
        delete res[Meterkey]
        delete res.FileName[Filekey]
        return res;
    } else {
        return item
    }
    })
console.log(obj)

Comments

0
const obj = {oldKey: 'value'};

obj['newKey'] = obj['oldKey'];
delete obj['oldKey'];

console.log(obj); // 👉️ {newKey: 'value'}

3 Comments

keys are not static they are dynamic so cannot target it specifically.
ok will do it ! thanks for the feedback
@Aksheet-Goku we can use the looping method right !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.