'How to convert nested json data into a comma separated list with javascript

var data=  [
      {
        name: "productname",
        id: "1356",
        price: "0.00",
        category: "Health",
        position: "1",
        list: "New Products",
        stocklevel: "20",
        brand: "Health"
      },
      {
        name: "productname2",
        id: "5263",
        price: "0",
        category: "Hair",
        position: "2",
        list: "New Products",
        stocklevel: "",
        brand: "Hair"
      },
      {
        name: "productname3",
        id: "7473",
        price: "0.00",
        category: "skin",
        position: "3",
        list: "New Products",
        stocklevel: "10",
        brand: "skin"
      },
      
      
     ]

i have data of multiple product as in nested order the product could be upto n.

but i want this data into commma seprated values As list so i can make it anylaytical friendly.

Like this

Product1: "productname",
Product1Price: 0.00,
Product1id: "1356",
Product1brand: "health", 
Product1stocklevel: "20",
Product2: "productname2",
Product2price: 0,
Product2id: "5263",
Product2brand: "hair",
Product2stocklevel: "",
Product3: "productname3",
Product3price: 0.00,
Product3id: "7473",

The product details should be show out of the nexted loop

We can separate key and values with map function. But as all product data is in same format so i am confused about how map function would work here.



Solution 1:[1]

You can still use .map along with details from this answer to iterate each property:

var data = [{
    name: "productname",
    id: "1234",
    price: "0.00",
    category: "Health",
    position: "1",
    list: "New Products",
    stocklevel: "20",
    brand: "Health"
  },
  {
    name: "productname2",
    id: "2345",
    price: "0",
    category: "Hair",
    position: "2",
    list: "New Products",
    stocklevel: "",
    brand: "Hair"
  },
  {
    name: "productname3",
    id: "356anything",
    price: "0.00",
    category: "skin",
    position: "3",
    list: "New Products",
    stocklevel: "10",
    brand: "skin"
  },
]
console.log(
  data.map((e, i) => {
    return Object.keys(e).map(p => `Product${i+1}${p=="name"?"":p}:"${e[p]}"`).join(",\n")
  }).join(",\n")
);

Added \n to match the expected output, unclear if you want them on a single line (comma separated list) or different lines as in your sample.

If you don't want the number values in quotes, then you'll have check if they're numbers first etc as your original has them in quotes.

Alternatively, and if you want them in a specific order / different case (one of your examples has Product1Price instead of Product1price) then you'll have to hardcode the output, as in the other answer.

Solution 2:[2]

The data format you are asking is not analytic friendly!

var data = [
  {
    name: "productname",
    id: "1",
    price: "0.00",
    category: "Health",
    position: "1",
    list: "New Products",
    stocklevel: "20",
    brand: "Health"
  },
  {
    name: "productname2",
    id: "2",
    price: "0",
    category: "Hair",
    position: "2",
    list: "New Products",
    stocklevel: "",
    brand: "Hair"
  },
  {
    name: "productname3",
    id: "3",
    price: "0.00",
    category: "skin",
    position: "3",
    list: "New Products",
    stocklevel: "10",
    brand: "skin"
  },
]

var result = {}

data.forEach(function(e) {
  var key = "Product" + e.id
  result[key] = e.name
  result[key + "Price"] = e.price
  result[key + "Category"] = e.category
  result[key + "Position"] = e.position
  result[key + "List"] = e.list
  result[key + "Stocklevel"] = e.stocklevel
  result[key + "Brand"] = e.brand
})

console.log('result: ', result)

var data = {
  1356: {
    name: "productname",
    id: "1356",
    price: "0.00",
    category: "Health",
    position: "1",
    list: "New Products",
    stocklevel: "20",
    brand: "Health"
  },
  5263: {
    name: "productname2",
    id: "5263",
    price: "0",
    category: "Hair",
    position: "2",
    list: "New Products",
    stocklevel: "",
    brand: "Hair"
  },
  7473: {
    name: "productname3",
    id: "7473",
    price: "0.00",
    category: "skin",
    position: "3",
    list: "New Products",
    stocklevel: "10",
    brand: "skin"
  }
}

// Access to any product in the data set
console.log('product 5263: ', data[5263])

// And also access to any product property directly from the dataset
console.log('product name 5263: ', data[5263].name)

// Looping through the array of goods and selecting the required fields
Object.keys(data).map(function(key) {
  var product = data[key]
  // Output only name and price:
  console.log(product.name, ' ', product.price)
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2