Parse JSON data into Scala List and HashMap - json

I have JSON data shown below. I am using Python to encode a list, a dictionary and another list into JSON. The final JSON data will look like so:
{
"0": [3, 3, 3],
"1": {
"0": [0, 8, 9],
"1": [1, 2, 3, 4, 10, 11],
"2": [4]
},
"2": [1, 1, 1, 1]
}
My aim is to write some type of Scala function to extract the JSON data in a way that allows:
"0": [3, 3, 3] to be a List(3,3,3)
{"0":[0,8,9], ...} to be a HashMap[Int,List[Int]]
"2": [1, 1, 1, 1] to be a List(1,1,1,1)
Note the length of original Python list and dictionary will vary in size, and the "0", "1", "2" will always be there representing the list, dictionary and list in this order.
I am quite new to Scala and struggling on how to do it without using external libraries. I am trying to use spray-json to do it, since I am using a newer version of Scala (no built-in json parser).

That doesn't look like valid JSON to me, which means any of the JSON parsers you could use won't work. Is that structure fixed? You may want to instead convert it to something thats valid JSON.
eg.
{
"list" : [ 1,1,1],
"someotherObject" : {
"0" : [1,2,3]
},
"anotherList" : [9,8,7]
}
Then you could use Argonaut (for example), and define a decoder, which tells how to map that JSON to object types you specify. See http://argonaut.io/doc/codec/

Related

Ruby create JSON from SQL Server

I am trying to create JSON in Ruby from data coming from a SQL Server table, based off a query. I've worked with Ruby quite a bit and JSON some. But never together.
This is a sample of the JSON I'm trying to create.
Even help with just creating the JSON with the nested arrays and the root element would be helpful.
{
"aaSequences": [
{
"authorIds": [
"ent_fdfdfdfdf_one"
],
"aminoAcids": "aminoAcids_data",
"name": "bbbbb-22",
"schemaId": "ls_jgjgjg",
"registryId": "src_fgfgfgf",
"namingStrategy": "NEW_IDS"
},
{
"authorIds": [
"ent_fdfdfdfdf_two"
],
"aminoAcids": "aminoAcids_data",
"name": "bbbbb-22",
"schemaId": "ls_jgjgjg",
"registryId": "src_fgfgfgf",
"namingStrategy": "NEW_IDS"
}
]
}
Generate a JSON from a Ruby hash object
To generate a json, first start with a hash (like a dict) in Ruby
my_hash = {:foo => 1, :bar => 2, :baz => 3}
Make sure you require the json package as well
require 'json'
Then you can simply convert the hash object to a JSON string
my_hash.to_json # outputs: "{'foo': 1, 'bar': 2, 'baz': 3'}"
You can nest arrays into your hash as well
my_hash_2 = {:foo => [1, 2, 3, 4], :bar => ['a', 'b', 'c', 'd']}
I'll let you try that one on your own, but ruby will handle the nested object just fine for you.
From the docs

mongodb import object with numbers as keys results in array

I have a simple .json which I am trying to import:
{
"data": {
"plans": {
"1": "14",
"2": "20",
"3": "40"
}
}
}
When I use MongoDB Compass to directly import the json file, the plans object is converted into an array:
{ "_id": { "$oid": "5fe3ff5d909016064978f2bd" }, "plans": [null, "14", "20", "40"] }
Am I doing something wrong? Or can I not use numbers as keys in JSON
MongoDB uses BSON, the following note is from that spec:
Array - The document for an array is a normal BSON document with integer values for the keys, starting with 0 and continuing sequentially. For example, the array ['red', 'blue'] would be encoded as the document {'0': 'red', '1': 'blue'}. The keys must be in ascending numerical order.
The object format you are using matches that description, so some drivers will confuse it for an array.
It might be that the data is being stored properly, but when you query it, the client is converting to an array.
Try retrieving the document with something else, perhaps the mongo shell.

Convert JSON array and keys to JSON dict

I've noticed that some APIs use a format of sending a stripped down version of their data via a JSON array like the following:
[
"joe",
[
5,
2,
"yellow"
]
]
And store a set of keys like the following:
[
"name",
["some_data", [
"favorite_number",
"least_favorite_number",
"car_color"
]]
]
To turn the data from a bunch of random values to a readable set of data, like the following:
{
"name": "joe",
"some_data": {
"favorite_number": 5,
"least_favorite_number": 2,
"car_color": "yellow"
}
}
I was wondering how this could be done? I'd prefer it'd be in python, but I'm fine with programming my own libraries.
After grasping at more straws than I could fit in my mouth, I've figured it out. JSON schema is what I'm supposed to be using!

Replacing JSON file with CSV for d3js

http://bl.ocks.org/robschmuecker/7880033
I'm new to javascript and d3. The above example is a dendrogram. I can create my own. However, if I wanted to use it for something like employee data, it seems like it would be a pain to always having to be editing the json unless I'm missing some easier trick.
A csv in excel, that I've used in other charts, would seem like it would work well. Is It possible to replace the flare.json with a csv with the data? if so , how?
No, it's not possible directly. To know why, you'll have to understand the way the function d3.csv creates an array. Suppose you have this CSV:
foo, bar, baz
21, 33, 5
1, 14, 42
When parsed, it will generate a single array of objects, without nested arrays or nested objects. The first row defines the key names, and the other rows the values. This is the array generated for that CSV:
[
{"foo": 21, "bar": 33, "baz": 5},
{"foo": 1, "bar": 14, "baz": 42}
]
Or, if you don't change the type, with the numbers as strings:
[
{"foo": "21", "bar": "33", "baz": "5"},
{"foo": "1", "bar": "14", "baz": "42"}
]
You will not get anywhere close of what you want, which is an array of objects containing arrays containing objects containing arrays etc...
You can modify this array later to create the nested children you need (look at #torresomar comment below), but it's way easier to simply edit your JSON.

How to read nested json file with several roots in extjs

How to read the following json file and display in extjs grid columns?
{
"users": [
{
"id": 123,
"name": "Ed",
"orders": [
{
"id": 50,
"total": 100,
}
]
}
]
}
Could someone explain how to read the nested data in the json? I have tried a lot of options like renderer functions, using the '.' property, etc.
Edit: I would like to read the data within "orders", which are "id" and "total".
You are trying to read in an array nested in an object nested in an array.
So your root would have to be users[0].orders
Is that really what you are trying to do?
Working code:
http://jsfiddle.net/el_chief/s6Ynp/1/
Why is your code nested like this?