-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?

3
  • 6
    You'll need to write code, which you do not appear to have even attempted to do. Commented Nov 23, 2021 at 14:38
  • 1
    Why you want to modify them ? What is logic behind it and what you have tried so far ? Commented Nov 23, 2021 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 relaxedjson.org JSON requires the Keys to be within quotes. Commented Nov 23, 2021 at 14:50

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

3 Comments

This not gonna generate an array!
@nimeresam he wants the result to be an object, not an array !
Sorry, just got confused.
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
  }), {}))

2 Comments

@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.
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.
0

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;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.