unencode json in Zapier - json

I'm trying to make a Zapier zap to get a JSON from Olark.
This is the sample JSON Olark sends...
{
"kind": "Conversation",
"id": "EV695BI2930A6XMO32886MPT899443414",
"tags": ["olark", "customer"],
"items": [{
"kind": "MessageToVisitor",
"nickname": "John",
"timestamp": "1307116657.1",
"body": "Hi there. Need any help?",
"operatorId": "1234"
},
{
"kind": "MessageToOperator",
"nickname": "Bob",
"timestamp": "1307116661.25",
"body": "Yes, please help me with billing."
}],
"visitor": {
"kind": "Visitor",
"id": "9QRF9YWM5XW3ZSU7P9CGWRU89944341",
"fullName": "Bob Doe",
"emailAddress": "bob#example.com",
"phoneNumber": "(555) 555-5555",
"city": "Palo Alto",
"region": "CA",
"country": "United State",
"countryCode": "US",
"organization": "Widgets Inc.",
"ip": "123.4.56.78",
"browser": "Chrome 12.1",
"operatingSystem": "Windows",
"conversationBeginPage": "http://www.example.com/path",
"customFields": {
"myInternalCustomerId": "12341234",
"favoriteColor": "blue"
},
"chat_feedback": {
"comments": "Very helpful, thanks",
"friendliness": 5,
"knowledge": 5,
"overall_chat": 5,
"responsiveness": 5
}
},
"operators": {
"1234": {
"kind": "Operator",
"id": "1234",
"username": "jdoe",
"nickname": "John",
"emailAddress": "john#example.com"
}
},
"groups": [{
"name": "My Sales Group",
"id": "0123456789abcdef",
"kind": "Group"
}]
}
I can get what I want -- the email -- like this...
var obj = {};
var data = {};
data = JSON.parse(inputData.data);
obj.value = data.visitor["emailAddress"];
return obj;
However, the live data that comes from Olark is an encoded version of the JSON. It looks like this...
raw_body
data=%7B%22kind%22%3A+%22Conversation%22%2C+%22id%22%3A+%224pkhSGlkBYHz0gw83L6TF0UBa6rA39Bo%22%2C+%22manuallySubmitted%22%3A+false%2C+%22items%22%3A+%5B%7B%22kind%22%3A+%22MessageToOperator%22%2C+%22nickname%22%3A+%22juliachevron%40gmail.com%22%2C+%22timestamp%22%3A+%221588965389.388434%22%2C+%22body%22%3A+%22Hi.+I+have+a+stimulator+and+it+is+no+longer+working+at+the+higher+levels%22%2C+%22visitor_nickname%22%3A+%22juliachevron%40gmail.com%22%7D%2C+%7B%22kind%22%3A+%22MessageToVisitor%22%2C+%22nickname%22%3A+%22Christian%22%2C+%22operatorId%22%3A+%22663710%22%2C+%22timestamp%22%3A+%221588965397.901964%22%2C+%22body%22%3A+%22Hi+there%22%7D%2C+%7B%22kind%22%3A+%22MessageToVisitor%22%2C+%22nickname%22%3A+%22Christian%22%2C+%22operatorId%22%3A+%22663710%22%2C+%22timestamp%22%3A+%221588965408.398821%22%2C+%22body%22%3A+%22Can+you+explain+more%3F%22%7D%2C+%7B%22kind%22%3A+%22MessageToOperator%22%2C+%22nickname%22%3A+%22juliachevron%40gmail.com%22%2C+%22timestamp%22%3A+%221588965445.279921%22%2C+%22body%22%3A+%22it+is+only+flashing+up+to+the+2+and+sometimes+3%22%2C+%22visitor_nickname%22%3A+%22juliachevron%40gmail.com%22%7D%2C+%7B%22kind%22%3A+%22MessageToVisitor%22%2C+%22nickname%22%3A+%22Christian%22%2C+%22operatorId%22%3A+%22663710%22%2C+%22timestamp%22%3A+%221588965482.985280%22%2C+%22body%22%3A+%22Try+our+%5C%22oreo%5C%22+troubleshooting+test+from+page+12+of+the+User+Guide%22%7D%2C+%7B%22kind%22%3A+%22MessageToVisitor%22%2C+%22nickname%22%3A+%22Christian%22%2C+%22operatorId%22%3A+%22663710%22%2C+%22timestamp%22%3A+%221588965488.741729%22%2C+%22body%22%3A+%22It+involves+you+pressing+the+wet+sponges+into+each+other+as+you+turn+the+device+to+the+max+setting%22%7D%2C+%7B%22kind%22%3A+%22MessageToOperator%22%2C+%22nickname%22%3A+%22juliachevron%40gmail.com%22%2C+%22timestamp%22%3A+%221588965539.836595%22%2C+%22body%22%3A+%22thank+you%21%22%2C+%22visitor_nickname%22%3A+%22juliachevron%40gmail.com%22%7D%2C+%7B%22kind%22%3A+%22MessageToVisitor%22%2C+%22nickname%22%3A+%22Christian%22%2C+%22operatorId%22%3A+%22663710%22%2C+%22timestamp%22%3A+%221588965583.359044%22%2C+%22body%22%3A+%22This+test+is+to+make+sure+the+device+is+functioning+correctly%22%7D%2C+%7B%22kind%22%3A+%22MessageToVisitor%22%2C+%22nickname%22%3A+%22Christian%22%2C+%22operatorId%22%3A+%22663710%22%2C+%22timestamp%22%3A+%221588965619.075057%22%2C+%22body%22%3A+%22If+you+are+not+getting+to+the+higher+levels+when+the+sponges+are+on+your+temples%2C+it+may+mean+that+you+need+to+use+more+water%2C+tighten+the+headband+a+little+more%2C+or+that+you+need+to+replace+the+sponges%22%7D%2C+%7B%22kind%22%3A+%22MessageToOperator%22%2C+%22nickname%22%3A+%22juliachevron%40gmail.com%22%2C+%22timestamp%22%3A+%221588965698.050212%22%2C+%22body%22%3A+%22it+worked.+thanks%22%2C+%22visitor_nickname%22%3A+%22juliachevron%40gmail.com%22%7D%2C+%7B%22kind%22%3A+%22MessageToVisitor%22%2C+%22nickname%22%3A+%22Christian%22%2C+%22operatorId%22%3A+%22663710%22%2C+%22timestamp%22%3A+%221588965702.713077%22%2C+%22body%22%3A+%22You%27re+very+welcome.%22%7D%5D%2C+%22tags%22%3A+%5B%5D%2C+%22visitor%22%3A+%7B%22kind%22%3A+%22Visitor%22%2C+%22id%22%3A+%22YN7k5dd6nGH52Lvd3L6TF0VBb03B3roI%22%2C+%22fullName%22%3A+%22juliachevron%40gmail.com%22%2C+%22emailAddress%22%3A+%22juliachevron%40gmail.com%22%2C+%22ip%22%3A+%22%22%2C+%22city%22%3A+%22Oak+Park%22%2C+%22region%22%3A+%22IL%22%2C+%22country%22%3A+%22United+States%22%2C+%22countryCode%22%3A+%22US%22%2C+%22organization%22%3A+%22Comcast+Cable%22%2C+%22browser%22%3A+%22Chrome+80.0.3987.149%22%2C+%22operatingSystem%22%3A+%22Macintosh%22%2C+%22referrer%22%3A+%22https%3A%2F%2Fwww.google.com%2F%22%2C+%22conversationBeginPage%22%3A+%22https%3A%2F%2Fwww.fisherwallace.com%2F%22%2C+%22chat_feedback%22%3A+%7B%7D%7D%2C+%22operators%22%3A+%7B%22663710%22%3A+%7B%22kind%22%3A+%22Operator%22%2C+%22id%22%3A+%22663710%22%2C+%22nickname%22%3A+%22Christian%22%2C+%22emailAddress%22%3A+%22christian%40fisherwallace.com%22%2C+%22username%22%3A+%22fisherwallace%22%7D%7D%7D
And I get "SyntaxError: Unexpected token d in JSON at position 0" when I try to parse it with the code that works on the sample JSON.
Is there a way in Zapier to unencode the JSON?

There sure is! What you're seeing is percent encoding. Node.js can decode that, but to get valid json, you'll also need to replace the + characters with spaces.
Try this:
const inputWithReplacedSpaces = inputData.data.replace(/\+/g, '%20') // '%20' is a space
const jsonStr = decodeURIComponent(inputWithReplacedSpaces)
const data = JSON.parse(jsonStr)
return { email: data.visitor.emailAddress }

There were two issues. See screenshot.
I needed to set the Import Data to "data" in the Custom Run Javascript step. And also remove the "data=" from the string at the beginning of the Raw Body.

Related

Parse unstructured json in golang

Is there any solution to parse an unstructured json(text) data?
below is a sample response of a web requst that i want to parse and access data (the inner list)
res,err := http.Get("url_of_server")
[[
{
"id": "1",
"text": "sample text",
"user": {
"user_id": "1",
"username": "user1"
},
"created_at_utc": "2022-12-20T16:38:06+00:00",
"status": "Active"
},
{
"id": "2",
"text": "sample text",
"user": {
"user_id": "2",
"username": "user2"
},
"created_at_utc": "2022-12-01T10:15:00+00:00",
"status": "Active"
}
],
"{"code": "hsdvnkvuahudvhafdlfv",
"is_updated": true}",
null
]
what i want to get is:
[
{
"id": "1",
"text": "sample text",
"user": {
"user_id": "1",
"username": "user1"
},
"created_at_utc": "2022-12-20T16:38:06+00:00",
"status": "Active"
},
{
"id": "2",
"text": "sample text",
"user": {
"user_id": "2",
"username": "user2"
},
"created_at_utc": "2022-12-01T10:15:00+00:00",
"status": "Active"
}
]
in python it is possible by easily using res.json()[0]
I have tried using json.Unmarshal() to a map and also struct but does not work,
i don't know how to get rid of this part of response:
"{"code": "hsdvnkvuahudvhafdlfv",
"is_updated": true}",
null
Declare a type for the items:
type Item struct {
ID string `json:"id"`
Text string `json:"text"`
User struct {
UserID string `json:"user_id"`
Username string `json:"username"`
} `json:"user"`
CreatedAtUtc time.Time `json:"created_at_utc"`
Status string `json:"status"`
}
Declare a slice of the items:
var items []Item
Declare a slice representing the entire JSON thing. The first element is the items.
var v = []any{&items}
Unmarshal to v. The items slice will have the values that you are looking for. The second and third elements of v will contain the values you want to ignore.
err := json.Unmarshal(data, &v)
Run the code in the GoLang PlayGround.
Go's standard JSON library is not as flexible as others when it comes to dealing with unexpected or uncontrolled input.
A great alternative is tidwall's gjson.
Example code with gjson:
package main
import (
"fmt"
"github.com/tidwall/gjson"
)
const textInput = `[[
{
"id": "1",
"text": "sample text",
"user": {
"user_id": "1",
"username": "user1"
},
"created_at_utc": "2022-12-20T16:38:06+00:00",
"status": "Active"
},
{
"id": "2",
"text": "sample text",
"user": {
"user_id": "2",
"username": "user2"
},
"created_at_utc": "2022-12-01T10:15:00+00:00",
"status": "Active"
}
],
"{"code": "hsdvnkvuahudvhafdlfv",
"is_updated": true}",
null
]`
func main() {
jsonBody := gjson.Parse(textInput)
fmt.Println(jsonBody.Get("0"))
}

Add a new element/field to every JSON array/record in an existing JSON file - python 3

I have JSON files that consists of 10+ records, like the one below.
Example:
[
{
"ID": null,
"entity": "xxx",
"Name": "Abc",
},
{
"ID": null,
"entity": "yyy",
"Name": "efg",
}
]
I want to add a new element "Date": "xx/xx/xx" to every record, like this:
[
{
"ID": null,
"entity": "xxx",
"Name": "Abc",
"Date": "xx/xx/xx"
},
{
"ID": null,
"entity": "yyy",
"Name": "efg",
"Date": "xx/xx/xx"
}
]
The order does not matter.
So far I have something like this:
date = 'xx-xx-xx'
def file(data, json):
with pathlib.Path(json).open('r+') as upfile:
fdata = json.load(upfile)
fdata.append(data)
upfile.seek(0)
json.dump(fdata, updfile)
...
new_ele = {'Date: ': date}
file(new_ele, file.json)
But my result is like this:
[
{
"ID": null,
"entity": "xxx",
"Name": "Abc",
},
{
"ID": null,
"entity": "yyy",
"Name": "efg",
},
{
"Date": "xx-xx-xx"
}
]
How can I get the Date into every single record?
Thank you
Once imported, your json is a list of dictionaries. So go through all list elements (which are dictionaries) and update them with the update method:
def update_json(data, json_path):
with pathlib.Path(json_path).open('r+') as upfile:
fdata = json.load(upfile)
for elt in fdata:
elt.update(data)
upfile.seek(0)
json.dump(fdata, upfile)

Extract address from Google People JSON object in Podio

I am trying to extract address data from a Google People API call within Podio Workflow Automation. I am successfully extracting all other data except the address.
The situation - if I request names only, I can extract each of the separate name fields, but if I add addresses into the request, I can no longer extract the name fields, or the address fields. If I request only address, i cannot extract address fields.
For some reason including the address data seems to break the JSON..
Request for name data only and I get:
{
"resourceName": "people/c2138163302931177819",
"etag": "%EgUBAi43PRoEAQIFByIMT1hSSUpZWTVuMFk9",
"names": [
{
"metadata": {
"primary": true,
"source": {
"type": "CONTACT",
"id": "1dac48320efd215b"
}
},
"displayName": "xx xxxxx xxxxxx",
"familyName": "xxxxxx",
"givenName": "xxxxx",
"honorificPrefix": "xx",
"displayNameLastFirst": "xxxxxx, xx xxxxx",
"unstructuredName": "xx xxxxx xxxxxx"
}
]
}
Request for name and address data and I get:
{
"resourceName": "people/c2138163302931177819",
"etag": "%EggBAgwQLjc9QBoEAQIFByIMT1hSSUpZWTVuMFk9",
"names": [
{
"metadata": {
"primary": true,
"source": {
"type": "CONTACT",
"id": "1dac48320efd215b"
}
},
"displayName": "xx xxxxx xxxxxx",
"familyName": "xxxxxx",
"givenName": "xxxxx",
"honorificPrefix": "xx",
"displayNameLastFirst": "xxxxxx, xx xxxxxx",
"unstructuredName": "xx xxxxx xxxxxx"
}
],
"addresses": [
{
"metadata": {
"primary": true,
"source": {
"type": "CONTACT",
"id": "1dac48320efd215b"
}
},
"formattedValue": "xxxxxxx\nSouthampton, England xxxxxxx\nUnited Kingdom",
"streetAddress": "xxxxxxx",
"city": "Southampton",
"region": "England",
"postalCode": "xxxxxxx",
"country": "United Kingdom",
"countryCode": "GB"
}
]
}
For me this looks no different in structure, however when I've changed it to code on here it's not recognising the latter one correctly..
In Podio I am base64 encoding the response and then as an example to extract first name I use:
json_decode(base64_decode([(Variable) google_array]))->{'names'}[0]->{'givenName'}
For City I'm using
json_decode(base64_decode([(Variable) google_array]))->{'addresses'}[0]->{'city'}
Extracting the name works fine if I only request name details, but not if it includes address details as well. It is only address that seems to create this problem. I cannot extract any address details.
Sounds like your json_decode (or the encode) doesn't work well with those "\n" line breaks.
See this discussion about json_decode in PHP 7: New lines and tabs in json_decode() (PHP 7)
use str_replace to remove "\n" from JSON response before base64 encoding

Jmeter: JSON response manipulation and passing to the next http request

I've got the response from HTTP GET request as JSON file and I want to use that JSON and pass it to the next HTTP request. I got the following response data
{
"apiInfo": {
"id": "23143",
"name": "bookkeeping",
"state": "used",
"data": "15893712000000"
},
"apiDetails": [
{
"bookName": "abc",
"state": "old",
"noOfTimesUsed": "53"
"additionalParam"{
"name": "abc",
"id": "123"
}
},
{
"bookName": "def",
"state": "new",
"noOfTimesUsed": "5",
"action": "keep"
"additionalParam"{
"name": "def",
"id": "456"
}
},
{
"bookName": "xyz",
"state": "avg",
"noOfTimesUsed": "23"
"additionalParam"{
"name": "ghi",
"id": "789"
}
},
{
"bookName": "pqr",
"state": "old",
"noOfTimesUsed": "75",
"action": "discard"
"additionalParam"{
"name": "jkl",
"id": "012"
}
}
]
}
I want to use "apiInfo" & "apiDetails" part from the JSON response and manipulate its data. As you can notice, some array field have attribute "action" in it and some one doesn't. I want to make sure all the field in the array have this data and is assigned as ' "action":"keep" '. Also, I want to add "id" from apiInfo & "name" from additionalParams from apiDetails itself. The end result I want is somewhat like this
"apiDetails": [
{
"id": "23143",
"bookName": "abc",
"state": "old",
"noOfTimesUsed": "53",
"action": "keep",
"name":"abc"
},
{
"id": "23143",
"bookName": "def",
"state": "new",
"noOfTimesUsed": "5",
"action": "keep",
"name":"def"
},
{
"id": "23143",
"bookName": "xyz",
"state": "avg",
"noOfTimesUsed": "23",
"action": "keep",
"name":"ghi"
},
{
"id": "23143",
"bookName": "pqr",
"state": "old",
"noOfTimesUsed": "75",
"action": "keep",
"name":"jkl"
}
]
I've been trying to use JSR223 sampler and have been struggling with it. It's bit complicated and I need help. P.S.: I've tried using javascript code to manipulate the results as desired but have been unsuccessful.
Please help.
Thanks, Sid
Add JSR223 PostProcessor as a child of the request which returns the above JSON
Put the following code into "Script" area:
def apiDetails = new groovy.json.JsonSlurper().parse(prev.getResponseData()).apiDetails
apiDetails.each { apiDetail ->
apiDetail.put('action', 'keep')
}
vars.put('request', new groovy.json.JsonBuilder(apidetails: apiDetails.collect()).toPrettyString())
That's it, you should be able to refer the generated request as ${request} where required
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It

insert the array into the database

I am getting this result when I am using graph api . it is in array format
{
"id": "216805086",
"name": "raj sharma",
"first_name": "raj ",
"last_name": "sharma",
"link": "https://www.facebook.com/raj.sharma.5",
"username": "raj .sharma.5",
"favorite_teams": [
{
"id": "198358615428",
"name": "Mumbai Indians"
},
{
"id": "190313434323691",
"name": "Indian Cricket Team"
}
],
"favorite_athletes": [
{
"id": "100787839962234",
"name": "Saina Nehwal"
}
],
"gender": "male",
"email": "raj.discoverme#gmail.com",
"timezone": 5.5,
"locale": "en_GB",
"verified": true,
"updated_time": "2013-08-13T06:01:17+0000"
}
I am working in a php language and phpmyadmin database . Now i want to insert the array into my database . Should i make a column for id , name , first_name ,last_name,link,favorite_teams etc or should i make a one column for all of this ........
how toinsert tha array into the database
Actually this is not an array. This is JSON. In JSON there are two formats,
JSONArray [ ]
JSONObject { }
You are getting the JSONObject as your output. There is a function in PHP callerd JSONDecode.
Go through this you will get idea.
Storing facebook app data in a database is against Facebook policy http://developers.facebook.com/policy/
$data = '{
"id": "216805086",
"name": "raj sharma",
"first_name": "raj ",
"last_name": "sharma",
"link": "https://www.facebook.com/raj.sharma.5",
"username": "raj .sharma.5",
"favorite_teams": [
{
"id": "198358615428",
"name": "Mumbai Indians"
},
{
"id": "190313434323691",
"name": "Indian Cricket Team"
}
],
"favorite_athletes": [
{
"id": "100787839962234",
"name": "Saina Nehwal"
}
],
"gender": "male",
"email": "raj.discoverme#gmail.com",
"timezone": 5.5,
"locale": "en_GB",
"verified": true,
"updated_time": "2013-08-13T06:01:17+0000"
}';
//decode to get as php variable
$values = json_decode($data,true); //true to decode as a array not an object
$sql = "INSERT INTO TableName (id,name,first_name,last_name,link,username)
VALUES ('".$values['id']."','".$values['name']."','".$values['first_name']."','".$values['last_name']."','".$values['link']."','".$values['username']."')";
mysql_query($sql);
Json_decode() takes a JSON encoded string and converts it into a PHP variable.