-4

I have a batch of JSON files looking like this :

{
  "properties": [
    {
      "Frame": "Yellow"
    },
    {
      "Sky": "Apocalypse"
    },
    {
      "Grass": "green"
    },
    {
      "Sign": "sign"
    },
    {
      "Graffitis": "shit"
    },
    {
      "Things": "can"
    },
    {
      "Quotes": "emptyness"
    },
    {
      "Border": "framing"
    },
    {
      "Logo": "logo"
    },
    {
      "Overlay": "overlay"
    }
  ]
}

And I want to modify them so they look like this :

{
  Border: "framing",
  Frame: "Yellow",
  Graffitis: "shit",
  Grass: "green",
  Logo: "logo",
  Overlay: "overlay",
  Quotes: "emptyness",
  Sign: "sign",
  Sky: "Apocalypse",
  Things: "can"
}

How can I achieve this?

Ivar
  • 6,138
  • 12
  • 49
  • 61
  • 6
    You'll need to write code, which you do not appear to have even attempted to do. – Scott Hunter Nov 23 '21 at 14:38
  • 1
    Why you want to modify them ? What is logic behind it and what you have tried so far ? – Sudhir Ojha Nov 23 '21 at 14:39
  • You need to refine your question. If Assuming all the JSON files has the property "properties" with only one item, you require something.properties[0]. And assuming you need them without the "" for the keys, for some reason. If this is the case you can look at relaxedjson http://www.relaxedjson.org/ JSON requires the Keys to be within quotes. – Rajsanthosh Sreenivasan Nov 23 '21 at 14:50

3 Answers3

2

Use Object.assign like so

const input = {
  "properties": [
    {
      "Frame": "Yellow"
    },
    {
      "Sky": "Apocalypse"
    },
    {
      "Grass": "green"
    },
    {
      "Sign": "sign"
    },
    {
      "Graffitis": "shit"
    },
    {
      "Things": "can"
    },
    {
      "Quotes": "emptyness"
    },
    {
      "Border": "framing"
    },
    {
      "Logo": "logo"
    },
    {
      "Overlay": "overlay"
    }
  ]
}    
const output = Object.assign({}, ...input.properties);
nip
  • 1,609
  • 10
  • 20
1

You can use Array.prototype.reduce() and spread syntax to accomplish this in one line:

let obj = {
  "properties": [{
    "Frame": "Yellow"
  }, {
    "Sky": "Apocalypse"
  }, {
    "Grass": "green"
  }, {
    "Sign": "sign"
  }, {
    "Graffitis": "shit"
  }, {
    "Things": "can"
  }, {
    "Quotes": "emptyness"
  }, {
    "Border": "framing"
  }, {
    "Logo": "logo"
  }, {
    "Overlay": "overlay"
  }]
}

console.log(obj["properties"].reduce((prev, current) =>
  ({ ...prev,
    ...current
  }), {}))
Taxel
  • 3,859
  • 1
  • 18
  • 40
  • 1
    Please try to avoid answering a question where the user is posting his requirement without any attempts. – Nitheesh Nov 23 '21 at 14:47
  • @Nitheesh There is no reason to avoid answering questions that shouldn't be closed. And showing attempts [is in itself not a requirement when asking questions](https://meta.stackoverflow.com/questions/260828/do-we-need-a-close-reason-for-zero-effort-questions). – Ivar Nov 23 '21 at 14:55
  • Yes, there is no rule for that. But you could better mark this question for close with specific reason. So that user can concentrate more on he problem and try to produce atleast one solution for the problem. This is only a personal openion. – Nitheesh Nov 23 '21 at 15:01
0

Hey and welcome to StackOverflow!

I recommend using a for-loop to iterate through an object you have parsed from the json (JS read json file and use as an object):

let newObject = {};
properties.forEach(property => {
  Object.assign(newObject, property);
}
return newObject;
benicamera
  • 750
  • 1
  • 7
  • 24
  • 1
    Please try to avoid answering a question where the user is posting his requirement without any attempts. – Nitheesh Nov 23 '21 at 14:47