0

I have a complex object where the key of each object property should actually be a value. So, I am trying to convert this:

{
  "2019-04-24T00:00:00Z": {
    "one": 185,
  },
  "2019-04-25T00:00:00Z": {
    "one": 207,
  }
}

to this:

{
  {
    "date": "2019-04-24T00:00:00Z",
    "one": 185,
  },
  {
    "date": "2019-04-25T00:00:00Z",
    "one": 207,
  }
}

I have pasted a snippet with an attempt.

var obj2 = {"all":[{"branches":[{"branchname":"NAN","mains":[{"mainName":"FirstOne","plName":[{"metrics":{"2019-04-24T00:00:00Z":{"one":185,"two":143697,"three":126161,"four":3909,"five":0,"six":0.2304,"seven":30.72,"eight":13.333,"nine":1.165},"2019-04-25T00:00:00Z":{"one":207,"two":50819,"three":16428,"four":2820,"five":0,"six":0.329,"seven":57.166,"eight":15.442,"nine":1.422}}}]}]}]}]};

var json2 = jsonPath(obj2, "$.all..branches..mains..[*].plName[*].metrics.");

var found = Object.keys(json2).find(function(layerKey) {
  console.log(json2[layerKey]);
});

for (var i = 0; i<= Object.keys(json2).length; i++) {
  for (var prop in json2[i]) {
    console.log("Key:" + prop);
    //Add date as a value in the JSON
    //json2[i].map(i=>i.Date=prop);
    json2[i]["Date"] = prop;
  }
}

//json2.map(i=>i.Date=prop);
console.log(json2);
<script src="https://www.w3resource.com/JSON/jsonpath.js"></script>
VLAZ
  • 26,331
  • 9
  • 49
  • 67
Datacrawler
  • 2,780
  • 8
  • 46
  • 100

5 Answers5

2

You can try using nested map

var obj2 = {"all":[{"branches":[{"branchname":"NAN","mains":[{"mainName":"FirstOne","plName":[{"metrics":{"2019-04-24T00:00:00Z":{"one":185,"two":143697,"three":126161,"four":3909,"five":0,"six":0.2304,"seven":30.72,"eight":13.333,"nine":1.165},"2019-04-25T00:00:00Z":{"one":207,"two":50819,"three":16428,"four":2820,"five":0,"six":0.329,"seven":57.166,"eight":15.442,"nine":1.422}}}]}]}]}]};

var json2 = jsonPath(obj2, "$.all..branches..mains..[*].plName[*].metrics");

var result = json2.map(e =>
  Object.keys(e).map(m => Object.assign(e[m], {
    data: m
  }))
)

console.log(result)
<script src="https://www.w3resource.com/JSON/jsonpath.js"></script>
User863
  • 19,346
  • 2
  • 17
  • 41
2

You may want an array as output, because the output you provide is not a valid js object literal.

const obj = {"2019-04-24T00:00:00Z": {"one": 185,},"2019-04-25T00:00:00Z": {"one": 207,}};

const newArr = Object.keys(obj).map(item=>({...obj[item],date:item}));


console.log(newArr);
Emeeus
  • 5,072
  • 2
  • 25
  • 37
1

Try this:

const a = {
  "2019-04-24T00:00:00Z": {
    "one": 185,
  },
  "2019-04-25T00:00:00Z": {
    "one": 207,
  }
}

const result = Object.keys(a).reduce((acc, ele)=> acc =  [...acc,{date: ele, one: a[ele].one}] ,[]);

console.log(result);
Ghoul Ahmed
  • 4,446
  • 1
  • 14
  • 23
1

There you go:

var example = {"2019-04-24T00:00:00Z": {"one": 185}, "2019-04-25T00:00:00Z": {"one": 207}}

var result = []
for (var key in example) {
    var objectCopy = JSON.parse(JSON.stringify(example[key]));
    objectCopy['date'] = key;
    result.push(objectCopy); 
}
console.log(result)
<script src="https://www.w3resource.com/JSON/jsonpath.js"></script>
Software Person
  • 2,526
  • 13
  • 18
1

To me, Object.entries() is the most natural way to achieve your goal:

var obj2 = {"all":[{"branches":[{"branchname":"NAN","mains":[{"mainName":"FirstOne","plName":[{"metrics":{"2019-04-24T00:00:00Z":{"one":185,"two":143697,"three":126161,"four":3909,"five":0,"six":0.2304,"seven":30.72,"eight":13.333,"nine":1.165},"2019-04-25T00:00:00Z":{"one":207,"two":50819,"three":16428,"four":2820,"five":0,"six":0.329,"seven":57.166,"eight":15.442,"nine":1.422}}}]}]}]}]};

var json2 = jsonPath(obj2, "$.all..branches..mains..[*].plName[*].metrics.");

const res = json2.map(item => Object.entries(item).map(([date, obj]) => ({...obj,date})));

console.log(res);
.as-console-wrapper {min-height: 100%}
<script src="https://www.w3resource.com/JSON/jsonpath.js"></script>
Yevhen Horbunkov
  • 14,965
  • 3
  • 20
  • 42