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.
Related
I have got an image folder that contains images of playing cards and I imported them as follow:
import D2 from "../image/D2.png";
import H2 from "../image/H2.png";
import S2 from "../image/S2.png";
and all the way until I reach SA (Spade Ace)
I also have key value pair dictionary such as:
1: "C2",
2: "D2",
3: "H2",
4: "S2",...
I need this dictionary to both calculate the rank of the card and to generate image.
atm I can retrieve the value by evaluating the last char of the string and convert it into int (in case of K,Q etc I have a switch case for them)
My issue is I have trouble to convert the string into the alias that I imported.
I tried doing eval(4) but it didn't work.
if I modified the dictionary into
1: C2,
2: D2,
3: H2,
4: S2,...
I can easily use this as the src of my images, but I can no longer use the card-code to obtain the value of the card.
Could you please suggest a solution or a better approach for this problem.
If I follow the question properly can't you just create the dictionary in this manner? If you intend to use card codes to obtain the value / image then you could build your dictionary like so:
dict = {
C2: { image: C2, value: 1 },
D2: { image: D2, value: 2 },
...
}
c2Image = dict['C2'].image;
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 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#
How to store data correctly in a JSON-like object? From what I've seen, there are two ways to store data inside a json object. Both have different ways to access the data (examples in Python):
Option 1:
obj1 = [
{"id": 1, "payload": "a"},
{"id": 2, "payload": "b"},
{"id": 3, "payload": "c"},
]
Access a certain id's payload in option 1:
for element in obj1:
if element["id"] == 2:
print(element["payload"])
This means, the whole list of elements has to be scanned (possibly) to find the right id and return its value.
On the other hand theres Option 2:
obj2 = {
1: "a",
2: "b",
3: "c",
}
To access the payload of the second "id" is just:
print(obj2[2])
My question now is, why is it more common to see option 1, even though that one seems more complicated to search through? When would I use option 1 and when option 2?
We should clarify some terminology.
When you put a list of values in [], you are creating an array; it is keyed by the array index, not by any element of the data.
When you put a list of keys and values in {}, you are creating an object; you can, as you note, locate a value if you know the corresponding key.
There are many differences between the two structures. In the particular use case you site - wanting to find a data instance based on one of its field's values - an object that uses that field as the key makes sense.
But object keys are unordered. And arrays lend themselves more naturally to having you iterate over all their elements.
It depends what you're going to do with the values; that's why there isn't just one data structure.
Apart from technical artifacts that somehow evolved, option one makes sense in case the order of elements is important.
I want to generate an object that will effectively be able to be applied as a patch to array A in order to produce array B.
Given a function isSame which compares two values and returns true if they're the same, or false otherwise (and the purpose of which is to compare two array elements), is there a known algorithm to calculate the difference between two arrays and return a list of specific differences? The differences would be composed of sets of: X elements removed at index Y and the following elements inserted at index Y.
I've written something that sort of works, but it's buggy at the moment and I'm having trouble moving forward with it, and I'm worrying that I'm reinventing the wheel when somebody else may have already done this. http://jsfiddle.net/G6tYt/1/
If you want the comparison to be both deep and tolerant of objects and arrays both, then this tool I wrote a while back might be of use:
https://github.com/danski/spahql/blob/master/src/SpahQL.DataHelper.js#L18
SpahQL.DataHelper.compare(
{"a": "aval", "b": "bval", "c": "cval", "arr": [0,1,2]},
{"a": "modified", "c": "cval", "d": "added", "arr": [0,1,2,3]}
);
// -> {"/": "~", "/a": "~", "/b": "-", "/d": "+", "/arr": "~", "/arr/3": "+"}