【質問内容】php json の書き方の質問になるのですが、 元々 "zip01":"150", の......

【質問内容】php json の書き方の質問になるのですが、
元々 “zip01”:“150”, のようなシンプルなキーと値で書くところ、
値を加工する命令式を挿入するときはどうやって書けばよいのでしょうか?
文字列の最後から5文字をカットしたい場合。
$str = “150-0022”;//文字列
$cut = 5;//カットしたい文字数
$replace = substr( $str , 0 , strlen($str)-$cut );
echo $replace;//150
なのはわかったのですが、それをどう:“150”(変数だから “{zip1}” とかでしょうか)の代わりに置いたらいいかわかりません。
* Shopify から注文者が入力した送付先郵便番号が7桁まとめて送られて来て、
それを発送会社のAPIの仕様に合わせて3桁のzip1と4桁のzip2というように分けて(データを加工して、切り出して?)使わないといけません。

回答者
途中までの過程が全くわからないので、想定ですが、
3桁のzip1
4桁のzip2 の変数を作れば良いですか? であれば、、、
//郵便番号
$zipcode = “1898501”;
//最初から3文字分を取得する
$zip1 = substr($zipcode ,0,3);
//4文字目から最後まで取得する
$zip2 = substr($zipcode ,3);
こちらで問題ないかと。

質問者
早速ありがとうございます。
すごい初歩的な質問ですみません、
Bodyの中、
{
“zip01”:"{substr($shipping_address.zip ,0,3);}",
“zip02”:"{substr($shipping_address.zip ,4);}"
}
こんな感じですか(元の郵便番号がハイフン付きなので、5文字目から最後まで取得するには3を4に変えるのかなと思いました)?

回答者
Body というのが、断片的な情報で、どういう処理が良く分かってないのですが、
PHPだけでいうと、以下の通りの記載で問題ないです。

<?php //郵便番号 $zipcode = "123-4567"; //最初から3文字分を取得する $zip1 = substr($zipcode ,0,3); //5文字目から最後まで取得する $zip2 = substr($zipcode ,4); ?>

質問者
Parabolaのドキュメントで、
Sending a body in your API request
In Parabola, the body of the request will always be sent in JSON.
(中略)
Simple JSON looks like this:
{
“key1”: “value1”,
“key2”: “value2”,
“key3”: “value3”
}
But it can be nested and have other complications. Luckily, your API docs will most likely show you the JSON that needs to be sent. If you get stuck, reach out.
In most situations, you will want to send data from Parabola in the body of the request. To do so, you can use merge tags to specify where in your JSON blob in the body of the request data should be merged in.
Using merge tags in the “Send to an API” step
Merge tags can be added to the endpoint URL or the body of a request.
A request will be sent for every row of data you pass into this step, and you can use cell values to customize the URL or body for each row’s request. For example, if you have a column named id, you could use it in the endpoint URL by including {id} in it. Your URL would look like this:
http://third-party-api-goes-here.com/users/{id}
Similarly, you can add merge tags to the body.
{
“key1”: “{id}”,
“key2”: “{Name}”,
“key3”: “{Type}”
}
となっていて、製本直送のAPIドキュメントもサンプルは
{
“(キー)”: “(値)”
}
なので、そういう形式で書くのではないのですか?

回答者
Bodyというのは、Json形式のBodyのことですね。
私が、プログラミング詳しくないので
Jsonの中にPHP構文を入れるやり方は見たことないです ><
Parabolaは、1つずつのWorkFlowの中で処理が行われます。分割する処理は、
TranceFormの中に、Colum -Spilitがあるので、そちらで分割したあと、Jsonを出力する方が良いです。

一例ですが、こんな感じです。

Delimiter は “-” で分割してみます。

質問者
ありがとうございます。アタマ爆発しそうですが見てみます!

参考URL