0

I am attempting to parse and display a large JSON object on a dashboard I am creating and I think I am getting an error because of the way that I am formatting the javascript within my component. Here is the code as it is:

import React, { Component } from 'react'; 

class WhoIs extends Component {

  render() {

    var origObject = {"id":3,"code":"\nfrom ipwhois import IPWhois\nimport json\nobj = IPWhois('104.24.114.36')\nresults = obj.lookup_rdap(depth=1)\nprint(json.dumps(results))\n","state":"available","output":{"status":"ok","execution_count":3,"data":{"text/plain":"{\"raw\": null, \"entities\": [\"CLOUD14\", \"ABUSE2916-ARIN\", \"ADMIN2521-ARIN\", \"NOC11962-ARIN\"], \"asn_registry\": \"arin\", \"network\": {\"status\": null, \"handle\": \"NET-104-16-0-0-1\", \"name\": \"CLOUDFLARENET\", \"links\": [\"https://rdap.arin.net/registry/ip/104.16.0.0\", \"https://whois.arin.net/rest/net/NET-104-16-0-0-1\"], \"raw\": null, \"country\": null, \"ip_version\": \"v4\", \"start_address\": \"104.16.0.0\", \"notices\": [{\"description\": \"By using the ARIN RDAP/Whois service, you are agreeing to the RDAP/Whois Terms of Use\", \"links\": [\"https://www.arin.net/whois_tou.html\"], \"title\": \"Terms of Service\"}], \"end_address\": \"104.31.255.255\", \"remarks\": [], \"parent_handle\": \"NET-104-0-0-0-0\", \"cidr\": \"104.16.0.0/12\", \"type\": null, \"events\": [{\"action\": \"last changed\", \"timestamp\": \"2017-02-17T18:08:30-05:00\", \"actor\": null}, {\"action\": \"registration\", \"timestamp\": \"2014-03-28T10:30:55-04:00\", \"actor\": null}]}, \"objects\": {\"CLOUD14\": {\"status\": null, \"roles\": [\"registrant\"], \"handle\": \"CLOUD14\", \"entities\": [\"ABUSE2916-ARIN\", \"ADMIN2521-ARIN\", \"NOC11962-ARIN\"], \"links\": [\"https://rdap.arin.net/registry/entity/CLOUD14\", \"https://whois.arin.net/rest/org/CLOUD14\"], \"raw\": null, \"notices\": null, \"contact\": {\"kind\": \"org\", \"name\": \"Cloudflare, Inc.\", \"title\": null, \"phone\": null, \"role\": null, \"address\": [{\"type\": null, \"value\": \"101 Townsend Street\\nSan Francisco\\nCA\\n94107\\nUnited States\"}], \"email\": null}, \"events_actor\": null, \"remarks\": [], \"events\": [{\"action\": \"last changed\", \"timestamp\": \"2017-02-17T18:19:16-05:00\", \"actor\": null}, {\"action\": \"registration\", \"timestamp\": \"2010-07-09T13:10:42-04:00\", \"actor\": null}]}, \"ADMIN2521-ARIN\": {\"status\": [\"validated\"], \"roles\": [\"technical\"], \"handle\": \"ADMIN2521-ARIN\", \"entities\": null, \"links\": [\"https://rdap.arin.net/registry/entity/ADMIN2521-ARIN\", \"https://whois.arin.net/rest/poc/ADMIN2521-ARIN\"], \"raw\": null, \"notices\": null, \"contact\": {\"kind\": \"group\", \"name\": \"Admin\", \"title\": null, \"phone\": [{\"type\": [\"work\", \"voice\"], \"value\": \"+1-650-319-8930\"}], \"role\": null, \"address\": [{\"type\": null, \"value\": \"101 Townsend Street\\nSan Francisco\\nCA\\n94107\\nUnited States\"}], \"email\": [{\"type\": null, \"value\": \"[email protected]\"}]}, \"events_actor\": null, \"remarks\": [], \"events\": [{\"action\": \"last changed\", \"timestamp\": \"2017-10-05T15:42:30-04:00\", \"actor\": null}, {\"action\": \"registration\", \"timestamp\": \"2011-04-19T15:11:36-04:00\", \"actor\": null}]}, \"ABUSE2916-ARIN\": {\"status\": [\"validated\"], \"roles\": [\"abuse\"], \"handle\": \"ABUSE2916-ARIN\", \"entities\": null, \"links\": [\"https://rdap.arin.net/registry/entity/ABUSE2916-ARIN\", \"https://whois.arin.net/rest/poc/ABUSE2916-ARIN\"], \"raw\": null, \"notices\": null, \"contact\": {\"kind\": \"group\", \"name\": \"Abuse\", \"title\": null, \"phone\": [{\"type\": [\"work\", \"voice\"], \"value\": \"+1-650-319-8930\"}], \"role\": null, \"address\": [{\"type\": null, \"value\": \"101 Townsend Street\\nSan Francisco\\nCA\\n94107\\nUnited States\"}], \"email\": [{\"type\": null, \"value\": \"[email protected]\"}]}, \"events_actor\": null, \"remarks\": [], \"events\": [{\"action\": \"last changed\", \"timestamp\": \"2017-02-17T18:11:38-05:00\", \"actor\": null}, {\"action\": \"registration\", \"timestamp\": \"2011-02-14T19:00:47-05:00\", \"actor\": null}]}, \"NOC11962-ARIN\": {\"status\": [\"validated\"], \"roles\": [\"noc\"], \"handle\": \"NOC11962-ARIN\", \"entities\": null, \"links\": [\"https://rdap.arin.net/registry/entity/NOC11962-ARIN\", \"https://whois.arin.net/rest/poc/NOC11962-ARIN\"], \"raw\": null, \"notices\": null, \"contact\": {\"kind\": \"group\", \"name\": \"NOC\", \"title\": null, \"phone\": [{\"type\": [\"work\", \"voice\"], \"value\": \"+1-650-319-8930\"}], \"role\": null, \"address\": [{\"type\": null, \"value\": \"101 Townsend Street\\nSan Francisco\\nCA\\n94107\\nUnited States\"}], \"email\": [{\"type\": null, \"value\": \"[email protected]\"}]}, \"events_actor\": null, \"remarks\": [], \"events\": [{\"action\": \"last changed\", \"timestamp\": \"2017-02-17T18:15:44-05:00\", \"actor\": null}, {\"action\": \"registration\", \"timestamp\": \"2011-04-19T15:25:31-04:00\", \"actor\": null}]}}, \"asn_country_code\": \"US\", \"asn_date\": \"2014-03-28\", \"asn_cidr\": \"104.24.112.0/20\", \"nir\": null, \"query\": \"104.24.114.36\", \"asn\": \"13335\"}"}},"progress":1.0}; 
    var myTarget = origObject.output.data['text/plain'];
    var obj = JSON.parse(myTarget);

    for (var key in obj) {
      if (obj.hasOwnProperty(key)) {
        document.getElementById('mydiv').innerHTML
        console.log(key + " -> " + obj[key]);
      }
    }

    return (
      <div className="col-md-12 bucket">
          <div className="card">
              <div className="card-header">
                <span className="header-title">
                    Table Header
                </span>
                <span className="float-right">
                  <button className="btn btn-sm dl-btn"><i className="fa fa-download" aria-hidden="true"></i></button>
                </span>
              </div>
            <div className="card-block data-block">
              <div id="mydiv"></div>   
            </div>
          </div>
      </div>
    );
  }
}

export default WhoIs;

After the return() I have a div with "mydiv" assigned to it. If I take out the document.getElementById and just use the console.log I get all of the objects parsed and no errors. From my understanding this type error seems to come from the html div with the id assigned being placed after my script code, but I've tried placing the JS after the return() but it doesn't work. I would assume this is a formatting issue with React??? Any help would be appreciated. Thanks!

5
  • render does not return. Is the code complete? Commented Feb 21, 2018 at 8:29
  • Is the code complete? What do you mean? Commented Feb 21, 2018 at 8:30
  • will it be possible to create a code snippet explaining your issue? Commented Feb 21, 2018 at 8:32
  • It would be better to show the whole codes. And the error stack would help. Commented Feb 21, 2018 at 8:34
  • I can't find a fiddle or whatever with updated syntax so I updated with the whole component. Does this help? Commented Feb 21, 2018 at 8:41

1 Answer 1

1

You can create an array of values to render in JSX, you'll probably never need to use document.getElement in React :

render() {
    var origObject = {"id":3,"code":"\nfrom ipwhois import IPWhois\nimport json\nobj = IPWhois('104.24.114.36')\nresults = obj.lookup_rdap(depth=1)\nprint(json.dumps(results))\n","state":"available","output":{"status":"ok","execution_count":3,"data":{"text/plain":"{\"raw\": null, \"entities\": [\"CLOUD14\", \"ABUSE2916-ARIN\", \"ADMIN2521-ARIN\", \"NOC11962-ARIN\"], \"asn_registry\": \"arin\", \"network\": {\"status\": null, \"handle\": \"NET-104-16-0-0-1\", \"name\": \"CLOUDFLARENET\", \"links\": [\"https://rdap.arin.net/registry/ip/104.16.0.0\", \"https://whois.arin.net/rest/net/NET-104-16-0-0-1\"], \"raw\": null, \"country\": null, \"ip_version\": \"v4\", \"start_address\": \"104.16.0.0\", \"notices\": [{\"description\": \"By using the ARIN RDAP/Whois service, you are agreeing to the RDAP/Whois Terms of Use\", \"links\": [\"https://www.arin.net/whois_tou.html\"], \"title\": \"Terms of Service\"}], \"end_address\": \"104.31.255.255\", \"remarks\": [], \"parent_handle\": \"NET-104-0-0-0-0\", \"cidr\": \"104.16.0.0/12\", \"type\": null, \"events\": [{\"action\": \"last changed\", \"timestamp\": \"2017-02-17T18:08:30-05:00\", \"actor\": null}, {\"action\": \"registration\", \"timestamp\": \"2014-03-28T10:30:55-04:00\", \"actor\": null}]}, \"objects\": {\"CLOUD14\": {\"status\": null, \"roles\": [\"registrant\"], \"handle\": \"CLOUD14\", \"entities\": [\"ABUSE2916-ARIN\", \"ADMIN2521-ARIN\", \"NOC11962-ARIN\"], \"links\": [\"https://rdap.arin.net/registry/entity/CLOUD14\", \"https://whois.arin.net/rest/org/CLOUD14\"], \"raw\": null, \"notices\": null, \"contact\": {\"kind\": \"org\", \"name\": \"Cloudflare, Inc.\", \"title\": null, \"phone\": null, \"role\": null, \"address\": [{\"type\": null, \"value\": \"101 Townsend Street\\nSan Francisco\\nCA\\n94107\\nUnited States\"}], \"email\": null}, \"events_actor\": null, \"remarks\": [], \"events\": [{\"action\": \"last changed\", \"timestamp\": \"2017-02-17T18:19:16-05:00\", \"actor\": null}, {\"action\": \"registration\", \"timestamp\": \"2010-07-09T13:10:42-04:00\", \"actor\": null}]}, \"ADMIN2521-ARIN\": {\"status\": [\"validated\"], \"roles\": [\"technical\"], \"handle\": \"ADMIN2521-ARIN\", \"entities\": null, \"links\": [\"https://rdap.arin.net/registry/entity/ADMIN2521-ARIN\", \"https://whois.arin.net/rest/poc/ADMIN2521-ARIN\"], \"raw\": null, \"notices\": null, \"contact\": {\"kind\": \"group\", \"name\": \"Admin\", \"title\": null, \"phone\": [{\"type\": [\"work\", \"voice\"], \"value\": \"+1-650-319-8930\"}], \"role\": null, \"address\": [{\"type\": null, \"value\": \"101 Townsend Street\\nSan Francisco\\nCA\\n94107\\nUnited States\"}], \"email\": [{\"type\": null, \"value\": \"[email protected]\"}]}, \"events_actor\": null, \"remarks\": [], \"events\": [{\"action\": \"last changed\", \"timestamp\": \"2017-10-05T15:42:30-04:00\", \"actor\": null}, {\"action\": \"registration\", \"timestamp\": \"2011-04-19T15:11:36-04:00\", \"actor\": null}]}, \"ABUSE2916-ARIN\": {\"status\": [\"validated\"], \"roles\": [\"abuse\"], \"handle\": \"ABUSE2916-ARIN\", \"entities\": null, \"links\": [\"https://rdap.arin.net/registry/entity/ABUSE2916-ARIN\", \"https://whois.arin.net/rest/poc/ABUSE2916-ARIN\"], \"raw\": null, \"notices\": null, \"contact\": {\"kind\": \"group\", \"name\": \"Abuse\", \"title\": null, \"phone\": [{\"type\": [\"work\", \"voice\"], \"value\": \"+1-650-319-8930\"}], \"role\": null, \"address\": [{\"type\": null, \"value\": \"101 Townsend Street\\nSan Francisco\\nCA\\n94107\\nUnited States\"}], \"email\": [{\"type\": null, \"value\": \"[email protected]\"}]}, \"events_actor\": null, \"remarks\": [], \"events\": [{\"action\": \"last changed\", \"timestamp\": \"2017-02-17T18:11:38-05:00\", \"actor\": null}, {\"action\": \"registration\", \"timestamp\": \"2011-02-14T19:00:47-05:00\", \"actor\": null}]}, \"NOC11962-ARIN\": {\"status\": [\"validated\"], \"roles\": [\"noc\"], \"handle\": \"NOC11962-ARIN\", \"entities\": null, \"links\": [\"https://rdap.arin.net/registry/entity/NOC11962-ARIN\", \"https://whois.arin.net/rest/poc/NOC11962-ARIN\"], \"raw\": null, \"notices\": null, \"contact\": {\"kind\": \"group\", \"name\": \"NOC\", \"title\": null, \"phone\": [{\"type\": [\"work\", \"voice\"], \"value\": \"+1-650-319-8930\"}], \"role\": null, \"address\": [{\"type\": null, \"value\": \"101 Townsend Street\\nSan Francisco\\nCA\\n94107\\nUnited States\"}], \"email\": [{\"type\": null, \"value\": \"[email protected]\"}]}, \"events_actor\": null, \"remarks\": [], \"events\": [{\"action\": \"last changed\", \"timestamp\": \"2017-02-17T18:15:44-05:00\", \"actor\": null}, {\"action\": \"registration\", \"timestamp\": \"2011-04-19T15:25:31-04:00\", \"actor\": null}]}}, \"asn_country_code\": \"US\", \"asn_date\": \"2014-03-28\", \"asn_cidr\": \"104.24.112.0/20\", \"nir\": null, \"query\": \"104.24.114.36\", \"asn\": \"13335\"}"}},"progress":1.0}; 
    var myTarget = origObject.output.data['text/plain'];
    var obj = JSON.parse(myTarget);
    var results = [];
    for (var key in obj) {
      if (obj.hasOwnProperty(key)) {
        results.push(obj[key]);
      }
    }
    return (
        <div> 
            {results.map((value) => <p> {value} </p>)}
        </div>
    );
}

If the JSON you are parsing is a constant you should consider doing the parsing in the constructor or in componentDidMount because doing it in render will reparse your object at each render(if the component state changes)

Sign up to request clarification or add additional context in comments.

8 Comments

I'm getting this error "Objects are not valid as a React child (found: object with keys {status, handle, name, links, raw, country, ip_version, start_address, notices, end_address, remarks, parent_handle, cidr, type, events}). If you meant to render a collection of children, use an array instead."
@RobiminusMaximus You need to handle the nested objects, i don't know what is your goal but if you want to display the objects in plain text you can do it with {JSON.stringify(value)}
I am just trying to dump the key value pairs into a span into this table/card.
@RobiminusMaximus The fact is your data has some objets with multiple levels of nested objects so you'll need a recursive function to parse it and display it correctly, you'll also have to handle the display of arrays.
@RobiminusMaximus Exactly, you can take a look at this codepen that shows an example of rendering nested objects, but it'll be more complex in your case as you'll have to test each time if the nested item is an array or an object.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.