1

I'm implementing some changes to existing js code that is a plugin for freeboard. The problem is that I'm not sure how to acces received json payload.

The implementation of api that the plugin is supposed to communicate with has been changed from not-so-RESTfull to RESTfull like. And there are difficulties.

The original jscript plugin contains this code, which I think is partly a culprit of my problems described here.

    $.ajax({
            url       : url,
            dataType  : "JSON",
            type: "POST",
            data: '{"entities":[{"type": "'+currentSettings.type+'","isPattern": false, "id" :"'+currentSettings.id+'"}]}',
            beforeSend: function(xhr)
            {
                xhr.setRequestHeader("Content-Type", "application/json");
                xhr.setRequestHeader("Accept", "application/json");
                xhr.setRequestHeader("Fiware-Service", currentSettings.service);
                xhr.setRequestHeader("Fiware-ServicePath", currentSettings.servicepath);
                xhr.setRequestHeader("X-Auth-Token", currentSettings.xauthtoken);
            },

with url of type:

     127.0.0.1:1026/v1/queryContext

This whole js part should imitate query taken from example from Orion Context Broker API v1 that looks like this.

The query that its supposed to imitate

Link to query operation in Orion Context Broker API v1

As you can see it is not really RESTfull and it has been changed, however plugin has been updated 3 years ago and doesn't work with new approach.

In the new version of API implementation I can query for resources simply by

    curl localhost:1026/v2/entities/Room1?type=Room -s -S -H 'Accept: application/json' | python -mjson.tool

Documentation of recent v2 of API implementation.

I changed that part of plugin code, mainly definition of URL and I suppose (but can't really check it - is there any compiler for js or something to test it? I tried Naked for python and running it via node, but there's no way of debugging it) it should work.

    $.ajax({
            url       : url,
            type: "GET",
            beforeSend: function(xhr)
            {                   
                xhr.setRequestHeader("Accept", "application/json");
                xhr.setRequestHeader("Fiware-Service", currentSettings.service);
                xhr.setRequestHeader("Fiware-ServicePath", currentSettings.servicepath);
                xhr.setRequestHeader("X-Auth-Token", currentSettings.xauthtoken);
            },

I have a connection and the service using the url that I feed the code, it's tested via Insomnia. If this code is without bugs, he real problem is with following part of original code:

    //Get attributes 
    attributes=data["contextResponses"][0]["contextElement"]["attributes"];

It gets attributes from received payload for further manipulation. In v1 api received payload looked like this:

And now it look like this:

enter image description here

Or if I use keyValues option like this:

enter image description here

where id is used in request's url and type can be too, it is known, so they don't matter. The most important things are those attributes that follows id and type.

How do I extract them?

The succes part of original code, containing mentioned getAttributes method:

Code of that plugin is mentioned in first paragraph of this question.

success   : function(data)
            {        
     //Initialize mydata 
                mydata={};

                //if advanced setting is true do not modify received JSON 
                if(currentSettings.advanced){
                    mydata=data;
                }

                //if advanced setting is false reduce received JSON nesting
                else{

                    //Get attributes 
                    attributes=data["contextResponses"][0]["contextElement"]["attributes"];

                    //Get each attribute and append it to mydata
                    //////////////////////////////////////////////
                    Then there is manipulation on received data that is 
                    similar in both cases
Szymon Caban
  • 127
  • 1
  • 8
  • Can you show us your `success` function in the `$.ajax` call? Because it sounds like you're asking how to read a JavaScript object's fields. Which is completely unrelated to 99% of your question content. Essentially, it's `success: function(response) { alert(response.capacityUsed); }` –  Jul 04 '18 at 13:19
  • In addition to success as in Chris's comment, your data field should also be a JSON object. – Helping hand Jul 04 '18 at 13:23
  • I edited the question, and original code of javascipt plugin is linked. – Szymon Caban Jul 04 '18 at 13:28
  • 1
    @SzymonCaban Like I thought, the bulk of your question is completely irrelevant. In your `success` function, `data` contains what you posted as images with green keys. Accessing the various values is done using for instance `data["location"]` or `data.location`. There's nothing further to it. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors example code: https://jsfiddle.net/khrismuc/nf41Lvty/ –  Jul 04 '18 at 13:32

1 Answers1

0

I dont know if i understand your question correct but i think this is wat you are looking for

const objectCreatedFromResponse = JSON.parse(response);

If you would then want to access to a attribute, lets say the id for instance you can just do

const id = objectCreatedFromResponse.id;
locomain
  • 185
  • 1
  • 15