1

I have a web service that accept JSON in a specific format. However, I'm given a JSON of values but with different key names.

I know it's inefficient at the moment since it's just simply projecting data from one to another.

All I need to do is convert this:

var json1 = { key1: "Value1", key2: "Value2", key3: "Value3" };

to

var json2 = { state: "Value1", city: "Value2, zipcode: "Value3" };

I looked into $.map but I'm not really sure how to use it in this case.

sksallaj
  • 3,872
  • 3
  • 37
  • 58
  • This looks like a normal JavaScript object, not a string. – Felix Kling Mar 12 '14 at 18:01
  • ah sorry, I used JSON.stringify on my code.. I'll add it – sksallaj Mar 12 '14 at 18:02
  • 1
    So, you have an object with specific properties and want to rename those properties while you are converting it to JSON? You could also just rename the properties on the object directly. This doesn't really have anything to do with JSON in particular. Have a look at http://stackoverflow.com/questions/4647817/javascript-object-rename-key – Felix Kling Mar 12 '14 at 18:03
  • ah! I was over complicating things a bit, the first thought was to use $.map, and I spent time trying to use that. Also thought about a foreach and looping through attributes. Geez! – sksallaj Mar 12 '14 at 18:10

2 Answers2

8

Are you just trying to do this?

var json1 = { key1: "Value1", key2: "Value2", key3: "Value3" };

var json2 = {
    state : json1.key1,
    city : json1.key2, 
    zip : json1.key3
};

alert(json2.city);

fiddle: http://jsfiddle.net/TTzqW/

4m1r
  • 12,234
  • 9
  • 46
  • 58
  • Given that you are working with objects, the variable names are a bit misleading. – Felix Kling Mar 12 '14 at 18:07
  • wow that was simple! I over complicated things a bit. Felix, I receive json objecs, and I convert to strings later in my code. Sorry that it slipped my mind. – sksallaj Mar 12 '14 at 18:08
  • 1
    @sksallaj: I assumed that, but you have to be careful with using the terminology correctly. There are no such things as "JSON objects". JSON is a language-independent, textual data exchange format. I.e. if you have a JavaScript object and you can convert it to a string containing JSON and vice versa. But you don't convert "JSON objects" to strings. – Felix Kling Mar 12 '14 at 18:50
  • Interesting, I never thought about it that way, I thought javascript associative objects were the same as calling it a json object because they look the same syntactically. Then using JSON.stringify on the "json" object for passing it around as a simple type. All this seems very fuzzy to me. Also, isn't stringify the same as serialization? That's how I thought of it. – sksallaj Mar 12 '14 at 19:00
4

You can use "map":

json2 = json1.map( (address) => {
            return {
                state: address.key1,
                city: address.key2,
                zip: address.key3
            }
        });
Pedro Ferreira
  • 493
  • 7
  • 14
  • 1
    json1.map is not a function. – J Rod Apr 09 '17 at 02:01
  • "The map() method creates a new array with the results of calling a provided function on every element in this array." - [link](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map). Use `var json1 = [{ key1: "Value1", key2: "Value2", key3: "Value3" }];` – Pedro Ferreira Apr 10 '17 at 09:56