Extract the String data from a JSON file

valeurs_d = "";
                for (var i = 0; i < keys.length -1 ; i++)
                    valeurs_d += + event[keys[i]] + ", ";
                var str5 = ",";
                var str6 = str5.concat(valeurs_d);
                var valeurs = str6.substring (0, str6.length - 2);
                console.log(valeurs);

I am working with a JSON document structured like this:

 {
              "temperature" : 12.45,
              "lala" : 45.75,
              "humidite" : 45.78,
              "toto" : "gvvgvh",
              "topic" : "sk1_001/data"
             }

However, the issue arises when attempting to retrieve the values as I get: 12.45, 45.75, 45.78, NaN!

Is there a way to retrieve my string instead of NaN?

On another note, does typeof in Javascript return number or string? I want to specify varchar for strings and double for numbers.

This is essential for updating my Apache Cassandra database using node.js.

How can I achieve returning varchar and double accurately?

Answer №1

It seems like you are looking to extract all the values from your object. Here is a simple solution for that:

const obj = {
  "temperature": 12.45,
  "lala": 45.75,
  "humidite": 45.78,
  "toto": "gvvgvh",
  "topic": "sk1_001/data"
};

const values = Object.keys(obj).map(key => obj[key]);

console.log(values);

This code will store all the values in an array, but if you prefer a string format, you can use values.join(",").

Answer №2

To concatenate all values within a string using commas as separators, implement the code snippet below

var myObject = {
 "temperature" : 24.32,
 "sample" : 34.56,
 "humidity" : 67.89,
 "test" : "abcde",
 "title" : "sk2_002/info"
 };


var newArray = []; 
for(var key in myObject){
  newArray.push(myObject[key].toString());
}
var finalResult = newArray.join(",");
console.log(finalResult);

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

JavaScript is unable to execute anything that comes after the initial code, specifically document.get

<section class="blogSingle"> <div class="container"> <div class="row"> <div class="blogSingle-left col-md-8"> <div id="content3" class="blogSingleContent"></div> </div> <div class ...

What is the best way to combine a collection of strings and HTML elements in a React application?

I am facing a challenge with my list of names. I typically use .join to add commas between each name, but one specific item in the list needs to display an icon of a star next to it based on a certain condition. The issue is that using join turns everyth ...

Encoding a string in JSON that contains the "#" symbol along with other special characters

The client side javascript code I have is as follows: <html> <script type="text/javascript" src="js/jquery.min.js"></script> <script> $(document).ready(function() { //var parameters = "a=" + JSON.stringify( ...

What steps are necessary to configure .eslintrc to identify the use of 'require'?

I recently started using ESLint and successfully integrated it with IntelliJ. Initially, ESLint did not recognize node out of the box. After consulting the documentation, I created a configuration file named .eslintrc at the project's root folder, sp ...

Having trouble updating the icon on my website using FontAwsome

Just a heads up - I'm not familiar with HTML/CSS/JS. This template is pre-made, and I'm simply making some adjustments to it. Hello, I'm currently working on my portfolio website and I want to display my projects based on the programming la ...

Guide on extracting Nolhmann JSON objects and converting them into individual strings

I'm in the process of converting my json object, which contains data from a .json file, into strings. I am utilizing the nolhmann json dictionary library for this task. Below is the content of the .json file: "dictionary": [ {"word": "MEAGRE", "d ...

Ensure that clicking on an element closes any currently visible elements before opening a new element

Take a look at my code snippet below. I am working on creating multiple clickable divs that reveal different content when clicked. However, the issue I am facing is that currently, both content blocks can be displayed simultaneously. My goal is to have onl ...

Can you explain the process of extracting images from JSON data using AJAX and jQuery?

Hello, I'm looking for guidance on incorporating jquery with AJAX to fetch images from a JSON file and showcase them on my website. Below is the code snippet I have put together. function bookSearch(){ var search = document.getElementById('sea ...

Leveraging AngularJS $filter in conjunction with ng-disabled

I have a variable in my $scope that contains information about an election, including a list of voters with unique IDs: $scope.election = { voters: [ { _id: '123' }, { _id: '456' }, { _id: '789' } ] } Additio ...

What is the process for creating static pages that can access local data within a NextJS 13 application?

I recently completed a blog tutorial and I must say, it works like a charm. It's able to generate dynamic pages from .md blog posts stored locally, creating a beautiful output. However, I've hit a roadblock while attempting what seems like a sim ...

Deleting the previous ion-view from DOM in Ionic v1 with Angular

I have been working on modifying an app built in Ionic v1. Within the app, there is a primary View titled 'Client's Profile' that contains links to two other Views: Support Request and Complaints. In the Support Request view, there is a Te ...

Can React components receive props or data when they are inserted into regular HTML code?

I have a project where I need to make updates to an old-fashioned website. The current layout is table-based and coded by hand. My idea to streamline the process is to use React to minimize repetitive coding tasks. Specifically, I want to loop through an a ...

What causes the other array to be affected when one is changed?

Take a look at this snippet of JavaScript code: var x = [1, 2, 3], y = x; y[1] = 3; x; // x === [1, 3, 3] Why does this happen? Why is the value of "x" changing when I update "y[1]"? I tried it in both Firefox and Chrome and got the same result. Th ...

What is the best way to combine a list of jsonNodes into a single jsonNode?

I've been working with the Jackson library in Java and I have a collection of objects that serve as wrappers for jsonNodes. I need to convert this list of jsonNodes into a single jsonNode. I attempted the following method: public JsonNode converte ...

creating a distinct angular service for every controller

Lately, I've been utilizing Angular services to store my commonly used codes that are required across different parts of my project. However, I have encountered an issue where variables in the service are shared between controllers. This means that if ...

Creating interactive functionality for textareas and input fields in Vue: A beginner's guide

I need assistance with creating a function where changing the input field will also automatically update the textarea's value. I have successfully implemented Vue js for updating the input field based on the textarea, but I am struggling to reverse th ...

Unable to show the name of the chosen file

After customizing the input [type = file], I successfully transformed it into a button with a vibrant green background. Here is the code snippet that enabled this transformation: <style> #file { height:0px; opacity:0; } ...

What is the best method for incorporating a JSON framework into a project

I've recently obtained the JSON framework DMG file from http://code.google.com/p/json-framework/downloads/list. I then proceeded to follow Option 3 outlined here: http://code.google.com/p/json-framework/wiki/InstallationInstructions. Initially, I set ...

Ways to update the text alongside a slider form with JavaScript

I am currently working on a project using html and js function slide(){ let v= document.getElementById("slide_inner"); document.getElementById("slider").textContent=v.value; } <form id="slider"> <label for="slide_inner"&g ...

Why won't my AngularJS checkbox stay checked?

In my application, I have the following code: <input type="checkbox" ng-checked="vm.eduToEdit.test" /> {{vm.eduToEdit.test}} <input type="checkbox" ng-model="vm.eduToEdit.test"> The value of vm.eduToEdit.test is displaying t ...