0
votes

I'm trying to get only one voice from the response of a script that I run inside AppleScript with

do shell script "<mycommand>"

Basically all I need is to store this "Ho1u8aA2p8jGuzEIZq8n" as a variable for later use.

Below is the full string I am receiving.

I think awk should be the solution, but unfortunately I had bad luck with it.

Any idea on how to achieve it?

Below is the full string I'm receiving that's a report from a new entry created in firebase.

{"id":"Ho1u8aA2p8jGuzEIZq8n","data":{"spa_id":"0","os":"10.14.6","name":"Spa_Name","activity":"0","spa_email":"[email protected]","serial":"C07XMRXWJYVW","nation":"Italy","spa_address":"Apliu Street 54, Hong Kong","ip_address":"192.168.2.1","global_unit_id":"0","city":"Perugia","branch":"Main","spa_unit_nr":"1","uid":"dev","spa_website":"www.spaname.com","continent":"Europe","spa_phone":"+852 3847569"}}
2

2 Answers

1
votes

This can be piped to the cut command, with double quotes as a field separator.

script | cut -d'"' -f3
0
votes

If any of the JSON values or keys were to contain a quotation mark. ", then using it as a delimiter to split the text with a tool such as cut would not work. Likewise, a comma--which is another tempting character to consider as a potential delimiter--presents the same issue. You also lose the key-value associations, which could be a handicap.

The script below should provide a more full-proof solution that works in more general situations, as it specifically caters to JSON strings, and converts them into native AppleScript data types (a record), so the key-value relationship is still there (albeit, unilateral).

set JSON to JSONStringToRecord from "{
    \"id\": \"Ho1u8aA2p8jGuzEIZq8n\",
    \"data\": {
        \"spa_id\": \"0\",
        \"os\": \"10.14.6\",
        \"name\": \"Spa_Name\",
        \"activity\": \"0\",
        \"spa_email\": \"[email protected]\",
        \"serial\": \"C07XMRXWJYVW\",
        \"nation\": \"Italy\",
        \"spa_address\": \"Apliu Street 54, Hong Kong\",
        \"ip_address\": \"192.168.2.1\",
        \"global_unit_id\": \"0\",
        \"city\": \"Perugia\",
        \"branch\": \"Main\",
        \"spa_unit_nr\": \"1\",
        \"uid\": \"dev\",
        \"spa_website\": \"www.spaname.com\",
        \"continent\": \"Europe\",
        \"spa_phone\": \"+852 3847569\"
    }
}"


on JSONStringToRecord from input as text
    local input

    script objc
        use framework "Foundation"
        to unwrap()
            tell the current application to tell every reference in ¬
                (NSJSONSerialization's JSONObjectWithData:((NSString's ¬
                    stringWithString:input)'s dataUsingEncoding:4) ¬
                options:0 |error|:(reference)) to if it ≠ {} ¬
                then return its item 1 as {record, list, text}
        end unwrap()
    end script

    unwrap() in objc
end JSONStringToRecord

I took your JSON string output and just formatted it for easier reading. But the variable JSON in the script above would, in your situation, look to get defined more like:

set JSONString to do shell script "<mycommand>"
set JSON to JSONStringToRecord from the JSONString

That ought to return the following record:

{
    |id|:"Ho1u8aA2p8jGuzEIZq8n",
    |data|:{
        spa_id:"0",
        os: "10.14.6",
        |name|: "Spa_Name",
        activity: "0",
        spa_email: "[email protected]",
        serial: "C07XMRXWJYVW",
        nation: "Italy",
        spa_address: "Apliu Street 54, Hong Kong",
        ip_address: "192.168.2.1",
        global_unit_id: "0",
        city: "Perugia",
        branch: "Main",
        spa_unit_nr: "1",
        uid: "dev",
        spa_website: "www.spaname.com",
        continent: "Europe",
        spa_phone: "+852 3847569"
    }
}

which is formatted for readability, and would look more minified in Script Editor, ie:

{|id|:"Ho1u8aA2p8jGuzEIZq8n",|data|:{spa_id:"0",os:"10.14.6",|name|:"Spa_Name",activity:"0",spa_email:"[email protected]",serial:"C07XMRXWJYVW",nation:"Italy",spa_address:"Apliu Street 54, Hong Kong",ip_address:"192.168.2.1",global_unit_id:"0",city:"Perugia",branch:"Main",spa_unit_nr:"1",uid:"dev",spa_website:"www.spaname.com",continent:"Europe",spa_phone:"+852 3847569"}}

This, clearly, looks very similar to the JSON string you started with, but it'll be clearer in Script Editor that it's not text, but rather a native data object, and the keys can now be accessed within AppleScript to isolate the values in the JSON record that you want. Therefore:

|id| of JSON

would get you "Ho1u8aA2p8jGuzEIZq8n". And to get one of the more nested items, you would do this:

ip_address of |data| of JSON

which would get you "192.168.2.1".