6

I have this JSON file: http://danish-regional-data.googlecode.com/svn/trunk/danish_regional_data.json

How do I remove all the attribues within_5_km, within_10_km, within_25_km, within_50_km, within_100_km for all postcodes?

I have read this question: Remove a JSON attribute

$(document).ready(function() {

    $.getJSON("post.json", function(data) {

    var pc = data.postalcodes;
    for (var id in pc) {
       if(pc.hasOwnProperty(id)) {
          for(var attr in pc[id]) {
             if(pc[id].hasOwnProperty(attr) && attr.indexOf('within_') === 0) {
               delete pc[id][attr];
             }
          }
       }
    }

    $("#json").html(pc);

    });

});
Community
  • 1
  • 1
Rails beginner
  • 14,321
  • 35
  • 137
  • 257
  • 1
    Well, you need to recursively loop over your JSON object, searching for those attributes, and delete them if/when they're found, per the answer in your linked question. – Matthemattics Nov 05 '12 at 14:42
  • 5
    **Warning** 12MB `.json` file in the question – andyb Nov 05 '12 at 14:43
  • If you're looking to do this after a JSON call to get the data it would be redundant as you'd have had to download them all first to remove them. – Rory McCrossan Nov 05 '12 at 14:45
  • @Lübnah - Can you show how to do that? Because in the linked question there is no loop or search :/ – Rails beginner Nov 05 '12 at 14:45
  • @Lübnah, actually there is no such thing as JSON object. JSON is used as a representation of an object of specified type. – jwaliszko Nov 05 '12 at 14:46
  • 1
    @JaroslawWaliszko Okay, that's a bit pedantic, but yes that's technically correct. JSON == JavaScript Object Notation. So, what I /meant/ was the object the OP has expressed in JSON. Happy now? – Matthemattics Nov 05 '12 at 14:47

5 Answers5

3

In ES2016 you can use destructing to pick the fields you want for the subset object.

//ES6 subset of an object by specific fields
var object_private = {name: "alex", age: 25, password: 123};
var {name,age} = object_private, object_public = {name,age}


//method 2 using literals
let object_public = (({name,age})=>({name,age}))(object_private);


//use map if array of objects
    users_array.map(u=>u.id)
Alex Gulakov
  • 87
  • 1
  • 5
2

Go to the json url you provided and open the Firebug console. Then drop in the folloing code and execute it:

var p = document.getElementsByTagName('pre');
for(i=0; i < p.length; i++) {

  var data = JSON.parse(p[i].innerHTML);
  var pc = data.postalcodes;

  // this is the code i gave you... the previous is jsut to pull it out of the page
  // in Firebug - this works for me

  for (var id in pc) {
     if(pc.hasOwnProperty(id)) {
        for(var attr in pc[id]) {
          if(pc[id].hasOwnProperty(attr) && attr.indexOf('within_') === 0) {
             console.log('Deleting postalcodes.'+id+'.'+attr);
             delete pc[id][attr];
           }
        }
     }
  }
}

// assume data is the complete json

var pc = data.postalcodes;
for (var id in pc) {
   if(pc.hasOwnProperty(id)) {
      for(var attr in pc[id]) {
         if(pc[id].hasOwnProperty(attr) && attr.indexOf('within_') === 0) {
           delete pc[id][attr];
         }
      }
   }
}
prodigitalson
  • 60,050
  • 10
  • 100
  • 114
1

JSON truncated:

var data = {"postalcodes":
{"800":{"id":"800","name":"H\u00f8je Taastrup","region_ids":["1084"],"region_names":["Hovedstaden"],"commune_ids":["169"],"commune_names":["H\u00f8je-Taastrup"],"lat":"55.66713","lng":"12.27888", "within_5_km":["800","2620","2630","2633"],"within_10_km":["800","2600","2605","2620"]},
"900":{"id":"900","name":"K\u00f8benhavn C","region_ids":["1084"],"region_names":["Hovedstaden"],"commune_ids":["101"],"commune_names":["K\u00f8benhavns"],"lat":"55.68258093401054","lng":"12.603657245635986","within_5_km":["900","999"]},
"1417":{"commune_id":"390","region_id":"1085"}}};
var pc = data.postalcodes;
for (var id in pc) {
    var entry = pc[id];
    for(var attr in entry) {
        if(attr.indexOf('within_') === 0) {
            delete entry[attr];
        }
    }
}
console.dir(data); // your data object has been augmented at this point

you can also use regular expression

var data = {"postalcodes":
{"800":{"id":"800","name":"H\u00f8je Taastrup","region_ids":["1084"],"region_names":["Hovedstaden"],"commune_ids":["169"],"commune_names":["H\u00f8je-Taastrup"],"lat":"55.66713","lng":"12.27888", "within_5_km":["800","2620","2630","2633"],"within_10_km":["800","2600","2605","2620"]},
"900":{"id":"900","name":"K\u00f8benhavn C","region_ids":["1084"],"region_names":["Hovedstaden"],"commune_ids":["101"],"commune_names":["K\u00f8benhavns"],"lat":"55.68258093401054","lng":"12.603657245635986","within_5_km":["900","999"]},
"1417":{"commune_id":"390","region_id":"1085"}}};
var regexp = new RegExp("^within_", "i");   // case insensitive regex matching strings starting with within_
var pc = data.postalcodes;
for (var id in pc) {
    var entry = pc[id];
    for(var attr in entry) {
        if(regexp.test(attr)) {
            delete entry[attr];
        }
    }
}
console.dir(data);
cbayram
  • 2,259
  • 11
  • 9
1

I have written an npm module unset which exactly does this. You specify json paths similar to the json-path module until the leaf attribute that you want to remove.

let unset = require('unset');
let object = {a: { b: [ {x: 1}, {x: [{ e: 2} ]}]}};
let newObject = unset(object, ['/a/b[*]/x']);

Multiple paths are supported in the second argument

Srivathsa
  • 941
  • 1
  • 10
  • 27
0

well you can do this:

var postalcodes = YOUR JSON;

for(var code in postalcodes)
{
 delete postalcodes[code].within_5_km;
 .
 .
 .
}

you will probably want to check if the code contains your properties...

AMember
  • 3,037
  • 2
  • 33
  • 64