0

So I am practising some nodejs and this time I am playing around with steam api and json objects. But I am having some problems.

So, from the Steam api,

http://steamcommunity.com/profiles/<steamid>/inventory/json/730/2

I got the json from this code,

var request = require('request');

var url = "http://steamcommunity.com/profiles/<steamid>/inventory/json/730/2"

request({
    url: url,
    json: true
}, function (error, response, body) {
     if (!error && response.statusCode === 200) {
        var json = JSON.parse(body);
        console.log(body) 
     }
});

And the json looks lite this, json-link

From the json I want to take out the classid and instanceid from each of the items, but there comes the problem. I don't know how. I know I need to parse it but nothing more unfortunately.

Would be very helpful if someone could explain how, or link a guide/tutorial so I can learn.

Thanks!

EDIT:

    var request = require('request');
var _ = require('lodash');

var url = "http://steamcommunity.com/profiles/76561198007691048/inventory/json/730/2";


request({
    url: url,
    json: true
}, function jsonParse(error, response, data) {
    console.log(data.rgDescriptions);
    var item = getItems(data.rgDescriptions);
    console.log(item);

}

);

function getItems(data){
    var item = data;
    if(!item){
        return "error";

    }
    return _(item).keys().map(function(id){
        return _.pick([id], "name");}).value();

Console give me this; [ {}, {}, {}, {}, {}, {}, {}, {}, {},.... ]

JSON look like this;

'1293508920_0': 
   { appid: '730',
     classid: '1293508920',
     instanceid: '0',
     icon_url: '-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXU5A1PIYQNqhpOSV-fRPasw8rsUFJ5KBFZv668FF4u1qubIW4Su4mzxYHbzqGtZ-KGlz8EuJcg3rnE9NiijVe3_UY-Zzr2JJjVLFEEeiQRtg',
     icon_drag_url: '',
     name: 'Shadow Case',
     market_hash_name: 'Shadow Case',
     market_name: 'Shadow Case',
     name_color: 'D2D2D2',
     background_color: '',
     type: 'Base Grade Container',
     tradable: 1,
     marketable: 1,
     commodity: 1,
     market_tradable_restriction: '7',
      },
  '1644880589_236997301': 
   { appid: '730',
     classid: '1644880589',
     instanceid: '236997301',
     icon_url: '-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXU5A1PIYQNqhpOSV-fRPasw8rsUFJ4MAlVo6n3e1Y27OPafjBN09izq42ChfbzNvXTlGkD6p0lj7_FpNjx0VDj_UBoZ272cNfBdg48MAyB-VS3xum61Me_ot2XnqkB5QYc',
     icon_drag_url: '',
     name: 'MLG Columbus 2016 Mirage Souvenir Package',
     market_hash_name: 'MLG Columbus 2016 Mirage Souvenir Package',
     market_name: 'MLG Columbus 2016 Mirage Souvenir Package',
     name_color: 'D2D2D2',
     background_color: '',
     type: 'Base Grade Container',
     tradable: 1,
     marketable: 1,
     commodity: 0,
     market_tradable_restriction: '7',
      },
DavidS
  • 11
  • 6
  • `JSON.parse(body)` does return an JavaScript Object (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object). To access any of the properties have a look at this and see if it helps: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects – puelo Apr 06 '16 at 21:37

1 Answers1

0

To work with complex objects or collections you can use lodash library.

So, you have json with the following format:

{
  "success":true,
  "rgInventory": {
    "5719625206": {"id":"5719625206","classid":"1651313004","instanceid":"188530139","amount":"1","pos":1},
    "5719034454": {"id":"5719034454","classid":"1649582636","instanceid":"188530139","amount":"1","pos":2},
    "5718628709": {"id":"5718628709","classid":"1649582636","instanceid":"188530139","amount":"1","pos":3},
    ...
  }
}

To extract array of required items, first of all install lodash library in your project: npm i -S, after that add this code into your file:

var _ = require('lodash');

function extractItems(data) {
  var rgInventory = _.get(data, 'rgInventory');
  if (!rgInventory) {
    return [];
  }
  return _(rgInventory)
    .keys()
    .map(function(id) {
      return _.pick(rgInventory[id], ['classid', 'instanceid']);
    })
    .value();
}
alexmac
  • 19,087
  • 7
  • 58
  • 69
  • Thansk for the help but unfortunately I still don't understand to 100%, I understand the code you shared but I don't undestand what to do with it, how to apply it to an object and then use it. Where should I put the code and how to get the data to my object ? – DavidS Apr 07 '16 at 17:20
  • Hmm, all what you need it's just call my function `extractItems` from your code. Place that call below the line where you parse json: `var items = extractItems(json)`; – alexmac Apr 07 '16 at 17:37
  • I guess I have too little knowledge to do this. I will keep on going with something else. Thanks for the help anyway ! – DavidS Apr 07 '16 at 17:46
  • Hey! I have read around a bit and decided to test this again. Here is my code now, https://gyazo.com/db298c2d699ca3a740a8390f0a3e94c7, but im getting error from your function. What did I wrong ? – DavidS Apr 11 '16 at 21:03
  • Bug in your code, you need to convert response body to object: `data = JSON.parse(data);` – alexmac Apr 11 '16 at 21:15
  • Everytime I am parsing im getting error, undifined 1, unexpected token o, here is my code now, https://gyazo.com/134173fe13e9de184690d2e8aa689f6f – DavidS Apr 12 '16 at 16:13
  • Are you sure that you're getting the correct json from the server in `request` call? If json is invalid, it can't be converted to object and `JSON.parse` throw an error. – alexmac Apr 12 '16 at 16:16
  • Could you try this url and see if it works for you ? "http://steamcommunity.com/profiles/76561198007691048/inventory/json/730/2" – DavidS Apr 12 '16 at 16:23
  • When I'm trying parse this json I'm getting: `SyntaxError: JSON.parse: bad control character in string literal at line 1 column 32575 of the JSON data`. – alexmac Apr 12 '16 at 16:30
  • Updated in main posts below edit, should the console look like that or im I doing something wrong? – DavidS Apr 12 '16 at 19:22