Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON-Datapoint not usable in binding #263

Closed
oweitman opened this issue Feb 5, 2020 · 5 comments
Closed

JSON-Datapoint not usable in binding #263

oweitman opened this issue Feb 5, 2020 · 5 comments
Labels

Comments

@oweitman
Copy link
Contributor

oweitman commented Feb 5, 2020

is is not possibe to use a JSON-String in a binding operation if the JSON contains douple quotaition marks.

Example:

  • Create Datapoint "javascript.0.json" type string and content: [{"a":1},{"a":2}]
    -create a basic HTML widget and following binding in HTML-property: {a:javascript.0.json;console.debug(a)}

if you change the datapoint to [{'a':1},{'a':2}]

it works.
if you request json data from external services it is not easy to change all doupe quotation marks to singel quotation marks

a possible solution is to test the string for valid JSON-Data (this includes also number, false,true) as follows:

  1. Test for JSON-Data. if no Error you can add the string without quotation marks (true,false,JSON,numbers)
    2 if the Test fails, add Data with quotaion marks. (strings)
    try {
        if (JSON.parse(value));
        string += 'var ' + oids[t].operations[k].arg[a].name + ' = ' + value + ';';
    } catch(e) {
        string += 'var ' + oids[t].operations[k].arg[a].name + ' = "' + value + '";';
    }

affected code

string += 'var ' + oids[t].operations[k].arg[a].name + ' = "' + value + '";';

@gsicilia82
Copy link
Contributor

gsicilia82 commented Apr 27, 2020

In my case it's not possible to bin JSONs at all.

I have tested the following code, it's without try/catch. Maybe it's faster but you have to parse the binding in second step:

string += 'var ' + oids[t].operations[k].arg[a].name + ' = ' + JSON.stringify(value) + ';';

Binding Example: {a: json.state; JSON.parse(a)["key"]}

For me it doesn't matter. I like your solution, because parsing is not necessary. Unfortunately in both cases is not possible to bind Arrays.
I tested an if-statement with

if (typeof value === "object") {
         ..... insert value without quotes
} else {
         ..... insert value with quotes
}

But I got errors in browsers console "type of undefined etc."

@oweitman
Maybe, if you make a pull request, they would merge your solution.

@oweitman
Copy link
Contributor Author

oweitman commented Apr 27, 2020

@gsicilia82
your example only works for simple datatypes (bool,number,string,etc).
if a JSON-string arrives you stringify it again. this leads to escaping quotation marks and this is wrong.
did you have an example of an array that didnt worked?

@gsicilia82
Copy link
Contributor

@oweitman
in my example, it works for JSONs when getting them from iobroker state. At least, they are parsable... The benefit with stringify is, that you neither need if/else nor try/catch.

Not one Array worked for me. It doesn't matter which one I tested. e.g.
objectState = ["a","b","c"]

{a:objectState;a[0]} shows "[" instead of "a". Because the Array is stored with quotes in var a; it's a String not an Array. This leads to show first char instead of first element.

@stale
Copy link

stale bot commented Aug 25, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs within the next 7 days. Please check if the issue is still relevant in the most current version of the adapter and tell us. Also check that all relevant details, logs and reproduction steps are included and update them if needed. Thank you for your contributions.
Dieses Problem wurde automatisch als veraltet markiert, da es in letzter Zeit keine Aktivitäten gab. Es wird geschlossen, wenn nicht innerhalb der nächsten 7 Tage weitere Aktivitäten stattfinden. Bitte überprüft, ob das Problem auch in der aktuellsten Version des Adapters noch relevant ist, und teilt uns dies mit. Überprüft auch, ob alle relevanten Details, Logs und Reproduktionsschritte enthalten sind bzw. aktualisiert diese. Vielen Dank für Eure Unterstützung.

@stale stale bot added the wontfix label Aug 25, 2020
@stale
Copy link

stale bot commented Sep 2, 2020

This issue has been automatically closed because of inactivity. Please open a new issue if still relevant and make sure to include all relevant details, logs and reproduction steps. Thank you for your contributions.
Dieses Problem wurde aufgrund von Inaktivität automatisch geschlossen. Bitte öffnet ein neues Issue, falls dies noch relevant ist und stellt sicher das alle relevanten Details, Logs und Reproduktionsschritte enthalten sind. Vielen Dank für Eure Unterstützung.

@stale stale bot closed this as completed Sep 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants