1

I need to send an HTTP API some JSON in the body like:

var body = {
            'param1': 'param1 value',
            'param2': 'param2 value',
            'param3': 'param3 value',
            'param4': 'param4 value',
            'param5': 'param5 value',
            'param6': 'param6 value'
       };

But the source I have to build that from is

var datasource = {
            'x4001': 'param1 value',
            'jd5jj': 'param2 value',
            'mmmmmoose': 'param3 value',
            'simple': 'param4 value',
            'crayonx3': 'param5 value',
            'hubbabubba': 'param6 value'
       };

There must be a quick jQuery replace/map function to create my 'body' variable from the datasource JSON - so that 'x4001' becomes 'param1', 'jd5jj' becomes 'param2', etc., etc. to get the body I crave?

in other answers I've seen, I see things like:

function renameProperty(obj, fromKey, toKey) {
  obj[toKey] = obj[fromKey];
  delete obj[fromKey];
}

addObjectResponse.forEach(obj => renameProperty(obj, 'SP02', 'O2'));

But my JavaScript / jQuery just doesn't understand anything with => in it - (which looks like LINQ statements from C#).

So - either I'm using the wrong jQuery, or I need a different solution?

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Robert Achmann
  • 1,986
  • 3
  • 40
  • 66
  • in the samples provided in the answer, I see things like : body.forEach(obj => renameProperty(obj, 'Title', 'requestor name')); or addObjectResponse.forEach(obj => renameProperty(obj, 'SP02', 'O2')); ... well my javascript / jquery doesn't understand anything like '=>' it just chokes, so those answers don't work – Robert Achmann Aug 18 '17 at 18:16
  • It also demonstrates the setting of the new key and removal of the old key, constituting a 'rename' – Taplar Aug 18 '17 at 18:17

2 Answers2

2

Since you're renaming all the keys in datasource, it's simplest to define a "key map" and use that to look up new names from old:

var keymap = {
  x4001: "param1",
  jd5jj: "param2",
  mmmmmoose: "param3",
  simple: "param4",
  crayonx3: "param5",
  hubbabubba: "param6"
};

var datasource = {
  x4001: "param1 value",
  jd5jj: "param2 value",
  mmmmmoose: "param3 value",
  simple: "param4 value",
  crayonx3: "param5 value",
  hubbabubba: "param6 value"
};

var body = {};

for (var key in datasource) 
  body[keymap[key]] = datasource[key];

console.log(body);
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
0

You can use JSON.stringify() and String.prototype.replace() with RegExp /"([\w\d])+"(?=:)/g

var n = 0;
var datasource = {
  'x4001': 'param1 value',
  'jd5jj': 'param2 value',
  'mmmmmoose': 'param3 value',
  'simple': 'param4 value',
  'crayonx3': 'param5 value',
  'hubbabubba': 'param6 value'
};
var body = JSON.parse(
             JSON.stringify(datasource)
             .replace(/"([\w\d]+)"(?=:)/g, function(match) {
               return `"param${++n}"`
             })
           );

console.log(body);

var n = 0;
var arr = ["a", "b", "c", "d", "e", "f"];
var datasource = {
  'x4001': 'param1 value',
  'jd5jj': 'param2 value',
  'mmmmmoose': 'param3 value',
  'simple': 'param4 value',
  'crayonx3': 'param5 value',
  'hubbabubba': 'param6 value'
};
var body = JSON.parse(
             JSON.stringify(datasource)
             .replace(/"([\w\d]+)"(?=:)/g, function(match) {
               return `"${arr[n++]}"`
             })
           );

console.log(body);
guest271314
  • 1
  • 15
  • 104
  • 177