What data format/structure is this and how to handle it? - data-structures

So I have ran into such data format:
{
"i": {
"hid|15#aid|9305#h|Openjobmetis Varese#a|Germani Basket Brescia#h2|VARESE#a2|BRESCIA#round|1019#nat|ita#hcolors": {
"bg|851010#g1|920000#g2|ad0b0b#g3|800000#c|"
},
"acolors": {
"bg|037f43#g1|00582d#g2|0fb966#g3|037f43#c|"
},
"hp|33#vp|20"
},
"idor": 0,
"jr|1#t": 19,
"t2": 30,
"ip|#b": false,
"v": {
"h": 0,
"a": 0,
"t": 30,
"h2": 12,
"a2": 12
}
}
I have never seen such structure and I could not find any sources to explain me this format. Actually I was not even sure how to search it.
So yeah, my question is, what is this data format and how can I handle it?

Looks like JSON.
It appears some data was also encoded as pipe-delimited string values in the JSON.

Ok, right after posting this question, I was able to decode this JSON. It seemed like a JSON all along but these pipes were kind of intimidating. This is how I solved it eventually.
function jsonDecode(json){
if(!json) return null;
json = json.replace(/#/g, '","').replace(/\|/g, '":"').replace(/%/g, '"},{"');
return JSON.parse(json);
}
Great! This question is now answered. Thanks everybody!

Related

How can I remove \ (backslash)? [duplicate]

This question already has answers here:
How to unmarshal an escaped JSON string
(6 answers)
Closed 9 months ago.
I use gin web framework and this is data in redis
[
{
"Score": 12,
"Member": "{"empname":"DDDDDDDD","empid":20}"
},
{
"Score": 5,
"Member": "{"empname":"Dixya Lhyaho","empid":10}"
}
]
after fetch from redis , why return like this
[
{
"Score": 12,
"Member": "{\"empname\":\"DDDDDDDD\",\"empid\":20}"
},
{
"Score": 5,
"Member": "{\"empname\":\"Dixya Lhyaho\",\"empid\":10}"
}
]
How to remove backslash in json (member only) ?
https://go.dev/play/p/QtaapMgfjtn
done.
Strongly recommend you to use json.NewDecoder() and then convert your struct by using the Decode() method. This is much more faster than Unmarshal.
You can check on detail the json documentation that Garr left you as a comment.
Anyway, how are you converting this struct to json? So we can help you better

What is the correct format for `child-resource-references` responses?

TS-0004 Table 7.5.2-2 states that the R/6 (retrieve child-resource-references) call (?fu=1&rcn=6) should return an m2m:resourceRefList. Is this correct? At least two open-source implementations i have seen return m2m:URIList for this query.
Is there anywhere listed examples that show a more complete list of responses for combinations of query parameters?
You are right. fu=1 (discovery request) and rcn=6 should return a m2m:resourceRefList. Such as:
{
"m2m:ch": [
{
"nm": "aResource",
"typ": 99,
"val": "cse-in/aResource"
},
{
"nm": "anotherResource",
"typ": 99,
"val": "cse-in/anotherResource"
}
]
}
There are a couple of examples in the developer guides: https://www.onem2m.org/developer-guides

How to read value from JSON object?

I'm trying to read individual value from be json array object to display in the page. I have tried with below code but couldn't make it. Please advise what am i doing wrong here.
Apperciate your help.
You can get the length of a JavaScript array via its property length. To access the array Reference in your object, you can use dot notation.
In combination, the following should do what you expect:
var obj = {
"Reference": [
{
"name": "xxxxxxxx",
"typeReference": {
"articulation": 0,
"locked": false,
"createdBy": {
"userName": "System",
},
"lastModifiedBy": {
"userName": "System",
},
"lastModified": 1391084398660,
"createdOn": 1391084398647,
"isSystem": true
},
...
},
...
]
};
console.log(obj.Reference.length);
In case you are actually dealing with a JSON string, not a JavaScript object, you will need to parse it first via JSON.parse().
You get the length of an array by simply access the length attribute.
For example [0,1,2,3].length === 4.
If you just want to loop through the array, use forEach or map instead of a for loop. It's safer, cleaner, less hassle and you don't meed to know the length.
E.g.
[0,1,2,3].forEach(num => console.log(num))

d3.js stack layout upgrading from v3 to v4

Using D3 v3, I formatted my data to match Mike's example to quick start my dev process. Example page here https://github.com/d3/d3-3.x-api-reference/blob/master/Stack-Layout.md
var data = [
{
"name": "apples",
"values": [
{ "x": 0, "y": 91},
{ "x": 1, "y": 290}
]
},
{
"name": "oranges",
"values": [
{ "x": 0, "y": 9},
{ "x": 1, "y": 49}
]
}
];
Then all i had to do to obtain the stacked values was
var stack = d3.layout.stack().values(d=>d.values)
var layers = stack(data)
Exactly how he did it in his example.
However, in v4 it seems like the stack function expects tabular formatted data, so the above data would look like this.
var data = [
{x: 0, apples: 91, oranges: 9},
{x: 1, apples: 290, oranges: 49},
];
Is there an easy way to keep my data format and use the v4 stack function? I can't seem to grok the new way to do this. In my current data format, i have useful properties associated with the values array. If i change my data format to tabular, I don't see a convenient way to pair properties with values.
I've been having the same problem. Unfortunately, the new way doesn't seem to match the way APIs serialize model instances (sort of like your original data); instead you're stuck with JSONified tabular data format. I feel like this works when you're working with csv dumps, but that's not generally how databases are organised.
I've posted a related answer here that transforms object-grouped format (eg by fruit in your example) into data-point format (by x-coordinate in your example) as expected by d3.stack(). You'll be able to adapt the code to your particular case.

includeStats in neo4j.rb

I'm using neo4j.rb, and when I run
MATCH (a {name:'apple'}) SET a.flag = true
I'd like to get the response data, which would be along the lines of:
{
"results": [
{
"columns": [],
"data": [],
"stats": {
"contains_updates": true,
"nodes_created": 0,
"nodes_deleted": 0,
"properties_set": 1,
"relationships_created": 0,
"relationship_deleted": 0,
"labels_added": 0,
"labels_removed": 0,
"indexes_added": 0,
"indexes_removed": 0,
"constraints_added": 0,
"constraints_removed": 0
}
}
],
"errors": []
}
Instead, I get nothing--the object is blank, I suppose because I'm not asking for nodes to be returned, but want metadata on the query results.
There's a proposed solution here using py2neo (py2neo return number of nodes and relationships created), with includeStats: true, and I've also tried appending it to the address I'm using to run queries as ?includeStats=true, which I saw somewhere else and resulted in a server not available error (response code 302 / RuntimeError) for me. Is there any solution for this using neo4j.rb ?
Unfortunately we don't keep the metadata when returning results in the neo4j-core gem. It might be something that's easy to add. Perhaps you could create an issue:
https://github.com/neo4jrb/neo4j-core/issues
Pull requests are welcome, of course!

Resources