0

I currently have JSON like the following (from the Australia Post developer guide):

"PickupDetails_v1.Header": 
{ "Common_v1.UserName": "testuser", 
"Common_v1.AccessKey": "xxxxxxxxx", 
"Common_v1.CustomerID": 123 }

I'd like to know what the best approach is to being able to have an object in JS without the namespaces (e.g. PickupDetails_v1) when retrieving data from the API and vice versa when sending data. Basically I'd like to be able to know the best way to be able to customize JSON property names in Angular.

For example:

var pickupDetails = { UserName: "testuser"; AccessKey: "xxxxxxx" }; 
$http.post(pickupDetails, "http://www.example.com/postageDetails"); // Should send the pickup details with the namespaces).

var storedPickupDetails = $http.get("http://www.example.com/postageDetails"); // Should have the same properties as pickupDetails.
4
  • i'm not sure what are you asking for... can you give more clarification? Commented Jul 14, 2014 at 7:27
  • Means you want only the content within "PickupDetails_v1.Header" Commented Jul 14, 2014 at 7:29
  • This might be helpful: stackoverflow.com/questions/4647817/… Commented Jul 14, 2014 at 7:35
  • Basically I want the property names in JavaScript to be like this: var pickupDetails = { "PickupDetails": { "UserName": "testUser" }}, etc Commented Jul 14, 2014 at 12:32

1 Answer 1

1

I would do this with interceptors.

The below example is taken from the documentation for $http and modified slightly for brevity.

$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
  return {
    // optional method
    'request': function(config) {
      // do something on success
      return config;
    },

    // optional method
    'response': function(response) {
      // do something on success
      return response;
    },
  };
});

$httpProvider.interceptors.push('myHttpInterceptor');

The config and response objects will contain all data about requests and responses and you could hook into the data property of those objects and change the keys to add/remove the namespace

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

Comments

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.