2

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.

4
  • This looks like a normal JavaScript object, not a string. Commented Mar 12, 2014 at 18:01
  • ah sorry, I used JSON.stringify on my code.. I'll add it Commented Mar 12, 2014 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 stackoverflow.com/questions/4647817/… Commented Mar 12, 2014 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! Commented Mar 12, 2014 at 18:10

2 Answers 2

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/

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

4 Comments

Given that you are working with objects, the variable names are a bit misleading.
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: 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.
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.
4

You can use "map":

json2 = json1.map( (address) => {
            return {
                state: address.key1,
                city: address.key2,
                zip: address.key3
            }
        });

2 Comments

json1.map is not a function.
"The map() method creates a new array with the results of calling a provided function on every element in this array." - link. Use var json1 = [{ key1: "Value1", key2: "Value2", key3: "Value3" }];

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.