1

I have a json, viewable at https://i.stack.imgur.com/lxjiM.jpg or here https://dweet.io/get/dweets/for/shyam__5

In python, I am able to print the yearlyWatts by doing:

print(collection[1]['content']['yearlyWatts'])

where collection is the json, done by:

collection = (dweepy.get_dweets_for('shyam__5'))

I am trying to do the same thing in Javascript. Currently, I have done:

getCryptoCurrencyInfo(5)
    .then(currencyInfo => {
        console.log(currencyInfo[1].yearlyWatts)

This does not work, I get no output.

Please do not pay attention to the function getCryptoCurrencyInfo, I would really appreciate if someone could tell me what to write in the console.log(HERE) in order to output the yearly watts of 111255.51

Any help would be appreciated. Thanks!

3 Answers3

1

Suppose you want a single yearlyWatt.

const data = {
    "this": "succeeded",
    "by": "getting",
    "the": "dweets",
    "with": [{
            "thing": "shyam__5",
            "created": "2020-07-03T08:38:01.184Z",
            "content": {
                "test": "test"
            }
        },
        {
            "thing": "shyam__5",
            "created": "2020-07-03T08:37:58.068Z",
            "content": {
                "yearlyWatts": 111429.4
            }
        }
    ]
}

console.log(data.with[1].content.yearlyWatts)
xMayank
  • 1,875
  • 2
  • 5
  • 19
  • If not done yest use 'JSON.parse()' to convert you json object to js object – Julien Maret Jul 03 '20 at 09:00
  • Hello @xMayank, my current code is this : [pastebin](https://pastebin.com/0XbiQe24). I am new to Javascript, could you please help me in how to incorporate the solution into my current code? – Shyam BHAGAT Jul 03 '20 at 09:12
1

I figured out how to do it thanks to xMayank's help. In the backend module, the code is:

import { fetch } from 'wix-fetch'

export function getCryptoCurrencyInfo() {
  const url = 'https://dweet.io/get/dweets/for/shyam__5'
  console.log(url)

  return fetch(url, { method: 'get' }).then(response => response.json())
}

To get it to work, the site page (front end) says this:

// For full API documentation, including code examples, visit https://wix.to/94BuAAs

import { getCryptoCurrencyInfo } from 'backend/serviceModule'
import { fetch } from 'wix-fetch'

$w.onReady(function() {
  //TODO: write your page related code here...

  getCryptoCurrencyInfo().then(currencyInfo => {
    const data = currencyInfo
    console.log(data.with[1].content.yearlyWatts)
    console.log(data.with[2].content.monthlyWatts)
    console.log(data.with[3].content.currentDailyCarbonSaved)
    console.log(data.with[4].content.currentDailyWatts)
  })
})
hgb123
  • 13,869
  • 3
  • 20
  • 38
0

considering global_obj your json_object, you can do this

global_obj.with.find(element => element.thing==="shyam__5");
xMayank
  • 1,875
  • 2
  • 5
  • 19
Debugger
  • 11
  • 3