NSJSONSerialization JSONObjectWithData will generate JSON object even if the JSON is invalid - ios

NSJSONSerialization JSONObjectWithData will generate JSON object even if the JSON is invalid, also the option that is being used in JSONObjectWithData is NSJSONReadingMutableContainers:
it will create a JSON object with only the valid JSON object/array
If the typo (in the JSON) is at the end of the final parentheses (like a comma), the JSON object will be 100% valid AND the error pointer will be nil.
If the typo is in the middle of value/key (inside the JSON) this value won't be created to the corresponding value/key inside that object AND the error pointer won't be nil.
NSJSONSerialization isValidJSONObject can only validate JSON object (NSArray or NSDictionary) thus, (obviously) NSString representation of the JSON will always return false - in other words, creating a JSON object will always be valid using JSONObjectWithData.
So how can I truly validate a JSON in iOS/Objective-c ?
The invalid JSON - a comma in the end makes the JSON invalid (as mentioned in #2):
{
"A": [
{
"B": 1,
"C": 2,
"D": [
1,
2,
3
]
}
],
"test": [
{
"id": 1,
"id2": 2,
"id3": 3,
"B": [
],
"C": [
],
"D": "test msg",
"E": 2,
"F": "www.google.com",
"G": [
2,
3
],
"test2": [
{
"id": 1,
"id2": 2,
"id3": 3,
"B": [
{
"id": 1,
"id2": "2",
"id3": 3,
"B": 4
}
]
},
]
}
]
}

Related

Rename all keys inside of a specific dictionary in fluentd

Does anyone have any idea how I can filter the elements of a specific list of dictionaries to add a prepended name to the keys of all elements? The field name that is the list of dictionaries is static and known ahead of time. However, the elements inside each dictionary are not. I have tried to use fluent-plugin-rewrite and fluent-plugin-record-transformer without success.
I have a record like the following. I know 'g' ahead of time, but the fact there are two entries in the dictionary and that the specific elements are 'h, i, k, and l' are dynamic.
{
"a": "d1",
"b": "d2",
"c": {
"e": "d3",
"f": "d4"
},
"g": [
{
"h": "d5",
"i": "d6",
},
{
"h": "d7",
"k": "d8",
"l": "d9",
}
]
}
}
That I would like to change to:
{
"a": "d1",
"b": "d2",
"c": {
"e": "d3",
"f": "d4"
},
"g": [
{
"prepend_h": "d5",
"prepend_i": "d6",
},
{
"prepend_h": "d7",
"prepend_k": "d8",
"prepend_l": "d9",
}
]
}
}

sorted map in cypher

When creating a map from collections via apoc's apoc.map.fromLists (or plain cypher for all I care), is there any way to respect/keep the sorting? Or is it just "It's a map, sorting doesn't make sense anyway".
specifically, when executing
with [{name: "z", a: 1}, {name: "b", b: 1}, {name: "b2", b: 2}] as qs
return apoc.map.fromLists([q in qs | q.name], qs)
I get
{
"b2": {
"name": "b2",
"b": 2
},
"z": {
"name": "z",
"a": 1
},
"b": {
"name": "b",
"b": 1
}
}
where indeed I would like to have
{
"z": {
"name": "z",
"a": 1
},
"b": {
"name": "b",
"b": 1
},
"b2": {
"name": "b2",
"b": 2
}
}
Sorry, maps in Cypher are unsorted and unsortable structures, with regard to map keys.
There is apoc.maps.sortedProperties(), however this doesn't return a map, but a list of key/value pairs, where those pairs are sorted alphabetically.

fill a JSON file with embedded arrays from Rails

I have a simple "rss" (ApplicationRecord) table indexed by an id. I would like to have a structured JSON that group each user from a family in an array structure. And then each family in a global array. How can I do that ?
my current plain code to put my data in a json file is :
json.rss #rss do |rs|
json.id rs.id
json.name rs.name
json.family rs.family
json.lastdate rs.lastdate
json.last rs.last
json.s1w rs.s1w
json.s2w rs.s2w
end
But the target file that I want is this one :
{
"rss": [
{
"familyname": "Smith",
"children": [
{
"id": "1",
"name": "bob",
"lastdate": "2010-09-23",
"last": "0.88",
"s1w": "0.83",
"s2w": "0.88"
},
{
"id": 2,
"name": "Mary",
"lastdate": "2011-09-23",
"last": "0.89",
"s1w": "0.83",
"s2w": "0.87"
}
]
},
{
"familyname": "Wesson",
"children": [
{
"id": "1",
"name": "john",
"lastdate": "2001-09-23",
"last": "0.88",
"s1w": "0.83",
"s2w": "0.88"
},
{
"id": 2,
"name": "Bruce",
"lastdate": "2000-09-23",
"last": "0.89",
"s1w": "0.83",
"s2w": "0.87"
}
]
}
]
}
The grouping you are trying to achieve can be done in Ruby with:
#rss.group_by(&:family).values
This is assuming #rss is an array-like collection of objects that have a .family method. The result: is an array of arrays of objects grouped by family.
Now it will be up to use to use Jbuilder's array! method to build the desired JSON output.

How to extract some specific values from json

I am still trying to create an app where the user could transform his name or a word with chemical elements (like breaking bad logo).
The user will enter a word in a text field and when he'll submit it will return him the word with the corresponding chemical symbols if they match, or it will display the "raw" letters if they don't match.
ex: If no symbol matches I am keepking the initial entry so it could be: hello => He ll O (bold char represent the existing chemical symbols)
I know this could be done in js, but the challenge is ROR (btw I don't know any js...)
In an earlier question I had just a hash like:
symbols =
{"cr" => "Cr",
"sb" => "Sb",
"ag" => "Ag",
"ar" => "Ar",
"as" => "As",
"at" => "At",
"n" => "N",
"ba" => "Ba",
"bk" => "Bk"}
and I was using name.downcase.gsub!(Regexp.union(symbols.keys), symbols)to transform the user entry. Actually I need more datas... that's why I chosen the json file.
Like on this picture i will need to use:
"number"
"small"
"molar"
(and the "name" will appear in a caption below)
I have organized a .json file with all the symbols I may need in the app and stored it in my config/periodic_table.json (pasted just a sample cause it's very long).
1°) If a user enter "hello" how do I loop to search for the "he" hash and print the "name","number", "small" and the "molar"
2°) I will use the json as a database (I will use heroku to deploy) so do I have anything to transform for using json and pg together?
[ "symbols"
{
"h": {
"name": "Hydrogen",
"number": 1,
"small": "H",
"molar": 1.00794
},
"he": {
"name": "Helium",
"number": 2,
"small": "He",
"molar": 4.002602
},
"b": {
"name": "Boron",
"number": 5,
"small": "B",
"molar": 10.811
},
"c": {
"name": "Carbon",
"number": 6,
"small": "C",
"molar": 12.0107
},
"n": {
"name": "Nitrogen",
"number": 7,
"small": "N",
"molar": 14.0067
}
}
]
I will need to loop first with the symbols that contain 3 chars, then 2 then 1... shall i change anythin in the json, like an harray for the hashes that contains 3 chars, another for 2 chars , and for 1char?
Correct Json Format
[{
"symbols":{
"h":{
"name": "Hydrogen",
"number": 1,
"small": "H",
"molar": 1.0079
},
"he":{
"name": "Helium",
"number": 2,
"small": "He",
"molar": 4.002602
},
"b": {
"name": "Boron",
"number": 5,
"small": "B",
"molar": 10.811
},
"c": {
"name": "Carbon",
"number": 6,
"small": "C",
"molar": 12.0107
},
"n": {
"name": "Nitrogen",
"number": 7,
"small": "N",
"molar": 14.0067
}
}
}]

Get specific json data rails

I have this json data (actual data is a lot longer, that's why I need only 2)
[
{
"id": 1,
"user_id": 1,
"event_id": 1,
"creator_id": 1,
"event_name": "Poker",
"cruise_ship_name": "Royal Cruise",
},
{
"id": 2,
"user_id": 1,
"event_id": 2,
"creator_id": 1,
"event_name": "Ballroom",
"cruise_ship_name": "Celebrity Infinity",
},
{
"id": 3,
"user_id": 1,
"event_id": 3,
"creator_id": 1,
"event_name": "Tennis",
"cruise_ship_name": "Mediterranean",
}
]
I want to combine all data and get only specific fields (event_name and cruise_ship_name)
So in my final json format
it will be:
[
{
"event_name": "Mexican Fiesta",
"cruise_ship_name": "Celebrity Infinity",
}
]
I have been looking at this example:
#object.to_json {:include => [ :assocation_a, :assocation_b ]}
but not sure what :association_a and :association_b are.
Suppose you have an array of hashes:
events = [
{
"id": 1,
"user_id": 1,
"event_id": 1,
"creator_id": 1,
"event_name": "Poker",
"cruise_ship_name": "Royal Cruise",
},
...
]
You can iterate through each value in your hash, only keeping values of interest:
events.each do |event_hash|
event_hash.keep_if { |key, _| [:event_name, :cruise_ship_name].include?(key) }
end
puts events
The to_json method accept parameters which allow you include specific attributes:
#object.to_json(only: [:event_name, :cruise_ship_name])
The include: :assocation_a option to object, allowing the object association in the assocation_a model to be converted to JSON as well.

Resources