Asterisk 13 | Need help for String Manipulation

Hi

I know we shouldn’t be using Asterisk 13 but its for a legacy app and need some suggestion here. We are making an API call and lets say it returns following JSON -

{
  "status": true,
  "name": {
    "businessname": "baba black sheep",
    "housename": "johny johny"
  },
  "anotherstring": "Hello World"
}

I want to fetch fields from this json and create a new string like -

"baba black sheep"<444443@johny johny;cp=Hello World>

So basically i just need to fetch data from json and create a new string. If this would have been Asterisk 16+, i would have just used json function.

Can someone help me how can I achieve it in Asterisk 13

  1. Write an AGI in a language you are comfortable with that has a JSON parsing library.
  2. Write a Bash script and execute it with the SHELL function. You could just grep for the bits you want or use ‘jq’ to actually parse and return the bits. Using jq would be much less fragile.

Any example please,?

I would do it as an AGI, because, well, for a lot of reasons :slight_smile:

However, here’s a shell script example:

extensions.conf:

; test.sh parsing JSON                                                                                                                                                       
        same = n,                       set(json={\"status\": true,\"name\":{\"businessname\": \"baba black sheep\",\"housename\":\"johny johny\"},\"anotherstring\": \"Hell$
        same = n,                       set(bits=${SHELL(/var/lib/asterisk/agi-bin/test.sh "${json}")})
        same = n,                       verbose(1,The extracted bits are ${bits})
        same = n,                       hangup()

test.sh:

#!/bin/bash

# save the json to a variable
	json="$1"

# parse the bits we need from the json
	anotherstring=$(jq .anotherstring <<<"${json}")
	businessname=$(jq .name.businessname <<<"${json}")
	housename=$(jq .name.housename <<<"${json}")

# return the bits in a formatted string
	printf '%s<44443@%s;cp=%s>'\
		"${businessname}"\
		"${housename//\"}"\
		"${anotherstring//\"}"

# (end of test.sh)

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