Json output string into an array or list - json

Hi i am trying to set json output to an array or list, but receiving an error
"cannot convert from system.collections.generic.list to String[]"
String received in paramValue-
{"0":["1234","2222","4321","211000","90024","12","2121","322223","2332","3232"],"1":["0856","6040222","175002","23572","","","","","",""]}
List<string> strlist;
strlist = new List<string>();
var jr = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(paramValue);
foreach (var item in jr)
{
//Need some thing like this
// strList.Add(item)
}
In order to take "item" values in the List, I also tried item.Value.CopyTo(strlist); but got the error "cannot convert from system.collections.generic.list to String[]".
Actually i need the "item" values to be stored in List, so that i can pass into another functions.
Thanks

When you iterate over the dictionary you have the List<string> in the value, so you can not add that value array using add to the List<string> strlist; add expect it to be a single value not multiple, as you want to add multiple values you can use AddRange.
like this
foreach (var item in jr)
{
//Need some thing like this
strlist.AddRange(item.Value);
}
if you want to use short-one then use
strlist.AddRange(jr.SelectMany(a => a.Value));

Related

store string into array

In MySQL, I have a field name postcodes and the type is LONGTEXT. It stores several postcodes being separated by comma. How would I retrieve that and store it as an array for other use ?
you can use the PHP method explode().
one think you can't do, is to do a where x = x on it in the database.
In the model, you can set the mutator methods:
public function getPostcodesAttribute($value) {
return explode(',',$value);
}
public function setPostcodesAttribute($value) {
$this->attributes['postcodes'] = implode(',',$value);
}
Lets say that you have the result stored in a string like this:
$s = "6000,5447"; //$s = $array->postcodes;
you can get the each value on an index in an array using this:
$values= explode(",", $s);
echo $values[0]; // 6000
Or even better.. you can store it as json, and retrieve it as json in array format.
Store it as a JSON field in MySQL, Laravel encode and decode them when you retrieve and save them respectively
in your migration
$table->json('field_name');
then in the model
protected $json = ['field_name'];
then whenever you access the field, laravel will convert it to an array for you, you don't have to call any explicit methods.
Doc - https://laravel.com/docs/5.7/eloquent-mutators#attribute-casting
// the final array all the post codes are collected.
$postCodes = [];
foreach (Model::pluck('postcodes') as $stringCodes)
foreach (explode(',', $stringCodes) as $postCode) $postCodes[] = $postCode;

Accessing element of a JArray -without Foreach loop

I'm developing an application where I receive a JSON array from an API. It has only one object. How do I access it in a single line syntax without using forEach loop?
object is inside array
[{"fiscalPeriod":8,"fiscalYear":2018,"calendarStart":"2018-02-11","calendarEnd":"2018-03-10"}]
Since you only have one item in the array you can get it out like this:
var array = [{"fiscalPeriod":8,"fiscalYear":2018,"calendarStart":"2018-02-11","calendarEnd":"2018-03-10"}];
var obj = array[0];
That square bracket syntax with the 0 in it says: give me the first item in the array.
Here some examples
var arr = [{"fiscalPeriod":8,"fiscalYear":2018,"calendarStart":"2018-02-11","calendarEnd":"2018-03-10"}];
console.log(arr[0].fiscalPeriod); // logs '8'
console.log(arr[0].fiscalYear); // logs '2018'
console.log(Object.keys(arr[0])); //["fiscalPeriod", "fiscalYear", "calendarStart", "calendarEnd"]
console.log(Object.keys(arr[0])[2]); //logs "calendarStart"

SSIS: How can I convert an NTEXT input to a string to perform a Split function in a script component?

I receive a Unicode text flat-file in which one column is a single fixed-length value, and the other contains a list values delimited by a vertical pipe '|'. The length of the second column and the number of delimited values it contains will vary greatly. In some cases the column will be up to 50000 characters wide, and could contain a thousand or more delimited values.
Input file Example:
[ObjectGUID]; [member]
{BD3481AF8-2CDG-42E2-BA93-73952AFB41F3}; CN=rGlynn SrechrshiresonIII,OU=Users,OU=PRV,OU=LOL,DC=ent,DC=keke,DC=cqb,DC=corp
{AC365A4F8-2CDG-42E2-BA33-73933AFB41F3}; CN=reeghler Johnson,OU=Users,OU=PRV,OU=LOL,DC=ent,DC=keke,DC=cqb,DC=corp|CN=rCoefler Cellins,OU=Users,OU=PRV,OU=LOL,DC=ent,DC=keke,DC=cqb,DC=corp|CN=rDasije M. Delmogeroo,OU=Users,OU=PRV,OU=LOL,DC=ent,DC=keke,DC=cqb,DC=corp|CN=rCurry T. Carrollton,OU=Users,OU=PRV,OU=LOL,DC=ent,DC=keke,DC=cqb,DC=corp|CN=yMica Macintosh,OU=Users,OU=PRV,OU=LOL,DC=ent,DC=keke,DC=cqb,DC=corp
My idea is to perform a Split operation on this column and create a new row for each value. I am attempting to use a script component to perform the split.
The width of the delimited column can easily exceed the 4000 character limit of DT-WSTR, so I chose NTEXT as the datatype. This presents problem because the .Split method I am familar with requires a string. I am attempting to convert the NTEXT to a string in the script component.
Here is my code:
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
var stringMember = Row.member.ToString();
var groupMembers = stringMember.Split('|');
foreach (var groupMember in groupMembers)
{
this.Output0Buffer.AddRow();
this.Output0Buffer.objectGUID = Row.objectGUID;
this.Output0Buffer.member = groupMember;
}
}
The output I am trying to get would be this:
[ObjectGUID] [member]
{BD3481AF8-2CDG-42E2-BA93-73952AFB41F3} CN=rGlynn SrechrshiresonIII,OU=Users,OU=PRV,OU=LOL,DC=ent,DC=keke,DC=cqb,DC=corp
{AC365A4F8-2CDG-42E2-BA33-73933AFB41F3} CN=reeghler Johnson,OU=Users,OU=PRV,OU=LOL,DC=ent,DC=keke,DC=cqb,DC=corp
{AC365A4F8-2CDG-42E2-BA33-73933AFB41F3} CN=rCoefler Cellins,OU=Users,OU=PRV,OU=LOL,DC=ent,DC=keke,DC=cqb,DC=corp
{AC365A4F8-2CDG-42E2-BA33-73933AFB41F3} CN=rDasije M. Delmogeroo,OU=Users,OU=PRV,OU=LOL,DC=ent,DC=keke,DC=cqb,DC=corp
{AC365A4F8-2CDG-42E2-BA33-73933AFB41F3} CN=rCurry T. Carrollton,OU=Users,OU=PRV,OU=LOL,DC=ent,DC=keke,DC=cqb,DC=corp
{AC365A4F8-2CDG-42E2-BA33-73933AFB41F3} CN=yMica Macintosh,OU=Users,OU=PRV,OU=LOL,DC=ent,DC=keke,DC=cqb,DC=corp
But what I am in fact getting is this:
[ObjectGUID] [member]
{BD3481AF8-2CDG-42E2-BA93-73952AFB41F3} Microsoft.SqlServer.Dts.Pipeline.BlobColumn
{AC365A4F8-2CDG-42E2-BA33-73933AFB41F3} Microsoft.SqlServer.Dts.Pipeline.BlobColumn
What might I be doing wrong?
The following code worked:
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
var blobLength = Convert.ToInt32(Row.member.Length);
var blobData = Row.member.GetBlobData(0, blobLength);
var stringData = System.Text.Encoding.Unicode.GetString(Row.member.GetBlobData(0, Convert.ToInt32(Row.member.Length)));
var groupMembers = stringData.Split('|');
foreach (var groupMember in groupMembers)
{
this.Output0Buffer.AddRow();
this.Output0Buffer.CN = Row.CN;
this.Output0Buffer.ObjectGUID = Row.ObjectGUID;
this.Output0Buffer.member = groupMember;
}
}
I was trying to perform an implicit conversion as I would in PowerShell, but was actually just passing some object metadata to the string output. This method properly splits my members and builds a complete row.

Immutable.js and how to replace list with new list

So, I have a default Immutable List:
var someObject = Immutable.Map({
myList: Immutable.List([4,5,6,7])
})
Now, I allow a person to overwrite this list, but I still need it to be contained within "someObject". Basically, if a user supplies an array, we replace what is in myList, with this "new List". I tried to accomplish like so, no luck.
basically, if this newList exists, use it - if not, either reset the myList to itself or ignore.
//example
var newList = [44444,99999]
someObject.get('myList')
.clear()
.set('myList', newList || someObject.get('myList'))
This is impossible, because the objects supplied by immutable.js are immutable - that's their point.
Instead of trying to mutate someObject, one way to accomplish what you're trying to do would be to define a defaultList = Immutable.List(4, 5, 6, 7) and then build someObject after the user has input their list. Something along the lines of
if (userList) {
someObject = Immutable.Map({myList: Immutable.List(userList)})
} else {
someObject = Immutable.Map({myList: Immutable.List(defaultList)})
}

Count the Number of Instances of a Word in Json String using Jackson Object Mapper

I'm using the Jackson Object Mapper (com.fasterxml.jackson.databind.ObjectMapper) to parse a json string and need to count the number of times the word "path"
occures in the string. The string looks like:
"rows":[{"path":{"uid":"2"},"fields":[]},{"path":{"uid":"4"},"fields":[]},{"path":{"uid":"12"},....
Does anyone know which API option is most efficeint for achieving this?
To count total number of 'childs' inside the 'rows' root, you can use a code like this:
String inputJsonString = "{\"rows\":[{\"path\":{\"uid\":\"2\"},\"fields\":[]},{\"path\":{\"uid\":\"4\"},\"fields\":[]},{\"path\":{\"uid\":\"12\"},\"fields\":[]}]}";
ObjectNode root = (ObjectNode) new ObjectMapper().readTree( inputJsonString );
root.get( "rows" ).size();
If you need to get the exact count of 'path' occurrences, you may use the code like this:
int counter = 0;
for( Iterator<JsonNode> i = root.get( "rows" ).iterator(); i.hasNext(); )
if( i.next().has( "path" ) )
counter++;
System.out.println(counter);