Home > Software design >  Extract and convert from JSON by Regex
Extract and convert from JSON by Regex

Time:05-11

Good afternoon, it is necessary to extract the data inside the token_dict object from the data received by the api and convert the data, example:

"token_dict": {
  "0x13a637026df26f846d55acc52775377717345c06": {
    "chain": "bsc",
    "decimals": 18,
    "display_symbol": null,
    "id": "0x13a637026df26f846d55acc52775377717345c06",
    "is_core": true,
    "is_verified": true,
    "is_wallet": true,
    "logo_url": "https://static.debank.com/image/bsc_token/logo_url/0x13a637026df26f846d55acc52775377717345c06/8dfc8adee7cc793678ec6d572b4d7e43.png",
    "name": "SpaceY Token",
    "optimized_symbol": "SPAY",
    "price": 1.0733528304908733,
    "protocol_id": "",
    "symbol": "SPAY",
    "time_at": 1621572439
  },
  "0xd9ec19fe88140ac0a3ab5517f8e54a2143599f80": {
    "chain": "bsc",
    "decimals": 18,
    "display_symbol": null,
    "id": "0xd9ec19fe88140ac0a3ab5517f8e54a2143599f80",
    "is_core": null,
    "is_verified": null,
    "is_wallet": false,
    "logo_url": null,
    "name": "Phenomenal Protocol PNM Token",
    "optimized_symbol": "PNM",
    "price": 0,
    "protocol_id": "",
    "symbol": "PNM",
    "time_at": 1651860868
  }
}

Convert to this form:

"token_dict": [
  {
    "chain": "bsc",
    "decimals": 18,
    "display_symbol": null,
    "id": "0x13a637026df26f846d55acc52775377717345c06",
    "is_core": true,
    "is_verified": true,
    "is_wallet": true,
    "logo_url": "https://static.debank.com/image/bsc_token/logo_url/0x13a637026df26f846d55acc52775377717345c06/8dfc8adee7cc793678ec6d572b4d7e43.png",
    "name": "SpaceY Token",
    "optimized_symbol": "SPAY",
    "price": 1.0733528304908733,
    "protocol_id": "",
    "symbol": "SPAY",
    "time_at": 1621572439
  },
 {
    "chain": "bsc",
    "decimals": 18,
    "display_symbol": null,
    "id": "0xd9ec19fe88140ac0a3ab5517f8e54a2143599f80",
    "is_core": null,
    "is_verified": null,
    "is_wallet": false,
    "logo_url": null,
    "name": "Phenomenal Protocol PNM Token",
    "optimized_symbol": "PNM",
    "price": 0,
    "protocol_id": "",
    "symbol": "PNM",
    "time_at": 1651860868
  }
]

That is, the data that needs to be received as a list at the output, and since I work on the Bubble platform, there are some difficulties with this.

CodePudding user response:

Firstly, note that Regex is very far from the best solution to this issue.

A better approach to achieve what you require is to use Object.keys() to retrieve a list of all the unique property names within token_dict and then use map() to create an array of their associated object values:

Object.keys(data.token_dict).map(k => data.token_dict[k]);

Here's a working example of this:

var data = {token_dict:{"0x13a637026df26f846d55acc52775377717345c06":{chain:"bsc",decimals:18,display_symbol:null,id:"0x13a637026df26f846d55acc52775377717345c06",is_core:!0,is_verified:!0,is_wallet:!0,logo_url:"https://static.debank.com/image/bsc_token/logo_url/0x13a637026df26f846d55acc52775377717345c06/8dfc8adee7cc793678ec6d572b4d7e43.png",name:"SpaceY Token",optimized_symbol:"SPAY",price:1.0733528304908733,protocol_id:"",symbol:"SPAY",time_at:1621572439},"0xd9ec19fe88140ac0a3ab5517f8e54a2143599f80":{chain:"bsc",decimals:18,display_symbol:null,id:"0xd9ec19fe88140ac0a3ab5517f8e54a2143599f80",is_core:null,is_verified:null,is_wallet:!1,logo_url:null,name:"Phenomenal Protocol PNM Token",optimized_symbol:"PNM",price:0,protocol_id:"",symbol:"PNM",time_at:1651860868}}};

var output = Object.keys(data.token_dict).map(k => data.token_dict[k]);
console.log(output);

Note that this is assuming that you do not have the ability to correct the response data format from your API on the server side. That would by far be the best approach to solving your problem.

CodePudding user response:

a simple map function would sufice

   const data = { "token_dict": {
      "0x13a637026df26f846d55acc52775377717345c06": {
        "chain": "bsc",
        "decimals": 18,
        "display_symbol": null,
        "id": "0x13a637026df26f846d55acc52775377717345c06",
        "is_core": true,
        "is_verified": true,
        "is_wallet": true,
        "logo_url": "https://static.debank.com/image/bsc_token/logo_url/0x13a637026df26f846d55acc52775377717345c06/8dfc8adee7cc793678ec6d572b4d7e43.png",
        "name": "SpaceY Token",
        "optimized_symbol": "SPAY",
        "price": 1.0733528304908733,
        "protocol_id": "",
        "symbol": "SPAY",
        "time_at": 1621572439
      },
      "0xd9ec19fe88140ac0a3ab5517f8e54a2143599f80": {
        "chain": "bsc",
        "decimals": 18,
        "display_symbol": null,
        "id": "0xd9ec19fe88140ac0a3ab5517f8e54a2143599f80",
        "is_core": null,
        "is_verified": null,
        "is_wallet": false,
        "logo_url": null,
        "name": "Phenomenal Protocol PNM Token",
        "optimized_symbol": "PNM",
        "price": 0,
        "protocol_id": "",
        "symbol": "PNM",
        "time_at": 1651860868
      }
    }
   }
   const formated_data = {
 "token_dict" : Object.values(a.token_dict).map(token=> token})
}
  • Related