Split variable containing JSON into two variables

Our dev who is not familiar with openwrt is trying to code something but he's not getting anywhere.

He has this kind of sting for example;

[{"mn":"001","sn":"001","fr":"00.69","hr":"01.00"}]
[{"1":"26.75","2":"42.16","3":"9","4":"827.301","5":"78.53","6":"2","7":"415","8":"73.10","9":"21.71","10":"0","11":"0.0","12":"0.0","13":"0.000"}]

which is mainly two JSON separated by a newline that I get and place into a variable.
I need to split these int two bash variables.
I tried the following but with no result

    ## Execute command
    what="/etc/pgm_that_sends_back_the string"
    which_command=$($what)
    echo "FULL: $which_command"
    ## Split string into an array
    IFS="\n"
    read -ra tmp <<< "$which_command"
    IFS=' '
    ## Assign array elements to single variables
    json_0=${tmp[0]}
    json_1=${tmp[1]}

The result is always weird since it is interpreting "\n" as the letter "n"
Any idea of how to do it using openwrt bash?

His reply,

This is unrelated to the problem i am having with the JSON sent by the program.

try with IFS=$'\n'

# Option 1
A1="${A%%$'\n'*}"
A2="${A##*$'\n'}"

# Option 2
A1="$(echo "${A}" \
| sed -n -e "1p")"
A2="$(echo "${A}" \
| sed -n -e "2p")"

# Option 3
I="1"
while read -r "A${I}"
do let I++
done << EOI
${A}
EOI

Ok, I passed that on and will update as soon as i get feedback.

I'm told this did the trick.

The following script work fine :

#!/bin/bash


delimiter=
```\n'
str=$which_command$delimiter
array=();
while [[ $s ]]; do
  array+=( "${s%%"$delimiter"*}" );
  s=${s#*"$delimiter"};
done;
echo${array[0]}
echo${array[1]}

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.