-1

sample input- '{"name":"John", "age":30, "car":null}'

expected output- ['{"name":"John", "age":30, "car":null}']

  • The way the output is described isn't clear to me. It looks like you want an array with 1 element in it which is the string `'{"name":"John", "age":30, "car":null}'`, which is the same as your input string. Can you elaborate on what it is that you want in the output array? – Jeff Scott Brown Mar 16 '22 at 22:35
  • Perhaps JSON.parse / JSON.stringify will help you. Check this question out: https://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object – Apolymoxic Mar 17 '22 at 03:38
  • I want when ever any json object will come it will convent into json array @JeffScottBrown – shiavm Singh Mar 17 '22 at 05:55
  • still question is unclear. your expected output is an array of string... – daggett Mar 17 '22 at 10:05
  • i want when ever we got json if it starts with "{" then wanted to push "[" and "]" at the start and end of json. – shiavm Singh Mar 17 '22 at 11:54

1 Answers1

0

want when ever we got json if it starts with "{" then wanted to push "[" and "]" at the start and end of json.

If that is all you need then you could do this:

String input =  '{"name":"John", "age":30, "car":null}'

// ...

if(input.startsWith('{')) {
    input = "[${input}]"
}
Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47