0

I need to add a title to each object in json response.

What would be the simplest way to achieve this?

{ "ADA": { "Price": "1.8600", "Volume": "11590.74", "High": "1.8650", "Low": "1.8061", "Sell": "1.8649", "Buy": "1.8110" }, "BAT": { "Price": "3.73", "Volume": "445.25", "High": "3.78", "Low": "3.61", "Sell": "3.78", "Buy": "3.73" }, "BCC": { "Price": "4299.00", "Volume": "10.85", "High": "4399.00", "Low": "4250.00", "Sell": "4299.00", "Buy": "4196.00" }, "BSV": { "Price": "2800.00", "Volume": "0.29", "High": "2815.00", "Low": "2760.00", "Sell": "2800.00", "Buy": "2750.00" }, "BTC": { "Price": "197500.00", "Volume": "37.40", "High": "197500.00", "Low": "195212.00", "Sell": "197500.00", "Buy": "197109.00" }, "BTCP": { "Price": "3.30", "Volume": "4607.16", "High": "3.30", "Low": "3.25", "Sell": "3.30", "Buy": "3.25" }, "BTG": { "Price": "132.00", "Volume": "8.68", "High": "137.00", "Low": "132.00", "Sell": "137.00", "Buy": "132.00" }, "BTT": { "Price": "0.0060", "Volume": "6296706.85", "High": "0.0061", "Low": "0.0059", "Sell": "0.0060", "Buy": "0.0059" }, "COMP": { "Price": "1922.00", "Volume": "0.55", "High": "1927.00", "Low": "1763.00", "Sell": "1920.00", "Buy": "1800.00" }, "DAI": { "Price": "17.79", "Volume": "803.49", "High": "17.79", "Low": "17.25", "Sell": "17.78", "Buy": "17.25" }, "DASH": { "Price": "1141.00", "Volume": "0.84", "High": "1190.01", "Low": "1141.00", "Sell": "1190.01", "Buy": "1141.00" }, "DOGE": { "Price": "0.0500", "Volume": "501931.08", "High": "0.0500", "Low": "0.0440", "Sell": "0.0500", "Buy": "0.0445" }, "ETH": { "Price": "6395.00", "Volume": "47.18", "High": "6420.00", "Low": "6310.00", "Sell": "6420.00", "Buy": "6395.00" }, "GAS": { "Price": "26.00", "Volume": "89.58", "High": "26.90", "Low": "25.51", "Sell": "26.90", "Buy": "25.51" }, "LTC": { "Price": "820.97", "Volume": "33.77", "High": "820.97", "Low": "807.00", "Sell": "820.97", "Buy": "807.00" }, "NEO": { "Price": "297.00", "Volume": "17.80", "High": "306.00", "Low": "296.00", "Sell": "307.00", "Buy": "297.00" }, "NMC": { "Price": "7.18", "Volume": "242.75", "High": "7.18", "Low": "7.05", "Sell": "7.19", "Buy": "7.18" }, "USDT": { "Price": "17.28", "Volume": "7083.53", "High": "17.48", "Low": "17.22", "Sell": "17.48", "Buy": "17.28" }, "USDT_BTC": { "Price": "10000.00", "Volume": "0.00", "High": "0.00", "Low": "0.00", "Sell": "11600.00", "Buy": "10001.00" }, "TRX": { "Price": "0.4579", "Volume": "422526.82", "High": "0.4600", "Low": "0.4482", "Sell": "0.4579", "Buy": "0.4500" }, "XAG": { "Price": "632.01", "Volume": "0.12", "High": "632.01", "Low": "632.01", "Sell": "668.00", "Buy": "632.01" }, "XAU": { "Price": "33500.00", "Volume": "0.01", "High": "34000.00", "Low": "33500.00", "Sell": "33700.00", "Buy": "33500.00" }, "XLM": { "Price": "1.42", "Volume": "17796.91", "High": "1.46", "Low": "1.34", "Sell": "1.42", "Buy": "1.38" }, "XMR": { "Price": "2016.00", "Volume": "2.53", "High": "2160.00", "Low": "2016.00", "Sell": "2159.90", "Buy": "2005.00" }, "XRP": { "Price": "4.21", "Volume": "47094.53", "High": "4.23", "Low": "4.11", "Sell": "4.21", "Buy": "4.20" }, "ZEC": { "Price": "1050.00", "Volume": "2.32", "High": "1130.00", "Low": "1050.00", "Sell": "1130.00", "Buy": "1050.00" }, "XZAR": { "Price": "1.00", "Volume": "0.00", "High": "1.00", "Low": "1.00", "Sell": "1.00", "Buy": "1.00" } }

EDIT 1:

Hi everyone thank you for the responses so far, I must apologise for the confusion in original post, let me reiterate issue.

I am looking to transform the above to this

{
  "pair": {
    "title": "ADA",
    "Price": "1.8600",
    "Volume": "11590.74",
    "High": "1.8650",
    "Low": "1.8061",
    "Sell": "1.8649",
    "Buy": "1.8110"
  },
  "pair": {
    "title": "BAT",
    "Price": "3.73",
    "Volume": "445.25",
    "High": "3.78",
    "Low": "3.61",
    "Sell": "3.78",
    "Buy": "3.73"
  }
cgd
  • 87
  • 1
  • 6
  • Familiarize yourself with [how to access and process nested objects, arrays or JSON](https://stackoverflow.com/q/11922383/4642212) and use the available [`Object`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object#Methods_of_the_Object_constructor) and [`Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Methods) methods. – Sebastian Simon Oct 18 '20 at 06:45
  • what will be the value of the title? – Jaromanda X Oct 18 '20 at 06:51
  • @JaromandaX edited post for clarity :) – cgd Oct 18 '20 at 07:33
  • `Object.entries(obj).forEach(([title, v]) => Object.assign(v,{title}))` – Jaromanda X Oct 18 '20 at 08:18
  • @JaromandaX I understand what you are doing, but how do I apply it to one of the below examples? – cgd Oct 18 '20 at 08:56
  • well .... obj is `obj` in one example, and `data` in the other – Jaromanda X Oct 18 '20 at 09:50
  • re: edit 1 so the title has been added - next step is to replace ADA with the word pair. Because its now defined in {..} "pair": { "title": "ADA", "Price": "1.8600", "Volume": "11590.74", "High": "1.8650", "Low": "1.8061", "Sell": "1.8649", "Buy": "1.8110" } – cgd Oct 18 '20 at 10:00
  • You cannot have the same key name (`pair`) for several elements of an object. Only the last one exists. – Mister Jojo Oct 18 '20 at 10:30
  • @MisterJojo - would adding the word pair to each key work? ie. "pair": "ADA" My aim is to ingest all in frontend without having to name each key.. – cgd Oct 18 '20 at 10:46
  • if you have `data = { pair : 'aa', pair : 'bb', pair : 'cc' }` is like `data.pair = 'aa'; data.pair = 'bb'; data.pair = 'cc';` then you have only the last one : `data.pair = 'cc';` – Mister Jojo Oct 18 '20 at 10:52
  • I understand, could I remove the keys entirely? this api endpoint is doing it https://api.valr.com/v1/public/marketsummary and I can ingest it quite easily. – cgd Oct 18 '20 at 10:55
  • N objects without key names ? this is not Javascript – Mister Jojo Oct 18 '20 at 11:01
  • your Link is not about an Object, but about an **Array** of Objects – Mister Jojo Oct 18 '20 at 11:12

3 Answers3

1

You can iterate the object using for..in , access each object value and add a new key to that object

const data = {
  "ADA": {
    "Price": "1.8600",
    "Volume": "11590.74",
    "High": "1.8650",
    "Low": "1.8061",
    "Sell": "1.8649",
    "Buy": "1.8110"
  },
  "BAT": {
    "Price": "3.73",
    "Volume": "445.25",
    "High": "3.78",
    "Low": "3.61",
    "Sell": "3.78",
    "Buy": "3.73"
  }
}
for (let keys in data) {
  data[keys] = Object.assign({}, data[keys], {
    'title': keys
  })
};

console.log(data)
brk
  • 48,835
  • 10
  • 56
  • 78
1

You can loop through the object using then add the title using Object.assign():

var obj = { "ADA": { "Price": "1.8600", "Volume": "11590.74", "High": "1.8650", "Low": "1.8061", "Sell": "1.8649", "Buy": "1.8110" }, "BAT": { "Price": "3.73", "Volume": "445.25", "High": "3.78", "Low": "3.61", "Sell": "3.78", "Buy": "3.73" }, "BCC": { "Price": "4299.00", "Volume": "10.85", "High": "4399.00", "Low": "4250.00", "Sell": "4299.00", "Buy": "4196.00" }, "BSV": { "Price": "2800.00", "Volume": "0.29", "High": "2815.00", "Low": "2760.00", "Sell": "2800.00", "Buy": "2750.00" }, "BTC": { "Price": "197500.00", "Volume": "37.40", "High": "197500.00", "Low": "195212.00", "Sell": "197500.00", "Buy": "197109.00" }, "BTCP": { "Price": "3.30", "Volume": "4607.16", "High": "3.30", "Low": "3.25", "Sell": "3.30", "Buy": "3.25" }, "BTG": { "Price": "132.00", "Volume": "8.68", "High": "137.00", "Low": "132.00", "Sell": "137.00", "Buy": "132.00" }, "BTT": { "Price": "0.0060", "Volume": "6296706.85", "High": "0.0061", "Low": "0.0059", "Sell": "0.0060", "Buy": "0.0059" }, "COMP": { "Price": "1922.00", "Volume": "0.55", "High": "1927.00", "Low": "1763.00", "Sell": "1920.00", "Buy": "1800.00" }, "DAI": { "Price": "17.79", "Volume": "803.49", "High": "17.79", "Low": "17.25", "Sell": "17.78", "Buy": "17.25" }, "DASH": { "Price": "1141.00", "Volume": "0.84", "High": "1190.01", "Low": "1141.00", "Sell": "1190.01", "Buy": "1141.00" }, "DOGE": { "Price": "0.0500", "Volume": "501931.08", "High": "0.0500", "Low": "0.0440", "Sell": "0.0500", "Buy": "0.0445" }, "ETH": { "Price": "6395.00", "Volume": "47.18", "High": "6420.00", "Low": "6310.00", "Sell": "6420.00", "Buy": "6395.00" }, "GAS": { "Price": "26.00", "Volume": "89.58", "High": "26.90", "Low": "25.51", "Sell": "26.90", "Buy": "25.51" }, "LTC": { "Price": "820.97", "Volume": "33.77", "High": "820.97", "Low": "807.00", "Sell": "820.97", "Buy": "807.00" }, "NEO": { "Price": "297.00", "Volume": "17.80", "High": "306.00", "Low": "296.00", "Sell": "307.00", "Buy": "297.00" }, "NMC": { "Price": "7.18", "Volume": "242.75", "High": "7.18", "Low": "7.05", "Sell": "7.19", "Buy": "7.18" }, "USDT": { "Price": "17.28", "Volume": "7083.53", "High": "17.48", "Low": "17.22", "Sell": "17.48", "Buy": "17.28" }, "USDT_BTC": { "Price": "10000.00", "Volume": "0.00", "High": "0.00", "Low": "0.00", "Sell": "11600.00", "Buy": "10001.00" }, "TRX": { "Price": "0.4579", "Volume": "422526.82", "High": "0.4600", "Low": "0.4482", "Sell": "0.4579", "Buy": "0.4500" }, "XAG": { "Price": "632.01", "Volume": "0.12", "High": "632.01", "Low": "632.01", "Sell": "668.00", "Buy": "632.01" }, "XAU": { "Price": "33500.00", "Volume": "0.01", "High": "34000.00", "Low": "33500.00", "Sell": "33700.00", "Buy": "33500.00" }, "XLM": { "Price": "1.42", "Volume": "17796.91", "High": "1.46", "Low": "1.34", "Sell": "1.42", "Buy": "1.38" }, "XMR": { "Price": "2016.00", "Volume": "2.53", "High": "2160.00", "Low": "2016.00", "Sell": "2159.90", "Buy": "2005.00" }, "XRP": { "Price": "4.21", "Volume": "47094.53", "High": "4.23", "Low": "4.11", "Sell": "4.21", "Buy": "4.20" }, "ZEC": { "Price": "1050.00", "Volume": "2.32", "High": "1130.00", "Low": "1050.00", "Sell": "1130.00", "Buy": "1050.00" }, "XZAR": { "Price": "1.00", "Volume": "0.00", "High": "1.00", "Low": "1.00", "Sell": "1.00", "Buy": "1.00" } }

for (const [key, value] of Object.entries(obj)) {
  Object.assign(value, {Title: key});
}
console.log(obj);
Mamun
  • 66,969
  • 9
  • 47
  • 59
1

in fact you want to get an Array...

const data = 
      { "ADA": { "Price": "1.8600", "Volume": "11590.74", "High": "1.8650", "Low": "1.8061", "Sell": "1.8649", "Buy": "1.8110"} 
      , "BAT": { "Price": "3.73", "Volume": "445.25", "High": "3.78", "Low": "3.61", "Sell": "3.78", "Buy": "3.73"} 
      , "BCC": { "Price": "4299.00", "Volume": "10.85", "High": "4399.00", "Low": "4250.00", "Sell": "4299.00", "Buy": "4196.00"} 
      , "BSV": { "Price": "2800.00", "Volume": "0.29", "High": "2815.00", "Low": "2760.00", "Sell": "2800.00", "Buy": "2750.00"} 
      , "BTC": { "Price": "197500.00", "Volume": "37.40", "High": "197500.00", "Low": "195212.00", "Sell": "197500.00", "Buy": "197109.00"} 
      , "BTCP": { "Price": "3.30", "Volume": "4607.16", "High": "3.30", "Low": "3.25", "Sell": "3.30", "Buy": "3.25"} 
      , "BTG": { "Price": "132.00", "Volume": "8.68", "High": "137.00", "Low": "132.00", "Sell": "137.00", "Buy": "132.00"} 
      , "BTT": { "Price": "0.0060", "Volume": "6296706.85", "High": "0.0061", "Low": "0.0059", "Sell": "0.0060", "Buy": "0.0059"} 
      } 

const ArrData = Object.entries(data)
                  .map(([key, val])=>({title: key, ...val})) 

console.log( ArrData )
.as-console-wrapper { max-height: 100% !important; top: 0; }
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40