0

I have a json object as below :

var jsonData = [
{COUNTRY_NAME: "Belgium", COUNTRY_CODE: "BE", TERRITORY: "BENELUX", MARKET: "Benelux", REGION: "NORTH"},
{COUNTRY_NAME: "Italy", COUNTRY_CODE: "IT", TERRITORY: "ITALY", MARKET: "ITALY", REGION: "SOUTH"},
{COUNTRY_NAME: "Spain", COUNTRY_CODE: "SP", TERRITORY: "SPAIN", MARKET: "SPAIN", REGION: "SOUTH"},...
];

I want to extract specific keys with its values ( COUNTRY_NAME and REGION ) from each array inside jsonData so my final result should be as below :

var jsonFinal = [
{COUNTRY_NAME: "Belgium", REGION: "NORTH"},
{COUNTRY_NAME: "Italy", REGION: "SOUTH"},
{COUNTRY_NAME: "Spain", REGION: "SOUTH"},...
];

I could make it work but I believe there is a better way to do it. What I did was :

I loop through each array inside my json object and remove the keys I don't need but now I am in situation that I have multiple keys to remove and don't want to delete one by one.

Any suggestions please how can I extract specific keys from a json object and add to another json object ? Thank you very much.

var jsonData = [
{COUNTRY_NAME: "Belgium", COUNTRY_CODE: "BE", TERRITORY: "BENELUX", MARKET: "Benelux", REGION: "NORTH"},
{COUNTRY_NAME: "Italy", COUNTRY_CODE: "IT", TERRITORY: "ITALY", MARKET: "ITALY", REGION: "SOUTH"},
{COUNTRY_NAME: "Spain", COUNTRY_CODE: "SP", TERRITORY: "SPAIN", MARKET: "SPAIN", REGION: "SOUTH"}
];



for (var i = 0; i < jsonData.length; i++) { 
       //console.log(jsonData[i]);
       delete jsonData[i]['COUNTRY_CODE'];
       delete jsonData[i]['TERRITORY'];
       delete jsonData[i]['MARKET'];
}

var finalJSON = jsonData;

console.log(finalJSON);       
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

3
  • Does this answer your question? Javascript- Retrieve specific key values from JSON object Commented Jul 28, 2020 at 18:05
  • @CharlesBamford thanks for your time but I saw that thread and was following it but couldn't make it work on my case. Commented Jul 28, 2020 at 18:06
  • 1
    Use jQuery for manipulating the dom. This is normal js stuff. Commented Jul 28, 2020 at 18:07

3 Answers 3

1

The simplest way in modern Javascript is to use map with object destructuring:

var jsonData = [
  {COUNTRY_NAME: "Belgium", COUNTRY_CODE: "BE", TERRITORY: "BENELUX", MARKET: "Benelux", REGION: "NORTH"},
  {COUNTRY_NAME: "Italy", COUNTRY_CODE: "IT", TERRITORY: "ITALY", MARKET: "ITALY", REGION: "SOUTH"},
  {COUNTRY_NAME: "Spain", COUNTRY_CODE: "SP", TERRITORY: "SPAIN", MARKET: "SPAIN", REGION: "SOUTH"}
];

var transformed = jsonData.map(({ COUNTRY_NAME, REGION }) => ({ COUNTRY_NAME, REGION }));

console.log(transformed);

Sign up to request clarification or add additional context in comments.

3 Comments

much appreciated. Thank you very much.
I have a further question concerning your solution. Can you please advise how to escape a special character in a key with your solution ? here is my jsfiddle jsfiddle.net/f8Lr3yk4
Sorry for the delay in replying. Unfortunately you can't use the same shorthand form if your key is not a valid JS variable name. But you can still use the "longhand" form where you specify both the key and a name (necessarily different) for the value, eg: jsonData.map(({ COUNTRY_NAME, REGION, "order#": order }) => ({ COUNTRY_NAME, REGION, "order#": order }));
1

You can do this without mutating the original array.

var jsonData = [{
    COUNTRY_NAME: "Belgium",
    COUNTRY_CODE: "BE",
    TERRITORY: "BENELUX",
    MARKET: "Benelux",
    REGION: "NORTH"
  },
  {
    COUNTRY_NAME: "Italy",
    COUNTRY_CODE: "IT",
    TERRITORY: "ITALY",
    MARKET: "ITALY",
    REGION: "SOUTH"
  },
  {
    COUNTRY_NAME: "Spain",
    COUNTRY_CODE: "SP",
    TERRITORY: "SPAIN",
    MARKET: "SPAIN",
    REGION: "SOUTH"
  }
];

let newData = []
for (let v of jsonData) {
  newData.push({
    COUNTRY_NAME: v.COUNTRY_NAME,
    REGION: v.REGION
  })
}

console.log(newData);

1 Comment

Thank you very much for your help
1
var jsonData = [
{COUNTRY_NAME: "Belgium", COUNTRY_CODE: "BE", TERRITORY: "BENELUX", MARKET: "Benelux", REGION: "NORTH"},
{COUNTRY_NAME: "Italy", COUNTRY_CODE: "IT", TERRITORY: "ITALY", MARKET: "ITALY", REGION: "SOUTH"},
{COUNTRY_NAME: "Spain", COUNTRY_CODE: "SP", TERRITORY: "SPAIN", MARKET: "SPAIN", REGION: "SOUTH"}
];

 var finalData=[];
jsonData.forEach(buidFinalData);

function buidFinalData(item, index) {
  finalData.push({
  COUNTRY_NAME: item.COUNTRY_NAME, REGION: item.REGION
  })
}

console.log(finalData);

1 Comment

Thank you very much for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.