Home > OS >  override JSON response using javascript function
override JSON response using javascript function

Time:08-04

I am trying to change the value of JSON response, i am assuming that this response is dynamic and i want to change only the value of this property bpi.USD.description and i want to change the value from United States Dollar to US Dollar

Test URL: https://api.coindesk.com/v1/bpi/currentprice.json

Response:

{
  "time": {
    "updated": "Aug 3, 2022 20:25:00 UTC",
    "updatedISO": "2022-08-03T20:25:00 00:00",
    "updateduk": "Aug 3, 2022 at 21:25 BST"
  },
  "disclaimer": "This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org",
  "chartName": "Bitcoin",
  "bpi": {
    "USD": {
      "code": "USD",
      "symbol": "$",
      "rate": "23,342.0112",
      "description": "United States Dollar",
      "rate_float": 23342.0112
    },
    "GBP": {
      "code": "GBP",
      "symbol": "£",
      "rate": "19,504.3978",
      "description": "British Pound Sterling",
      "rate_float": 19504.3978
    },
    "EUR": {
      "code": "EUR",
      "symbol": "€",
      "rate": "22,738.5269",
      "description": "Euro",
      "rate_float": 22738.5269
    }
  }
}

And I am expecting this response:

=========================

{
  "time": {
    "updated": "Aug 3, 2022 20:25:00 UTC",
    "updatedISO": "2022-08-03T20:25:00 00:00",
    "updateduk": "Aug 3, 2022 at 21:25 BST"
  },
  "disclaimer": "This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org",
  "chartName": "Bitcoin",
  "bpi": {
    "USD": {
      "code": "USD",
      "symbol": "$",
      "rate": "23,342.0112",
      "description": "US Dollar",
      "rate_float": 23342.0112
    },
    "GBP": {
      "code": "GBP",
      "symbol": "£",
      "rate": "19,504.3978",
      "description": "British Pound Sterling",
      "rate_float": 19504.3978
    },
    "EUR": {
      "code": "EUR",
      "symbol": "€",
      "rate": "22,738.5269",
      "description": "Euro",
      "rate_float": 22738.5269
    }
  }
}

I tried this but it didn't work:

function modifyResponse(args) {
  const {method, url, response, responseType, requestHeaders, requestData, responseJSON} = args;
      let jsonStr;
      let jsonObj = JSON.parse(jsonStr);
      jsonObj.bpi.USD.description = 'US Dollar';
}

CodePudding user response:

If you use JSON.Parse() you can convert it to an object, and change the properties you want.

let jsonStr; // this is just a stand in for the JSON string from https://api.coindesk.com/v1/bpi/currentprice.json 

let jsonObj = JSON.parse(jsonStr);
jsonObj.bpi.USD.description = 'US Dollar';

CodePudding user response:

Easy enough, hope this helps

you can perform this doing the following:

// assuming the response has been parsed
    const responseJson = {
      "time": {
        "updated": "Aug 3, 2022 20:25:00 UTC",
        "updatedISO": "2022-08-03T20:25:00 00:00",
        "updateduk": "Aug 3, 2022 at 21:25 BST"
      },
      "disclaimer": "This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org",
      "chartName": "Bitcoin",
      "bpi": {
        "USD": {
          "code": "USD",
          "symbol": "$",
          "rate": "23,342.0112",
          "description": "United States Dollar",
          "rate_float": 23342.0112
        },
        "GBP": {
          "code": "GBP",
          "symbol": "£",
          "rate": "19,504.3978",
          "description": "British Pound Sterling",
          "rate_float": 19504.3978
        },
        "EUR": {
          "code": "EUR",
          "symbol": "€",
          "rate": "22,738.5269",
          "description": "Euro",
          "rate_float": 22738.5269
        }
      }
    };
    // already parsed
    console.log(responseJson.bpi.USD);

    // assuming the response is a string dataType
    const responseJsonString = JSON.stringify({
      "time": {
        "updated": "Aug 3, 2022 20:25:00 UTC",
        "updatedISO": "2022-08-03T20:25:00 00:00",
        "updateduk": "Aug 3, 2022 at 21:25 BST"
      },
      "disclaimer": "This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org",
      "chartName": "Bitcoin",
      "bpi": {
        "USD": {
          "code": "USD",
          "symbol": "$",
          "rate": "23,342.0112",
          "description": "United States Dollar",
          "rate_float": 23342.0112
        },
        "GBP": {
          "code": "GBP",
          "symbol": "£",
          "rate": "19,504.3978",
          "description": "British Pound Sterling",
          "rate_float": 19504.3978
        },
        "EUR": {
          "code": "EUR",
          "symbol": "€",
          "rate": "22,738.5269",
          "description": "Euro",
          "rate_float": 22738.5269
        }
      }
    });
    const parsedJsonResponse = JSON.parse(responseJsonString);
    parsedJsonResponse.bpi.USD.description = "US Dollar";
    console.log(parsedJsonResponse.bpi.USD);

CodePudding user response:

one way to do it with pure js is like this:

const jsonData = {"time":{"updated":"Aug 3, 2022 20:39:00 UTC","updatedISO":"2022-08-03T20:39:00 00:00","updateduk":"Aug 3, 2022 at 21:39 BST"},"disclaimer":"This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org","chartName":"Bitcoin","bpi":{"USD":{"code":"USD","symbol":"$","rate":"23,276.4380","description":"United States Dollar","rate_float":23276.438},"GBP":{"code":"GBP","symbol":"£","rate":"19,449.6054","description":"British Pound Sterling","rate_float":19449.6054},"EUR":{"code":"EUR","symbol":"€","rate":"22,674.6490","description":"Euro","rate_float":22674.649}}}

const checkIfIsJson = (jsonData) => {
  try{
    return JSON.parse(jsonData);
  }catch (e){
    return jsonData;
  }
}

const mutateJsonData = (jsonData) => {
  const formatedJson = checkIfIsJson(jsonData);
  if(formatedJson !== undefined && formatedJson.bpi !== undefined && formatedJson.bpi.USD !== undefined && formatedJson.bpi.USD.description !== undefined){
    formatedJson.bpi.USD.description = 'US Dollar';
  }
  return formatedJson;
}
console.log(mutateJsonData(jsonData));

The CheckifIsJson function will check if the json you pass it is really JSON format, if not it will pass it to JSON.

If you have the possibility to use the optional chaining operator read here it will simplify the if like this

const jsonData = {"time":{"updated":"Aug 3, 2022 20:39:00 UTC","updatedISO":"2022-08-03T20:39:00 00:00","updateduk":"Aug 3, 2022 at 21:39 BST"},"disclaimer":"This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org","chartName":"Bitcoin","bpi":{"USD":{"code":"USD","symbol":"$","rate":"23,276.4380","description":"United States Dollar","rate_float":23276.438},"GBP":{"code":"GBP","symbol":"£","rate":"19,449.6054","description":"British Pound Sterling","rate_float":19449.6054},"EUR":{"code":"EUR","symbol":"€","rate":"22,674.6490","description":"Euro","rate_float":22674.649}}}

const checkIfIsJson = (jsonData) => {
  try{
    return JSON.parse(jsonData);
  }catch (e){
    return jsonData;
  }
}

const mutateJsonData = (jsonData) => {
  const formatedJson = checkIfIsJson(jsonData);
  if(formatedJson?.bpi?.USD?.description){
    formatedJson.bpi.USD.description = 'US Dollar';
  }
  return formatedJson;
}
console.log(mutateJsonData(jsonData));
  • Related