How do I cast a json-like string to json object in alasql? - alasql

I have a json-like string:
'{"a":"b","c":"d"}'
I want to be able to query it in alasql like json, ie something like column->a. How do I do that?

you can convert json-like string to array of object and use JSON.parse. For example
var data= '{"a":"b","c":"d"}'
var parseData = [JSON.parse(test)]
var response = alasql('SELECT * FROM ?',[parseData]);
console.log(response) // [ { a: 'b', c: 'd' } ]

Related

How to convert a string into JSON using SwiftyJSON

The string to convert:
[{"description": "Hi","id":2,"img":"hi.png"},{"description": "pet","id":10,"img":"pet.png"},{"description": "Hello! :D","id":12,"img":"hello.png"}]
The code to convert the string:
var json = JSON(stringLiteral: stringJSON)
The string is converted to JSON and when I try to count how many blocks are inside this JSON (expected answer = 3), I get 0.
print(json.count)
Console Output: 0
What am I missing? Help is very appreciated.
Actually, there was a built-in function in SwifyJSON called parse
/**
Create a JSON from JSON string
- parameter string: Normal json string like '{"a":"b"}'
- returns: The created JSON
*/
public static func parse(string:String) -> JSON {
return string.dataUsingEncoding(NSUTF8StringEncoding)
.flatMap({JSON(data: $0)}) ?? JSON(NSNull())
}
Note that
var json = JSON.parse(stringJSON)
its now changed to
var json = JSON.init(parseJSON:stringJSON)
I fix it on this way.
I will use the variable "string" as the variable what contains the JSON.
1.
encode the sting with NSData like this
var encodedString : NSData = (string as NSString).dataUsingEncoding(NSUTF8StringEncoding)!
un-encode the string encoded (this may be sound a little bit weird hehehe):
var finalJSON = JSON(data: encodedString)
Then you can do whatever you like with this JSON.
Like get the number of sections in it (this was the real question) with
finalJSON.count or print(finalJSON[0]) or whatever you like to do.
There is a built-in parser in SwiftyJSON:
let json = JSON.init(parseJSON: responseString)
Don't forget to import SwiftyJSON!
I'm using as follows:
let yourString = NSMutableString()
let dataToConvert = yourString.data(using: String.Encoding.utf8.rawValue)
let json = JSON(data: dataToConvert!)
print("\nYour string: " + String(describing: json))
Swift4
let json = string.data(using: String.Encoding.utf8).flatMap({try? JSON(data: $0)}) ?? JSON(NSNull())

tag-it autocomplete return data type issue

Can I get array of objects from assignedTags.
Example:
var data = {
label : example,
id : 1,
name : example_1
}
Auto complete shows label , but on select, when I call assignedTags I want to send array of objects back, example :
[{label : example,id : 1,name : example_1}, {label : example,id : 2,name : example_2}, ...]
How can I do it?
Use concat() to combine all of your arrays.
So its array1.concat(array2).
Then use JSON.stringify() to convert the array into a JSON object.
var jsonString = JSON.stringify(array1);
Hope this helps
push into an array somewhat like this :-
var array = [];
var data={label: example};
array.push(data);

It's possible to load an array of tuple from a json file?

I need to load an array of tuple from json file. I tried the following but it doesn't work.
my json file is:
{ "broken" : [(1,1), (1,2), (2,2), (3,1)]}
Then I'm using loadsjsonfrombundle to load data from JSON as follow:
let broken = [(Int, Int)]!
if let dictionary = Dictionary<String, AnyObject>.loadsjsonfrombundle(filename) {
broken = (dictionary["broken"]) as Array
}
Any suggestion?
Thanks.
An array is an ordered collection of values. An array begins with [
(left bracket) and ends with ] (right bracket). Values are separated
by , (comma).
A value can be a string in double quotes, or a number, or true or
false or null, or an object or an array. These structures can be
nested.
So this is invalid JSON format
{ "broken" : [(1,1), (1,2), (2,2), (3,1)]}
because (1,1) is not a string in double quotes, or a number, or true or
false or null, or an object or an array
The possible correct format would be
{ "broken" : [[1,1], [1,2], [2,2], [3,1]]}
in this case you can iterate trough array of arrays and initialize your tuples with array.firstObject and array.lastObject
About JSON

Transform data when parsing a JSON string using Dart

I'm using the parse() function provided in dart:json. Is there a way to transform the parsed data using parse()? I'm thinking of something similar to the reviver argument when parsing JSON using JavaScript:
JSON.parse(text[, reviver])
The parse() function in dart:json takes a callback as an arg that you can use to transform the parsed data. For example, you may prefer to express a date field as a DateTime object, and not as a list of numbers representing the year, month and day. Specify a ‘reviver’ function as a second argument to parse.
This function is called once for each object or list property parsed, and the return value of the reviver function is used instead of the parsed value:
import 'dart:json' as json;
void main() {
var jsonPerson = '{"name" : "joe", "date" : [2013, 10, 3]}';
var person = json.parse(jsonPerson, (key, value) {
if (key == "date") {
return new DateTime(value[0], value[1], value[2]);
}
return value;
});
person['name']; // 'joe'
person['date'] is DateTime; // true
}

Convert IQueryable generic to JSON

I'm producing a projection via:
var query = from book in books
select new
{
label = book.Title,
value = book.ID
};
In my razor page I need to use:
var booksArray = [{
#(json)
}];
such that the resulting array looks like:
label: 'c++',
value: 'c++'
}, {
label: 'java',
value: 'java'
}, {
label: 'php',
value: 'php'
}, {
label: 'coldfusion',
value: 'coldfusion'
}
I've come very very close from a couple different approaches - I can get a string that looks correct on the server side but when rendered to the page itself, all the ' marks become ' .
But focusing on achieving this via JSON.net...
The most likely approach seems like it should be:
var json = JsonConvert.ToString(query);
but that tosses:
Unsupported type: System.Linq.Enumerable+WhereSelectListIterator`2[Project.Entity.Book,<>f__AnonymousType3`2[System.String,System.Int32]]. Use the JsonSerializer class to get the object's JSON representation.
What's the correct JSON.net syntax?
thx
You need a combination of .ToArray() and Html.Raw()
ToArray() to evaluate the query and make JsonConvert happy
var query = from book in books
select new
{
label = book.Title,
value = book.ID
};
var json = JsonConvert.SerializeObject(query.ToArray());
Note: you need to use JsonConvert.SerializeObject if you want to serialize complex types. JsonConvert.ToString is used to convert simple types like bool, guid, int, uri etc.
And in your view Html.Raw to not html encode the JSON:
var booksArray = #(Html.Raw(json))

Resources