Home » Php » php Json remove double quotes for key and how to change name of the key

php Json remove double quotes for key and how to change name of the key

Posted by: admin February 25, 2020 Leave a comment

Questions:

Sample Json

data:[{"varExchange":"ftx","varPrice":482.3},{"varExchange":"ftx","varPrice":481.8}]
  1. I want to change the name of the key from “varExchange” to “name” and “varPrice” to “qty”
  2. I need to remove the double quotes for key.

Output Like

 data:[{name:"test","qty":482.3},{name:"test","qty":481.8}]

Kindly help me to fix this.
Thanks

How to&Answers:

First make proper input json array.

Then create new array as per your need for change the key

var data = [{"varExchange":"ftx","varPrice":482.3},{"varExchange":"ftx","varPrice":481.8}];
var new_data = [];
for(var k in data){
  
  new_data.push({name : data[k].varExchange, qty : data[k].varPrice });
}

console.log(new_data);

Answer:

I think its very simple..

  1. Just convert the JSON object to string by using json.stringify()
  2. replace your substrings i.e. “varExchange” with name and “varPrice” with qty.

and again you can cast it into object.