@@ -618,6 +618,42 @@ local html = import 'html.libsonnet';
618
618
},
619
619
],
620
620
},
621
+ {
622
+ name: 'parseCsvWithHeader' ,
623
+ params: ['str' , 'delimiter=","' , 'overwrite_duplicate_headers=true' ],
624
+ availableSince: 'upcoming' ,
625
+ description: [
626
+ html.p({},|||
627
+ Parses a CSV string into JSON. The CSV string would use the passed <code>delimiter</code> to split the columns.
628
+ ||| ),
629
+ html.p({},|||
630
+ In case of duplicate headers, the value would be overwritten by default. <code>overwrite_duplicate_headers</code>
631
+ provides an option handle duplicate headers by appending a sequence number at the end of header.
632
+ This example should make it clear:
633
+ ||| ),
634
+ html.pre({}, |||
635
+ std.parseCsvWithHeaders("head1,head1,head1\nvalue1,value2,value3", overwrite_duplicate_headers=false)
636
+ ||| ),
637
+ html.p({},|||
638
+ This would result in following output:
639
+ ||| ),
640
+ html.pre({}, |||
641
+ [
642
+ {
643
+ "head1": "value1",
644
+ "head1__1": "value2",
645
+ "head1__2": "value3",
646
+ },
647
+ ],
648
+ ||| ),
649
+ ],
650
+ examples: [
651
+ {
652
+ input: "std.parseCsvWithHeader('id,name\n 1,foo\n 2,bar')" ,
653
+ output: std.parseCsvWithHeader('id,name\n 1,foo\n 2,bar' ),
654
+ },
655
+ ],
656
+ },
621
657
{
622
658
name: 'encodeUTF8' ,
623
659
params: ['str' ],
@@ -932,6 +968,45 @@ local html = import 'html.libsonnet';
932
968
||| ),
933
969
],
934
970
},
971
+ {
972
+ name: 'manifestCsv' ,
973
+ params: ['json' , 'headers=null' ],
974
+ availableSince: 'upcoming' ,
975
+ description: [
976
+ html.p({}, |||
977
+ Convert the given csv compatible json to a CSV.
978
+ ||| ),
979
+ html.pre({}, |||
980
+ std.manifestCsv(
981
+ [
982
+ {
983
+ "id": 1,
984
+ "name": "foo",
985
+ "x": "baz",
986
+ },
987
+ {
988
+ "id": 2,
989
+ "name": "bar",
990
+ },
991
+ ],
992
+ ["id", "name"],
993
+ ||| ),
994
+ html.p({}, |||
995
+ Yields a string containing this CSV:
996
+ ||| ),
997
+ html.pre({}, |||
998
+ id,name
999
+ 1,foo
1000
+ 2,bar
1001
+ ||| ),
1002
+ html.p({}, |||
1003
+ If <code>json</code> param is not a valid csv compatible object, it would be an error.
1004
+ ||| ),
1005
+ html.p({}, |||
1006
+ The <code>headers</code> param adds is an optional which would default to all fields in the first object.
1007
+ ||| ),
1008
+ ],
1009
+ },
935
1010
{
936
1011
name: 'manifestXmlJsonml' ,
937
1012
params: ['value' ],
0 commit comments