-1

I want to change json key but I can not manage

I tried to write a function:

modifySlotKey(requestObjectIntent, slotValue) {

requestObjectIntent['slots']['toChange'] = requestObjectIntent['slots'][slotValue]

return requestObjectIntent;
}

but it doesn't work.

My json is like that:

"slots": {
  "toChange": {
    "name": "query",
    "value": "torte",
    "confirmationStatus": "NONE",
    "source": "USER"
  }
}

but I want to change it to:

"slots": {
  "slotValue": {
    "name": "query",
    "value": "torte",
    "confirmationStatus": "NONE",
    "source": "USER"
  }
}

Could somebody help me please to solve this issue?

R.J. Dunnill
  • 2,049
  • 3
  • 10
  • 21
Anna K
  • 1,666
  • 4
  • 23
  • 47
  • 2
    Possible duplicate of [How to rename JSON key](https://stackoverflow.com/questions/13391579/how-to-rename-json-key) – TheMri May 28 '19 at 20:58
  • When you say `JSON`, you implicitly says you have a `string`. So, you have a `string` or an `object` as input? – Shidersz May 28 '19 at 20:59
  • I have an Object and it is not the same case as @TheMri suggest!!!!!!!!!!!!!!!!!!!! – Anna K May 28 '19 at 21:27

1 Answers1

2

your code is fine but you only need to change the order of variables(slotValue = 'toChange') and delete de older key before return.

requestObjectIntent['slots'][slotValue] = requestObjectIntent['slots']['toChange];
delete(requestObjectIntent['slots']['toChange']);

I do a little example and works fine: https://jsfiddle.net/g7t4Lan5/1/

Maxi Schvindt
  • 1,432
  • 1
  • 11
  • 20