-2

How to create array or store rest api response to array

rest API response

{"functionality":[],"subfunctionality":   [{"id":1,"title":"a1","description":"sample}, 
{"id":2,"title":"a2","description":"sample}, 
{"id":3,"title":"a3","description":"sample}

i wanted the results to be stored in array some thing like below

a[0]={"id":1,"title":"a1","description":"sample}
a[1]={"id":2,"title":"a2","description":"sample}
a[2]={"id":3,"title":"a3","description":"sample}]}
testerBDD
  • 255
  • 3
  • 18
  • 1
    "How to create array or store rest api response to array" – You do it by writing a program which does that. If you have a problem with your program, carefully read the documentation of all the methods, classes, modules, and libraries you are using, write tests for your programs, trace the execution with pen and paper, single-step it in a debugger, then sleep on it, start again from the beginning, sleep on it again, and *then and only then* narrow your problem down to a concise, focused, simple, short, reproducible [mcve] and ask a specific, focused, narrow question on [so]. – Jörg W Mittag Nov 11 '18 at 09:11
  • Thanks for the comment but i believe the question is straight forward, specific, focused, narrow question and raised it as i didn't get the right documentation or answer to it and I do think the question is reproducible with required information @JörgWMittag – testerBDD Nov 12 '18 at 17:08
  • The question is missing the most important thing to help you fix your code: your code. – Jörg W Mittag Nov 12 '18 at 21:23

2 Answers2

0
arrayName= $responsebody.split(/{(.*?)}/)

worked for me.

Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100
testerBDD
  • 255
  • 3
  • 18
  • If it's a valid JSON, why not, `a = JSON.parse($responsebody)['subfunctionality']`. Doing with regex can get you in tough spot with corner cases such as body including characters like '{}' etc. – kiddorails Nov 11 '18 at 05:13
0

I assume that what you are showing as the "rest API response". If so you need to clean it up so that it is a valid JSON string, convert it to a hash and then extract the array you want.

str =<<_
{ "functionality":[], "subfunctionality": [{"id":1, "title":"a1", "description":"sample},
{"id":2, "title":"a2", "description":"sample}, 
{"id":3, "title":"a3", "description":"sample}
_

require 'json'

a = JSON.parse(str.gsub("\"sample", "\"sample\"") << ']}')["subfunctionality"]
  #=> [{"id"=>1, "title"=>"a1", "description"=>"sample"},
  #    {"id"=>2, "title"=>"a2", "description"=>"sample"},
  #    {"id"=>3, "title"=>"a3", "description"=>"sample"}]

The steps are as follows.

s = str.gsub("\"sample", "\"sample\"") << ']}'
  #=> "{ \"functionality\":[], \"subfunctionality\": [{\"id\":1, \"title\":\"a1\", 
  #    \"description\":\"sample\"},\n{\"id\":2, \"title\":\"a2\",
  #    \"description\":\"sample\"}, \n{\"id\":3, \"title\":\"a3\",
  #    \"description\":\"sample\"}\n]}"
h = JSON.parse(s)
  #=> {"functionality"=>[],
  #    "subfunctionality"=>[{"id"=>1, "title"=>"a1", "description"=>"sample"},
  #                         {"id"=>2, "title"=>"a2", "description"=>"sample"},
  #                         {"id"=>3, "title"=>"a3", "description"=>"sample"}]}
h["subfunctionality"]
  #=> (return value shown above)

Note that I've broken the string s in various places to make it easier to read.

Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100