Is it possible to move a particular JSON key to the top of the JSON using Javascript/VuejJS/Nodejs package?

BATMAN_2008

I have a web application built using the Vuejs within that I am obtaining a JSON from the Java backend service which has a format something like this:


{
  "a" : "Value-A",
  "c" : "Value-C",
  "b" : "Value-B",
  "schema" : "2.0"
}

Before displaying the result to the user I would like to move the schema to the top so that it would be easy for the user to read and I want it to look something like this:

{
  "schema" : "2.0",
  "a" : "Value-A",
  "c" : "Value-C",
  "b" : "Value-B"
}

As we can see only schema position has changed, the rest of the JSON as it is.

Please Note:

  1. I am aware the JSON order does not matter but I am doing this for better readability purposes. If there is a way then it would be really useful for the reader to understand the JSON better.

  2. I want to know if there is a direct way to do it rather than looping over the JSON, as my created JSON in the real application can be pretty large.

  3. All I want to do is move the schema to the top of the JSON. The rest of the JSON can be as it is I do not want to make any modifications to it.

  4. Is there a way to do this using vanilla Javascript or using some Nodejs library as I am using the Vuejs?

I would really appreciate it if there was a way to do it or is there any workaround for this.

evolutionxbox

A very simplistic approach could be to stringify a new object.

const myObject = {
  "a" : "Value-A",
  "c" : "Value-C",
  "b" : "Value-B",
  "schema" : "2.0"
};

console.log(
  JSON.stringify({
    schema: myObject.schema,
    ...myObject
  }, null, 2)
);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Json array move to top

How to preserve data types of a particular key in JSON Object using GSON?

Using Python to Display a particular value through key in Json file

Using Python to insert a particular value through key in Json file

find particular key in json and insert parent object to it using python

Fetch the Json for a particular key value

How to Read json file and write the change to values of particular key in the same json file using Javascript?

How to skip top key of json?

Is it possible to host MERN application on Heroku using single package.json?

Aggregating values of particular key in json array - PostgreSQL

Getting a particular key value for all object in JSON

How to get JSON object values for a Particular key

PHP/Laravel: create JSON with a particular Key name

How to Add the particular key values in JSON

How to check for a particular key exists in json string or not

Cypress: Stubbing a particular key in JSON response

Finding the latest index of a particular key with inconsistent JSON?

Is it possible to POST a package of json objects?

Is it possible to rename a json key name using its value in Python?

JSON - renaming and duplicating a key - is it possible?

Convert a particular JSON element to integer using javascript

Should package.json `name` and `version` be placed at the top level of JSON?

npm install: Is there a way to ignore a particular dependency in package.json

How to move a key to up level from nested JSON object using JOLT Transformation

Is it possible to reference a property in package.json

Conan package manager and CMakePresets.json? Is it possible?

how to apply key-decode-fn only to top level keys while json unmarshaling using metosin

Extract top-level key and contents from large JSON using stream

Move SAP package to top level