I have a problem with converting my response XML to Json in CA API Gateway.
My xml like ;
<Result>
<MetaDataItem>
<ComponentType>TEXTBOX</ComponentType>
<DataType>System.String</DataType>
</MetaDataItem>
<MetaDataItem>
<ComponentType>DATETIME</ComponentType>
<DataType>System.String</DataType>
</MetaDataItem>
<Result>
When I try to get the inner child elements with xpath;
The response is like;
{
"result":{
"resultCode":"1000",
"resultMessage":""
"result":"TEXTBOXSystem.StringDATETIMESystem.String,
}
}
It just returns just the first MetaDataItems values as string.I can not convert this result string into a json.
I want result json like;
"MetaDataItem": [
{
"ComponentType":"TEXTBOX",
"DataType" : "System.String"
}
,{
"ComponentType":"DATETIME",
"DataType" : "System.String"
}
]
Are there anyone who get the idea for CA API Gateway XML to JSON transformation?
I don't know that environment but if all you have is XPath 2.0 then
concat('[',
string-join(
for $item in //MetaDataItem
return concat('{', string-join(for $c in $item/* return concat('"', local-name($c), '":"', $c ,'"'), ', '), '}'), ', '), ']')
as the XPath 2.0 expression should give the string [{"ComponentType":"TEXTBOX", "DataType":"System.String"}, {"ComponentType":"DATETIME", "DataType":"System.String"}], perhaps if you additionally set the "variable prefix" to MetaDataItem the result is closer to what you want.
Related
I was trying to format my json in jsonformatter.org and I'm not sure why I'm getting the below error
Parse error on line 1:
...","topBrand":false}{"_id":{"$oid":"601c
----------------------^
Expecting 'EOF', '}', ',', ']', got '{'
[error][1]
Sample JSON:
{"_id":{"$oid":"601ac115be37ce2ead437551"},"barcode":"511111019862","category":"Baking","categoryCode":"BAKING","cpg":{"$id":{"$oid":"601ac114be37ce2ead437550"},"$ref":"Cogs"},"name":"test brand #1612366101024","topBrand":false}
{"_id":{"$oid":"601c5460be37ce2ead43755f"},"barcode":"511111519928","brandCode":"BUCKS","category":"Beverages","categoryCode":"BEVERAGES","cpg":{"$id":{"$oid":"5332f5fbe4b03c9a25efd0ba"},"$ref":"Cogs"},"name":"Starbucks","topBrand":false}
The JSON you posted contains two separate JSON documents, and is thus invalid. That's what the error is complaining about, it expected EOF (i.e. End of File), but it instead got the opening-brace from the second JSON document.
Either format them one at a time, or merge them into one document somehow.
For example, you could turn it into a list:
{
"data": [
{ ... },
{ ... }
]
}
Or a larger compound object:
{
"data1": { ... },
"data2": { ... }
}
I am not able to twist my head into understanding how to get Powershell to loop the entire JSON Structure, it wont' loop the System.Object[]
$x = ConvertFrom-Json '{
"Car companies": {
"Name of Company": "Ford",
"Cars": [{
"Name of car": "Ranger",
"Config": "Pickup"
},
{
"Name of car": "Puma",
"Config": "Hatchback"
}]
}
}'
foreach( $rootProperty in #($x.psobject.properties | where-object {$_.MemberType -eq "NoteProperty"}) ) {
write-host " - '$($rootProperty.Name)' = '$($rootProperty.Value)'"
foreach( $childProperty in #($rootProperty.Value.psobject.properties ) ) {
write-host "'$($childProperty.Name)' = '$($childProperty.Value)'"
}
}
Outut I get now is just
- 'Brand' = '#{Name of Brand=Ford; Cars=System.Object[]}'
Name of Brand' = 'Ford'
Cars' = ' '
...as a follop How to iterate through a unknown JSON data/object?
tl;dr
You're seeing a bug that unexpectedly string-expands the Cars property value's array elements to the empty string.
A simple workaround - for display purposes only - is to pipe the property value to Out-String to get the usual display representation:
"'$($childProperty.Name)' = '$($childProperty.Value | Out-String)'"
You're seeing a bug in how arrays of [pscustomobject] instances are stringified (as of PowerShell Core 7.0.0-preview.6):
Generally, PowerShell arrays are stringified by joining the stringified element representations with the separator specified in the $OFS preference variable, which defaults to a space char.
Normally, [pscustomobject] instances have a string representation that resembles a hashtable literal (but isn't one); e.g.:
PS> $custObj = [pscustomobject] #{ foo = 'bar' }; "$custObj"
#{foo=bar} # string representation that *resembles* a hashtable literal
Unexpectedly - and this is the bug - when custom objects are the elements of an array, they stringify to the empty string, which is what you saw:
PS> $custObj = [pscustomobject] #{ foo = 'bar' }; $arr = $custObj, $custObj; "[$arr]"
[ ] # !! Bug: custom objects stringified to empty strings, joined with a space
This is an indirect manifestation of a long-standing bug reported in this GitHub issue: that is, elements of an array being stringified are stringified by calls to their .ToString() method, and calling .ToString() on custom objects unexpectedly yields the empty string (unlike the string representation you get when you directly reference a single custom object in an expandable string, as shown above).
I have the below requirement.
Input is
{ "packageConfiguration": [
{
"packageId": [
"AIM_PACKAGE"
],
"component": [
"Handbook"
],
"fieldName": [
"Upload Handbook Document"
],
"assetUrl": [
"sflydamlocation.handbookfilename.pdf"
]
}
]}
I need to convert above json array into this output format:
{
"pakage": ""packageId":"AIM_PACKAGE", "component":"Handbook", "fieldName":"Upload Handbook Document","assetUrl":"sflydamlocation.handbookfilename.pdf""
}
You can do that treating all fields as strings, however note that:
The inner quotes must be escaped. Otherwise the output is not valid JSON.
Take in account that the value of "package" is not really valid JSON either, in case you want to parse it. It should an object (eg " { \"package\":... }")
This script expects all the arrays to have exactly 1 element. More elements are ignored and less could give an error. This is not a very robust design.
Script (not recommended):
%dw 2.0
output application/json
---
package: using (pc = payload.packageConfiguration[0]) (
" \"packageId\": \"$(pc.packageId[0])\", " ++
" \"component\": \"$(pc.component[0])\" " ++
" \"fieldName\": \"$(pc.fieldName[0])\" " ++
" \"assetUrl\": \"$(pc.assetUrl[0])\" "
)
Output:
{
"package": " \"packageId\": \"AIM_PACKAGE\", \"component\": \"Handbook\" \"fieldName\": \"Upload Handbook Document\" \"assetUrl\": \"sflydamlocation.handbookfilename.pdf\" "
}
This is an ugly string concatenation. Instead I would suggest to just write the desired output as a JSON object.
Script (recommended):
%dw 2.0
output application/dw
var pc = payload.packageConfiguration[0]
---
package:
write({
packageId: pc.packageId[0],
component: pc.component[0],
fieldName: pc.fieldName[0],
assetUrl: pc.assetUrl[0]
}, "application/json") replace /\n/ with ""
Output
{
"package": "{ \"packageId\": \"AIM_PACKAGE\", \"component\": \"Handbook\", \"fieldName\": \"Upload Handbook Document\", \"assetUrl\": \"sflydamlocation.handbookfilename.pdf\"}"
}
The second script is much cleaner, less error prone and returns an escaped JSON object that you could unescape to use as JSON.
Something like this should work, unless you require something more flexible. I'm assuming you're working w/ Mule3/DW1:
%dw 1.0
%output application/json
%var packageConfig = payload.packageConfiguration[0]
---
{
package: packageConfig mapObject ((value, key) -> {
(key): value[0]
})
}
I have a Json expression that contains values with "." and # like this
{"queued":"C1F","messageid":"dfs.jfdsf#sdf.abc.fr"}
that doesn't get processed by HTTP POST request , and it's give me this result :
"code":400,"message":"Unable to process JSON
PS: my web server is created with dropWizard in Intellij IDEA
how can I resolve this problem
EDIT: this is the code used in perl
my $queued=$1; my $messageid=$2 ;
my $json= "{\"queued\":\"$queued\",\"messageid\":\"$messageid\"}";
$req1->content($json);
my $response=$ua->request($req1);
if ($response->is_success) {
my $message =$response->decoded_content ;
print "resultat : $message \n";
}
else {
print "erreur", $response->code, " ", $response->message, "\n" ;
}
It would be less error-prone to use the JSON library to build your JSON string
use JSON 'to_json';
my $json = to_json({ queued => $queued, messageid => $messageid });
I try to find out why my JSON is not legal.
I use this site: http://jsonlint.com/
The first example which is good is:
{
"data": 1290,
"value": "a"
}
The second which is not good is:
{
"data": 1290,
"value": "a"
}
I dont understand why the second one does not work. Its the same as the first one.
EDIT
I found this sings at the end of the string.
How to remove them using PHP? the string source is from php.
There might be some additional output after you output your json. Try trimming output and terminating further execution.
$json = array('data' => 1290, 'value' => 'a');
echo json_encode($json);
die();