Filter out only the necessary values from JSON data

Why mark a question as duplicate? Is it for points or to be a wiseass? You can't assume a question won't help someone. The answers from the "duplicated" question didn't answer my question, but the person who answered MY question did. -EDIT

I'm trying to extract just one value from JSON data - the 24 hr price percentage change:

{
    "id": "stellar", 
    "name": "Stellar", 
    "symbol": "XLM", 
    "rank": "6", 
    "price_usd": "0.570132", 
    "price_btc": "0.00005009", 
    "24h_volume_usd": "672209000.0", 
    "market_cap_usd": "10187093680.0", 
    "available_supply": "17867956333.0", 
    "total_supply": "103629819514", 
    "max_supply": null, 
    "percent_change_1h": "1.8", 
    "percent_change_24h": "16.65", 
    "percent_change_7d": "23.95", 
    "last_updated": "1516839244"
} 

Currently, my code only displays [object Object]:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
$.getJSON('https://api.coinmarketcap.com/v1/ticker/stellar/',
function (data) {
document.body.append(data);
});
});
</script>

I want to isolate and display only the "percent_change_24h" value for now.

Thank you.

Answer №1

If you want to retrieve a specific value from the data object, you can simply access it using the key percent_change_24h:

var data = {
  "id": "stellar",
  "name": "Stellar",
  "symbol": "XLM",
  "rank": "6",
  "price_usd": "0.570132",
  "price_btc": "0.00005009",
  "24h_volume_usd": "672209000.0",
  "market_cap_usd": "10187093680.0",
  "available_supply": "17867956333.0",
  "total_supply": "103629819514",
  "max_supply": null,
  "percent_change_1h": "1.8",
  "percent_change_24h": "16.65",
  "percent_change_7d": "23.95",
  "last_updated": "1516839244"
};

console.log(data['percent_change_24h']);
document.body.append(data['percent_change_24h']);
// in your case document.body.append(data['percent_change_24h']);

I hope this explanation is helpful to you!

Answer №2

Check out this link for information on Stellar - a cryptocurrency that is currently ranked 6th in the market.

[
    {
        "id": "stellar", 
        "name": "Stellar", 
        "symbol": "XLM", 
        "rank": "6", 
        "price_usd": "0.566242", 
        "price_btc": "0.00004991", 
        "24h_volume_usd": "674523000.0", 
        "market_cap_usd": "10117586651.0", 
        "available_supply": "17867955133.0", 
        "total_supply": "103629819514", 
        "max_supply": null, 
        "percent_change_1h": "-0.26", 
        "percent_change_24h": "16.45", 
        "percent_change_7d": "21.53", 
        "last_updated": "1516840744"
    }
]

If you want to access the percent_change_24h field from the data array shown above, use data[0].percent_change_24h.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
                $.getJSON('https://api.coinmarketcap.com/v1/ticker/stellar/',
                function (data) {
                    document.body.append(data[0].percent_change_24h);
                });
        });
    </script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to incorporate a text box along with a submit button that triggers a callback to a javascript function when clicked, utilizing either plain javascript or jQuery?

As a beginner in the world of JavaScript, I'm struggling to find the solution to this problem. Any assistance would be greatly welcomed. ...

Python sends back a list containing garbled characters to Ajax

Need help fixing the output of a Python list returned to Ajax, as it appears strange. ap.py @app.route('/_get_comUpdate/', methods=['POST']) def _get_comUpdate(): comNr = request.form.get('comNr') com_result ...

Vue: The computed property cannot be set

I'm having trouble with utilizing computed properties and ajax calls in Vue.js. The "filterFactories" variable stores a list of factories. A computed property named "filterFactories" generates this list of factories. Now, I am looking to implement a ...

Error encountered: Unexpected 'o' token in JSON parsing

Although this issue has been discussed numerous times before, I am struggling to identify the error in my code. The error message I am receiving is 'Uncaught SyntaxError: Unexpected token o' Below is the ajax code I am using: $.ajax({ type: ...

Unsubscribe option in the AWS Software Development Kit for Node.js

Is there a way for me to include a List-Unsubscribe : <mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bcdddedffcdfd8da92dfd3d1">[email protected]</a>> header in my outgoing email message while using A ...

storing a value in the browser's local storage

I am in the process of creating a new game that includes a high score feature. The idea is that when the current score surpasses the existing one stored locally, it will be replaced: localStorage.setItem('highScore', highScore); var HighScore = ...

If the checkbox is selected, retrieve the product name and price and store them in an array

If the checkbox is checked, I want to retrieve the service name and its price in an array. To get the values of the selected items (i.e. service name and its price in an array), please explain how I can achieve this. $(document).on('click', &apos ...

Troubleshooting the issue with Angular 4 + Express not initializing on Heroku platform

I'm currently facing challenges while attempting to deploy my Angular 4 application to Heroku. I keep encountering errors such as not being able to find the "ng command" and similar issues. I believe that in order to successfully install the CLI, I n ...

Hiding the C3 tooltip after engaging with it

I'm currently expanding my knowledge on utilizing C3.js for creating charts, and one aspect I'm focusing on is enhancing the tooltip functionality. Typically, C3 tooltips only appear when you hover over data points as demonstrated in this example ...

Implementing a Search Box feature in React-leaflet version 3.1.0

Struggling to incorporate a searchbox feature into my react app. Encountering the error message "Attempted import error: 'MapControl' is not exported from 'react-leaflet'" with the latest version of react-leaflet. import { MapContainer, ...

The display of JSON data is not loading properly

Having some trouble displaying data in my app. Not sure if I'm missing something or making a mistake. Check out my code below. Projects.js: import Res2 from './images/Res/res2.jpg' import Res3 from './images/Res/res3.jpg' import ...

What is the process for deselecting a checkbox?

I am facing a situation where I need to uncheck a checkbox that is always checked based on user input from another section of my form. Specifically, I have implemented an onChange="functionName" event on a select box. Can someone guide me on how to accom ...

Using Typescript with Angular 2 to Implement Google Sign-In on Websites

Currently, I am in the process of creating a website that utilizes a typical RESTful web service to manage persistence and intricate business logic. To consume this service, I am using Angular 2 with components coded in TypeScript. Instead of developing m ...

Using jQuery to Capture Datepicker Input and Assigning to PHP Variable

I need help figuring out how to save a jQuery datepicker value in a php date formatted variable. My goal is to have: input field named "reviewdate" = 03/02/2015 input field named "reviewmonth" = 3 Both values should be stored in a mysql database. I am ...

Creating a callback function within stored procedures using JavaScript Language Integrated Query in documentDB: A step-by-step guide

According to the documentation, the code snippets below are considered equivalent. However, I have observed that in the first case, I am able to perform operations on multiple documents within the callback function, whereas the map function in the latter s ...

Exploring Node.js: Uncovering the Node Path in Windows Operating System

https://i.sstatic.net/zeI3T.jpg Hello there, Attached below is the picture showing the configuration settings for the Ponicode extension, which is used for automating unit tests. I'm currently trying to locate the Node Path for Node.js on my Window ...

Django could not locate the designated URL

Here is the ajax call I am using: function fetchBodyHeights(seats_id) { $('[data-body-length]').html(gettext("Body length")); $('[data-weights]').html(gettext("Weights")); $('[data-wheel-drive]').html(gettext("Whe ...

Modify HTML by replacing two consecutive tags using JavaScript

Currently working on a React project and struggling with a particular issue. My text editor seems to have trouble with certain HTML syntaxes, making the package less than ideal for my needs. The main goal is to convert the following syntax from: <p> ...

CSS Flexibility in Action

Presently, my tab bar has a fixed look as shown here: https://codepen.io/cdemez/pen/WNrQpWp Including properties like width: 400px; etc... Upon inspecting the code, you'll notice that all the dimensions are static :-( Consequently, I am encountering ...

Struggling with properly locating a string within an array

I have been attempting to search an array for a specific string using a For loop, iterating through each index of the array based on its length. The expected behavior is that if the string is found, it should display that string (which in this scenario is ...