create json object from string with ruby [closed] - ruby-on-rails

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am really new about Ruby development. I am trying to create a json file with strings. My json file like below. Can you help to me
{
"App":{
"properties":{"color":"red"},
"screens":[
{"id":"page1", "properties":{"color":"red"}, "elements":[
{"type":"txtbox", "properties":{"color":"red"}},
{"type":"button", "properties":{"color":"red"}}
]
},
{"id":"page2", "properties":{"color":"red"}, "elements":[
{"type":"txtbox", "properties":{"color":"red"}},
{"type":"button", "properties":{"color":"red"}}
]
}
]
}
}

You can parse JSON with ruby from a hash:
require 'json'
my_hash = JSON.parse('{"hello": "goodbye"}')
puts my_hash["hello"] => "goodbye"
Or generate it from a hash:
require 'json'
my_hash = {:hello => "goodbye"}
puts JSON.generate(my_hash) => "{\"hello\":\"goodbye\"}"
Have a look at the JSON documentation.

Related

create expandable tableview? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm using API (which one I cannot share publicly), and get responses like
[
Response1.response(category1: "cat1", response: "parent0"),
Response1.response(category2: "cat2", response: "parent1"),
Response1.response(category3: "cat3", response: "parent2"),
Response1.response(category4: "cat4", response: "parent3")
]
and
[
Response2.response(subcategory1: "resp0", response: "child0parent0"),
Response2.response(subcategory2: "resp1", response: "child0parent1"),
Response2.response(subcategory3: "resp2", response: "child0parent2"),
Response2.response(subcategory4: "resp3", response: "child0parent3")
]
I want to show data on expandable tableview like below example image or let me know, how can I create 2D array from above two responses?
Please use this link to get the code for creating expandable tableviews in iOS -
https://www.appcoda.com/expandable-table-view/
Create 2 dimensional array like this -
[
"parent 0" : ["child0parent0", "child1parent0", "child2parent0",
"child3parent0"],
"parent 1" : ["child0parent1", "child1parent1", "child2parent1"],
....
]

How to upload file from phone and send it as FormData Ionic 4 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm using Ionic trying to use chooser plugin to chose file from phone, transform the URI to a File and send it as FormData. Nothing seems to work.
You can initialize FormData object by creating an instance from new FormData interface as given below.
const formData = new FormData()
const formData = new FormData();
formData.append('firstname', this.firstname);
formData.append('lastname', this.lastname);
formData.append('email' , this.email);
formData.append('phone' , this.phone);
formData.append('experience' , this.experience);
formData.append('userId' , this.userInfo.user.id);

https://youtu.be/7r7UUulUdag [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I want to convert id into string to use in url
#tutorial_id = Demotutorial.where("job_id = ?", #job_id).select( "id")
#t_id = #tutorial_id.to_s
render json: #t_id
Getting this error
S
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
Tutorial Json array
{"demotutorials":[{"demotutorials":{"id":50}}]}
there are two things for your problem
if you using where the result is activerecord relation, which can
have couple of records, you must select which row, I gave you
sample first fow to choose first record
#tuturial_id is a activerecord row, so you must choose which column, I gave you sample using #tutorial_id.id
below is sample code:
#tutorial_id = Demotutorial.where("job_id = ?", #job_id).select( "id").first
#t_id = #tutorial_id.id.to_s
render json: #t_id
Following code is work for me
#tutorial_id = Demotutorial.where("job_id = ?", #job_id).select( "id").first
#t_id = #tutorial_id[0].id.to_s
render json: #t_id

Json and Ruby creating a string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a method that I can to push to a JSON call. The function looks like:
def my_function(name, text , id_person, id_company)
end
What I want is to get:
{"my_function":{"name":"names_value","text":"text_value ","id_person":"id_person_value","id_company":"id_company_value"}
Is there any easy way to do this?
The question is a bit unclear, but let me try with few answer possibilities
First of all this is not a JSON format
{"my_function":{"name":"names_value","text":"text_value ","id_person":"id_person_value","id_company":"id_company_value"}
There should not be double quote (") before semicolon (:)
obj = {
my_function:
{
name: "names_value",
text: "text_value",
id_person: "id_person_value",
id_company: "id_company_value"
}
}
If you want to get each of the data in that "JSON" format, you could do this
name = obj['my_function']['name'] #name_value
text = obj['my_function']['text'] #text_value
and so on...
But if you want to make a string data into JSON format, you could do this
def my_function(name_value, text_value, id_person_value, id_company_value)
{
my_function:
{
name: name_value,
text: text_value,
id_person: id_person_value,
id_company: id_company_value
}
}.to_json
end
I hope it's answering your question...

Get value of a key in hash of hash [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a hash e.g.
{{"badan"=>1, "bau"=>1, "China,"=>1, "pilot."=>1, "RT"=>1, "penentu"=>1, "merupakan"=>1, "China, test test bau"=>1, "satu"=>1, "merupakan salah salah satu"=>1, "RT #WOWFakta: #WOWFakta: Di"=>1, "Di"=>1, "seorang pilot."=>1, "kelulusan menjadi menjadi seorang"=>1, "seorang"=>1, "salah"=>1, "#WOWFakta:"=>1, "satu penentu penentu kelulusan"=>1, "Di Beijing Beijing China,"=>1, "menjadi"=>1, "kelulusan"=>1, "test"=>1, "bau badan badan merupakan"=>1, "Beijing"=>1}=>{"kebali"=>1, "kff"=>1, "box"=>1, "#zannahoctavia"=>1, "kebali kamu kamu ca?"=>1, "ca?"=>1, "kampus"=>1, "kantin"=>1, "kff hah hah goods"=>1, "#zannahoctavia haha haha kali"=>1, "aja"=>1, "box kemang kemang aja"=>1, "yaa"=>1, "china"=>1, "hah"=>1, "hah. Jadi Jadi kebali"=>1, "goods pw pw tapi"=>1, "aja di di kff"=>1, "di"=>1, "pw"=>1, "kemang"=>1, "haha"=>1, "kali kantin kantin kampus"=>1, "Jadi"=>1, "tapi"=>1, "tapi yaa yaa hah."=>1, "kampus china china box"=>1, "hah."=>1, "kamu"=>1, "kali"=>1}}
and I want to retrieve all keys' values in a very efficient way. How can I do this?
You can iterate your hash like that it contain hash within hash
h.each do |key, value|
puts key
value.values.each do |v|
puts v
end
end
your hash contains a key and a value that is also a hash. You can try the following
hash.flatten.map(&:keys).inject(:+)
or use flat_map
hash.flatten.flat_map(&:keys)

Resources