Convert Plain String to JSON? - string

I need to convert a string to JSON (in javascript). I have a plain string with correctly formatted JSON in it, like this:
var convert = '{"name":nick,"age":19}';
I need to convert it to just the json (e.g., minus the '' quotes). I have done some testing and found this to be the reason I'm having problems. There must be a way to convert it on the fly, right?
Help very much appreciated,
Nick

You need to use a JSON library; nearly all modern browsers have a native one available to them, however, to ensure compatibility with IE7 and below you will need to pull in Douglas Crockford's JSON2 library.
Once you have a JSON library, just issue:
var result = JSON.parse('{"name":nick,"age":19}');

JSON.parse(convert)
Crockford's json2.js will give you JSON.parse for browsers that don't already have it (modern browsers have it natively).

Related

encodeURIComponent Not working properly NodeJS

I'm trying to encode text of biography for Instagram change biography on profile and it doesnt accept my biography.
I used:
let bio = "111X#%(!#)!$(*!Gram)"
let biography = encodeURIComponent(bio)
and it failing. Need help please
Java equivalent to JavaScript's encodeURIComponent that produces identical output?
I need this but for NodeJS, so it generate same encoding as Java one.
There are some distinctions between Java's URI encoding and the JS encodeURIComponent functions. Spaces will translate to + in Java but they will translate to %20 in Javascript. The JS function uses UTF-8 by default, but Java could be using a different format for encoding (depending on your code editor). Take a look at this stack overflow post for more information: https://stackoverflow.com/a/25298843/9571755
Hope that helps and happy coding :)

AfterEffects Script - Create Excel data using script

nice to meet you!
I'm currently making an AfterEffect script that writes layer information to Excel, but no matter how much I research, I can't find a way to do it. If someone knows how to do it, can you tell me?
Actually, I'm Japanese and I don't understand English very well, so I used Google Translate to write the sentences, so I'm glad if it's conveyed well.
Layer information can be obtained from the API using the Layer object which can be accessed directly like so: app.project.item(index).layer(index) or by looping through a CompItem's layers like so:
var theComp = app.project.activeItem;
for (var i = 1; i <= theComp.numLayers; i++){
// layers in a comp are indexed from 1, rather than 0
theLayer = theComp.layer(i);
<do something with theLayer>
}
You can write this to a CSV XML or JSON file using the File.write() or File.writeln() methods of the File object. These can easily be imported into Excel.
Because the version of Javascript that extendscript uses dates back to 1995, it doesn't have native JSON.stringify() or XML.write() methods, so to create JSON or XML you will need Javascript implementations like this one for XML and this one for JSON. If you search for core JS polyfill for these functions there are dozens around.

How do `Map<String, String>` get to know, the `String` endings in `MethodChannel` arguments

If dart and kotlin code communicate through binary(array of 8-bit integers (0-255)), then how does String end or even int end is represented in, or determined from binary sequence of bytes, is there some special charCode or something else.
Also is there a way to save a List<int> as-it-is to a file.txt, so it can be read directly to List<int> instead of serialization.
Please guide this new dev,
Thanking you...
Since Flutter handles the MethodChannel, in both the Dart side and Kotlin side, it can be allowed to have its own internal protocol to communicate between the native layer and Flutter. In theory they could use JSON but they are probably using something else based on the supported types and also making it more efficient: https://docs.flutter.dev/development/platform-integration/platform-channels?tab=type-mappings-kotlin-tab#codec
For saving a List<int> to a file, you need to determine how you want to encode the content in the file and then how you want to decode it. It can be as simply as just saving each number separated by comma or encode the list into JSON.
If your list of numbers can be represented with Uint8List or Int8List, then you can basically just save the numbers as raw bytes to the file and then read them again.
But List<int> is a list of 64-bit numbers and you should therefore determine how you want to encode this exactly.
For writing to files, there are several ways to do it but the specific way depends on what you exactly want. So without any more details I can just suggest you check the API: https://api.dart.dev/stable/2.17.3/dart-io/File-class.html

Decode base64 to string and Encode string to base64 in XQuery

I needed to work with base64 in Xquery on a project and found almost nothing on Internet, thankfully there was one GitHub repository that had what I need to complete the encode-decode.
So, if you have a xs:string and you need to encode as base64 or you have a base64 string and need to decode as xs:string* you can use the XQuery on the repository that is highlighted as answer.
Obs: Comments are PT-BR, maybe you need to translate to understand what each function is used for
If your system supports it, use the EXPath binary library. Specification is at https://www.w3.org/2013/12/expath-binary-20131203/
See in particular the functions bin:encode-string($string, $encoding) which converts xs:string to xs:base64Binary, and bin:decode-string($in, $encoding) which converts xs:base64Binary to xs:string.
Your XQuery context also might come with a utility function that does the conversion.
eXist-db offers util:base64-encode and util:base64-decode.

How to check if a string is plaintext or base64 format in Node.js

I want to check if the given string to my function is plain text or base64 format. I am able to encode a plain text to base64 format and reverse it.
But I could not figure out any way to validate if the string is already base64 format. Can anyone please suggest correct way of doing this in node js? Is there any API for doing this already available in node js.
Valid base64 strings are a subset of all plain-text strings. Assuming we have a character string, the question is whether it belongs to that subset. One way is what Basit Anwer suggests. Those libraries require installing libicu though. A more portable way is to use the built-in Buffer:
Buffer.from(str, 'base64')
Unfortunately, this decoding function will not complain about non-Base64 characters. It will just ignore non-base64 characters. So, it alone will not help. But you can try encoding it back to base64 and compare the result with the original string:
Buffer.from(str, 'base64').toString('base64') === str
This check will tell whether str is pure base64 or not.
Encoding is byte level.
If you're dealing in strings then all you can do is to guess or keep meta data information with your string to identify
But you can check these libraries out:
https://www.npmjs.com/package/detect-encoding
https://github.com/mooz/node-icu-charset-detector

Resources