Printing parsed JSON - json

So, I've parsed a response like this:
var g_rgListingInfo = JSON.parse( response );
response looks like this
{"321242653847396921":{"listingid":"321242653847396921","price":28338,"fee":4249,"publisher_fee_app":730,"publisher_fee_percent":"0.10000000149011612","currencyid":"2003","steam_fee":1416,"publisher_fee":2833,"asset":{"currency":0,"appid":730,"contextid":"2","id":"3038615825","amount":"1","market_actions":[{"link":"steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M%listingid%A%assetid%D1030942533801731526","name":"Inspect in Game..."}]}},"321242653843485871":{"listingid":"321242653843485871","price":30175,"fee":4525,"publisher_fee_app":730,"publisher_fee_percent":"0.10000000149011612","currencyid":"2003","steam_fee":1508,"publisher_fee":3017,"asset":{"currency":0,"appid":730,"contextid":"2","id":"1730491611","amount":"1","market_actions":[{"link":"steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M%listingid%A%assetid%D1030942533801731526","name":"Inspect in Game..."}]}},"782860982384213986":{"listingid":"782860982384213986","price":31305,"fee":4695,"publisher_fee_app":730,"publisher_fee_percent":"0.10000000149011612","currencyid":"2003","steam_fee":1565,"publisher_fee":3130,"asset":{"currency":0,"appid":730,"contextid":"2","id":"2815962367","amount":"1","market_actions":[{"link":"steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M%listingid%A%assetid%D1030942533801731526","name":"Inspect in Game..."}]}},"783987515556891867":{"listingid":"783987515556891867","price":31305,"fee":4695,"publisher_fee_app":730,"publisher_fee_percent":"0.10000000149011612","currencyid":"2003","steam_fee":1565,"publisher_fee":3130,"asset":{"currency":0,"appid":730,"contextid":"2","id":"3708699202","amount":"1","market_actions":[{"link":"steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M%listingid%A%assetid%D1030942533801731526","name":"Inspect in Game..."}]}},"783987515558623437":{"listingid":"783987515558623437","price":30957,"fee":4642,"publisher_fee_app":730,"publisher_fee_percent":"0.10000000149011612","currencyid":"2003","steam_fee":1547,"publisher_fee":3095,"asset":{"currency":0,"appid":730,"contextid":"2","id":"4462433815","amount":"1","market_actions":[{"link":"steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M%listingid%A%assetid%D1030942533801731526","name":"Inspect in Game..."}]}},"718685320959305952":{"listingid":"718685320959305952","price":34000,"fee":5100,"publisher_fee_app":730,"publisher_fee_percent":"0.10000000149011612","currencyid":"2001","steam_fee":1700,"publisher_fee":3400,"asset":{"currency":0,"appid":730,"contextid":"2","id":"4450043953","amount":"1","market_actions":[{"link":"steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M%listingid%A%assetid%D1030942533801731526","name":"Inspect in Game..."}]}},"796369492002647568":{"listingid":"796369492002647568","price":34500,"fee":5175,"publisher_fee_app":730,"publisher_fee_percent":"0.10000000149011612","currencyid":"2001","steam_fee":1725,"publisher_fee":3450,"asset":{"currency":0,"appid":730,"contextid":"2","id":"4024113558","amount":"1","market_actions":[{"link":"steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M%listingid%A%assetid%D3082226233578562378","name":"Inspect in Game..."}]}},"718684619833530742":{"listingid":"718684619833530742","price":22958,"fee":3442,"publisher_fee_app":730,"publisher_fee_percent":"0.10000000149011612","currencyid":"2002","steam_fee":1147,"publisher_fee":2295,"asset":{"currency":0,"appid":730,"contextid":"2","id":"4331886445","amount":"1","market_actions":[{"link":"steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M%listingid%A%assetid%D1030942533801731526","name":"Inspect in Game..."}]}},"788487401257494747":{"listingid":"788487401257494747","price":34783,"fee":5217,"publisher_fee_app":730,"publisher_fee_percent":"0.10000000149011612","currencyid":"2001","steam_fee":1739,"publisher_fee":3478,"asset":{"currency":0,"appid":730,"contextid":"2","id":"2315637005","amount":"1","market_actions":[{"link":"steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M%listingid%A%assetid%D1030942533801731526","name":"Inspect in Game..."}]}},"321242020664839911":{"listingid":"321242020664839911","price":34783,"fee":5217,"publisher_fee_app":730,"publisher_fee_percent":"0.10000000149011612","currencyid":"2001","steam_fee":1739,"publisher_fee":3478,"asset":{"currency":0,"appid":730,"contextid":"2","id":"4283078084","amount":"1","market_actions":[{"link":"steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M%listingid%A%assetid%D6944696178921031564","name":"Inspect in Game..."}]}}}
I put it in here: http://json.parser.online.fr/ this is the
result
The problem I'm having is that I fail to loop through the items. g_rgListingInfo.length is NaN. I tried to use forEach but that failed too.
I want to loop through all these "321242653847396921", "321242653843485871"... wich are always changing and obtain their listingid, price, fee etc.
I am pretty new to node.js so I'm sorry if this is a stupid question.

You have an object, not an array. So, to iterate the results, you have to either convert your object into an array or just iterate it as object.
Converting into array
Depending on what you need, this could be more convenient:
var myData = Object.keys(g_rgListingInfo).map(Number).sort().map(function (c) {
return g_rgListingInfo[c];
});
// Then you can just use the `myData` array
myData.forEach(function (current, index) {
/* do something */
});
// ...or using for loop
for (var i = 0; i < myData.length; ++i) {
var current = myData[i];
/* do something */
}
Iterating the object
You have to get the keys of the object iterate them (optionally you maybe want to sort them first).
// ["3124...", ...]
var numbers = Object.keys(g_rgListingInfo);
// Optional sort
numbers = numbers.map(Number).sort();
// Iterate the object keys
numbers.forEach(function (currentKey) {
var currentObject = g_rgListingInfo[currentKey];
});

Related

Turn JSON Data Into Int To Perform Calculations

Trying to convert JSON data into int in order to perform calculations, multiply by numbers or percentages (or whichever method is best recommended)
Tried performing calculation on the object (using addition for example), but it only added numbers on to the end of the resulting string. I have seen suggestions on using JSON parse (reviver) but can't seem to get my head round getting the desired data when it is only one specific part of the JSON data required rather than multiple items of data from the JSON link.
var xmlhttp = new XMLHttpRequest();
var url = "https://api.coindesk.com/v1/bpi/currentprice.json";
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var json = JSON.parse(this.responseText);
parseJson(json);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
function parseJson(json) {
var gbpValue = "1 BTC equals to £" + json["bpi"]["GBP"]["rate"];
document.getElementById("data").innerHTML =
gbpValue;
As mentioned, have tried performing calculations on the result but it only adds numbers to the end of the string. Thanks for any advice or help.
What I can see in your code is that you are adding your json variable with a string which would always result in string concatenation in below line:
var gbpValue = "1 BTC equals to £" + json["bpi"]["GBP"]["rate"];
First check your json object if it a number or string.
You could use the Number() or parseInt() functions to convert a string to number.
Example:
var num=Number("23");
or
var num=parseInt("23");
Hope it helps. :)

Confused on how notes = JSON.parse(noteString);

var addnote = function (title, body) {
var notes = [];
var note = {
title: title,
body: body
}
need explanation on the two lines under try
try {
var noteString = fs.readFileSync("data.json");
notes = JSON.parse(noteString);
} catch (e) {
}
And explanation on how duplicateNotes works ..
var duplicateNotes = notes.filter(function(note){
return note.title === title
})
if (duplicateNotes.length === 0) {
notes.push(note);
fs.writeFileSync("data.json", JSON.stringify(notes));
}
}
JSON.parse converts a JSON object to String.
{
field1:field1Value,
field2:fieldValue
}
If this is in Json format you can access it's elements using JSONObjectName.fieldName
But,if it's converted to String it looses it's JSON properties. You can't access fields in same way. Output will act like String.
need explanation on the two lines under try
var noteString = fs.readFileSync("data.json");
There are two kinds of response back asynchronous and synchronous. Synchronous call is like you will not start playing until you get a pass and score a goal. But, Asynchronous call is like you start playing with your friend but, you run near him and, there is a promise you won't shoot until you get the ball.
readFileSync will read the file and the next line will wait until it gets the pass.JSON.parse() will convert file content to JSON object.
And explanation on how duplicateNotes works ..
var duplicateNotes = notes.filter(function(note){
return note.title === title
})
Whenever a match is found , that element in notes will be pushed to duplicate note. That's all.

How to 'merge' multiple objects as one json object while reading data from csv file

I want to convert some csv file into json file in nodejs.
While, some of property in the json will be array. Right now I can read a csv file row by row like this:
{"price":1,"bedrooms":"Bedrooms (All Levels): 4"},
{"price":null,"bedrooms":"Bedrooms (Above Grade): 4"},
{"price":null,"bedrooms":"Master Bedroom: 21x15"},
{"price":null,"bedrooms":"Master Bedroom Bath: Full"},
{"price":null,"bedrooms":"2nd Bedroom: 14x13"},
{"price":null,"bedrooms":"3rd Bedroom: 15x14"},
{"price":null,"bedrooms":"4th Bedroom: 15x12"}
BUT I want to get something like this:
`{"price":1,"bedrooms":["Bedrooms (All Levels): 4","Bedrooms (Above
Grade): 4","Master Bedroom: 21x15","Master Bedroom Bath: Full","2nd
Bedroom: 14x13","3rd Bedroom: 15x14","4th Bedroom: 15x12"]}`
Can someone point out some ways? I tried things like fast-csv,csv-parse,ect. But couldn't merge(push or append) the values of the same field into one field as an array.
Thanks.
the code I finished right now:
var fs = require('fs');
var csv = require('fast-csv');
var stream = fs.createReadStream("../../HouseDataDev01.csv");
csv
.fromStream(stream, {columns:true, ignoreEmpty:true, headers :
["price","bedrooms"]})
.on("data", function(data){
// console.log(data);
})
.on("end", function(){
console.log("done");
});
==========
I came up with an idea that maybe I can create an object
var NewHouse = require('../models/NewHouse.js');
//NewHouse is a schema I created before to store the csv data
var test = new NewHouse;
So that I can use the test object something like this:
.on("data", function(data){
for(i in test){
test.i.push(data[index];
}
But I found there are many other properties in test like:$__reset, $__dirty, $__setSchema
How could I write this loop?
Ok, let me explain this...
The main point in my solution is to create some thing like headtag and fieldname{}to record the stream from fs which read csv row by row. I use the headtag to validate the round of the streaming rows. For example for the first round, I need the row's value to be the key of every object in my final json file. If I set a header in fromStream() method, each round's result will conatin the header, I dont know how to 'merge' them, so I chose this 'tricky' way.
Then, as in my final json file, some of(not all of) field will be array. So when I read a second value which is not an empty string "", I should convert the field into an array.usingreadResult[fieldnames[i]]=new Array( readResult[fieldnames[i]]);.
here is the code:
//create a fime stream
var stream = fs.createReadStream(csvfilepath);
//as the file will be read row by row, headtag is pointer to row.
//e.g: headtag = 5, means the stream is reading the 5th row of the csv file
var headtag=0;
//the final stream read result, will be the same format in schema.
var readResult={};
//fieldname records the headers key name.
var fieldnames={};
csv.fromStream(stream,{ignoreEmpty:true})
.on("data", function(data){
if(headtag === 0){
//I assume the first row is the headers,so should make sure the headers' names are the same in your schema
fieldnames = data;
for(var i=+0; i<data.length; i++){
readResult["data[i]"] = {};
}
}
if(headtag === 1){
//some of fields may only conatins one value, so save them as a String
for(var i=+0; i<data.length; i++){
readResult[fieldnames[i]] = data[i];
}
}
if(headtag === 2){
for(var i=+0 ; i<data.length; i++){
//for those field that may contains multiple values, convert them as an array, then push all values in it.
if(data[i]!==""){
readResult[fieldnames[i]]=new Array( readResult[fieldnames[i]]);
readResult[fieldnames[i]].push(data[i]);
}
}
}
if(headtag > 2){
for(var i=+0 ; i<data.length; i++){
if(data[i]!==""){
readResult[fieldnames[i]].push(data[i]);
}
}
}
headtag=headtag+1;
})
.on("end",function(){
readResult.images = images;
//create a time tag
var startdate = moment().format('MM/DD/YYYY');
var starttime = moment().format('H:mm:ss');
readResult.save_date=startdate;
readResult.save_time=starttime;
//save the data in mongodb
NewHouse.create(readResult, function(error, house){
if(error){
console.log(error);
}
else{
console.log("successfully create a document in your mongodb collection!");
}
});
});
On the basis of this question, I updated my code. Now you can read both csv file and images together and save them to mongodb.
for more information check here:
https://github.com/LarryZhao0616/csv_to_json_converter

Transforming JSON

I am trying to transform JSON data provided by Quandl into a custom JSON format so that I can load it into my database.
The JSON Array is stock market data with Date, High, Low, Open, Close values. I need a flat JSON instead of an array.
I tried the following, but it returns full array instead of individual element. If I use [0][1], [0][2], it returns empty values.
Here is my code
var DataTransform = require("node-json-transform").DataTransform
var myData = {"dataset":{"data":[["2016-01-15",292.5,294.4,267.1,279.9,273.0,64104.0,182.09],["2016-01-14",288.0,302.0,265.0,287.6,288.2,68271.0,199.82],["2016-01-13",303.95,307.65,275.0,290.1,292.75,99921.0,293.08]]}}
var map = {
list : 'dataset.data',
item: {
date: [0],
high: [0][1],
low: [0][0][1]
}
};
var dataTransform = DataTransform(myData, map);
var result = dataTransform.transform();
console.log(result);
======================================================================
Output
[{"date":[["2016-01-15",292.5,294.4,267.1,279.9,273,64104,182.09]],"high":"","low":""},{"date":[["2016-01-14",288,302,265,287.6,288.2,68271,199.82]],"high":"","low":""},{"date":[["2016-01-13",303.95,307.65,275,290.1,292.75,99921,293.08]],"high":"","low":""}]
You should use the built-in Array functions for transforming data. e.g. map, reduce, filter, sort, etc. In this case map will do the job perfectly. e.g.
var ds = {"dataset":{"data":[["2016-01-15",292.5,294.4,267.1,279.9,273.0,64104.0,182.09],["2016-01-14",288.0,302.0,265.0,287.6,288.2,68271.0,199.82],["2016-01-13",303.95,307.65,275.0,290.1,292.75,99921.0,293.08]]}}
var transformed = ds.dataset.data
.map(function (d) {
return {
date : d[0],
high : d[1],
low : d[2],
etc:
}
})
// output in format : [{date:"2016-01-15",high:"294.4",low:"267.1"},...]

Query a JSON list of dict

[{"time":136803,"price":"1.4545","amount":"0.0885","ID":"112969"},
{"time":136804,"price":"2.5448","amount":"0.0568","ID":"5468489"},
{"time":136805,"price":"1.8948","amount":"0.0478","ID":"898489"}]
I have a large JSON file like the one above. It is a list of dictionaries. I want to choose a time and find the value assoaciated with that time. I will not know where in my list the time is located only the value for the time. Is there a way I can say, for time 136804, make x = to price? Or should I loop through each value? I also want to use this value (x) in a mathematical function.
My fist idea is to use brute force by going through each item and checking it for a matching time value in a loop.
Is this the best way?
Take a look at SpahQL http://danski.github.io/spahql/ which we use to query JSON in order to select values and subsequently change them as required.
I did something similar to this recently. JSON file I had to query had around 6000 lines and around 500 JSON objects. My query function given below loops through the each object to select the matching objects, but it can fetch any result within few milliseconds.
var data = '[{"time":136803,"price":"1.4545","amount":"0.0885","ID":"112969"},'+ '{"time":136804,"price":"2.5448","amount":"0.0568","ID":"5468489"},'+ '{"time":136805,"price":"1.8948","amount":"0.0478","ID":"898489"}]';
var data = JSON.parse(data);
var query = function(data, select, andwhere) {
var return_array = [];
$.each(data, function (i, obj) {
var temp_obj = {};
var where = true;
if (andwhere) {
$.each(andwhere, function(j, wh) {
if (obj[wh.col] !== wh.val) {
where = false;
}
});
}
if (where === false) {
return;
}
$.each(obj, function (j, elem) {
if (select.indexOf(j.trim())!==-1) {
temp_obj[j] = elem;
}
});
return_array.push(temp_obj);
});
return return_array;
};
var result = query(data, ['price','amount'],[{"col":"time","val":136804}]);
console.log(JSON.stringify(result));
http://jsfiddle.net/bejgy3sn/1/