I need a reverse from JSON.stringify function for node.js. I need to generate code snippets for different programming languages and need to convert JS structure to python/php/other languages native structures.
Source example: {"a": 5, "b": true, "c": null}
Target example (python): {"a": 5, "b": True, "c": None}
Target example (php): array("a" => 5, "b" => true, "c" => null)
Is there any easy way to do this, instead of inventing my own code generator?
Partially solved this issue:
kong/httpsnippet module had generation of PHP structure
Python:
child_process.execSync('python -c 'import sys,pprint,json; pprint.pprint(json.loads("\n".join(sys.stdin.readlines())))', {input: "{"a": 5, "c": ["b", null]}"})
Ruby:
echo '{"a": null}' | ruby -rjson -e 'puts(JSON.parse(ARGF.read).inspect)'
Now I need Java and C#
Related
This code
import panel as pn
json_viz = pn.Row(pn.pane.JSON({"a": 1, "b": {"c": 2, "d": 3}}))
json_viz.show()
produces that nice JSON visualisation widget/pane:
How can one change the root name Object to some arbitrary name?
Also changing the Object name of b would be a nice to have.
I want to create a markdown file which includes a js object.
I want to add commentaries which are stored in an other object to each line.
The output should look like this:
{
"a": "test, //commentary1
"b": "test2" //commentary2
}
My first intention was to rewrite the stringify function, but it's a bit tricky for edge cases.
How can I achieve this functionality in more better way?
You can opt for 2 options
Add comment as keys like
{
"_comment": "comment text goes here..."
}
Instead of json object use json array and parse accordingly
{
"a": ["test", "commentary1"],
"b": ["test2", "commentary2"]
}
I receive the error Invalid read syntax: "]" when using console.log to print values from JavaScript array objects inside of org file code blocks. Arrays that contain strings produce this error. Arrays which have just numeric values print to the console fine.
I am not sure why org-babel is having difficulty with console.log(). I tried checking the encoding of my org file as a first step. I verified my code using node.js by itself. Specifying a different interpreter (e.g babel-cli) to evaluate the code block produces the same error.
This works
#+BEGIN_SRC js
let myarray = [1, 2, 3, 4, 5];
console.log(myarray);
#+END_SRC
#+RESULTS:
: [1 (\, 2) (\, 3) (\, 4) (\, 5)]
This does not
#+BEGIN_SRC js
let myarray = ["a", "b", "c", "d", "e"];
console.log(myarray);
#+END_SRC
Is there something I need to do within my org config files? I am using Emacs version 26.1 on Windows 7 (build 1, x86_64-w64-mingw32). Node.js is version 10.15.3 .
Correction:
Where I have different lines trying to return/show values inside a code block, only the first return statement produces a result (return ends the immediate scope). What seems to work is process.stdout.write('yourcodehere'+ '\n');
Example:
Trying to use multiple return statements
#+BEGIN_SRC js
return ([0,1,2,3,4]);
return ([5,6,7,8,9]);
#+END_SRC
#+RESULTS:
| 0 | 1 | 2 | 3 | 4 |
Using process.stdout.write()
#+BEGIN_SRC js
process.stdout.write([0, 1, 2, 3, 4] + '\n');
process.stdout.write(["a", "b", "c", "d", "e"] + '\n');
#+END_SRC
#+RESULTS:
: 0,1,2,3,4
: a,b,c,d,e
: undefined
Previous Message:
Ok, I found a simple solution. Use return instead of
console.log().I tried the same example in Python and only got results when using return instead of print. So I tried the same with
my JavaScript examples using return as the final step and this works
great. The formatting of the array in the results block looks better
too.
I have a JSON object as follows
{"Sample" : [{ "key": "KeyName", "values": [ [1025409600000,
10], [1028088000000, -6.3382185140371]
] }]}
Using javascript I can modify any values as follows
data.Sample[0].values.push([1028088000000,0]);
How to perform the similar operation using jq commandline json processor? So the JSON object becomes
{"Sample" : [{ "key": "KeyName", "values": [ [1025409600000, 10],
[1028088000000, 0] ] }]}
Thank you.
As it stands there is a bug in the question, as Javascript's Array.push appends to an array.
The jq equivalent of the given expression:
data.Sample[0].values.push([1028088000000,0]);
would be:
.Sample[0].values += [[1028088000000,0]]
or if you want to use the Javascript syntax, you could define def push(x): .[length] = x;
If you want to replace the last value in the values array by another value, say $x, you could (using jq 1.5 or later) write:
.Sample[0].values[-1] = $x
A more robust (with respect to different jq releases) approach would be:
.Sample[0].values |= (.[length-1] = $x)
With jq 1.5 or later, if you wanted only to change the negative number to 0, you would write:
.Sample[0].values[-1][-1] = 0
Etc.
I am trying to apply the jQuery $.unique() function to an array of objects. I enjoy using this function with an array of primative variables but apparently it doesn't work with objects, as seen here: http://jsfiddle.net/abs10gxo/
var arr1 = [
"aaa",
"bbb",
"aaa"
];
console.log($.unique(arr1)); // Works as expected.
var arr2 = [
{ "a": "aaa" },
{ "b": "bbb" },
{ "a": "aaa" }
];
console.log($.unique(arr2)); // Doesn't work as expected.
I can't seem to find any literature on jQuery.com in regard to using this function with an array of objects; it only adheres to primative types. Can anyone recommend a solution? Library or custom are both welcome answers. I am familiar with doing this in C++ where you can overload the comparative == sign operator but not sure if that is the best approach since I am using jQuery.